Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ModbusMaster/ModbusMaster.cpp
- Committer:
- davidjhoward
- Date:
- 2016-10-06
- Revision:
- 197:594afd088f32
- Parent:
- 190:af7ab603c9fe
- Child:
- 199:d65ed41d4dd4
File content as of revision 197:594afd088f32:
/******************************************************************************
*
* File: ModbusMaster.cpp
* Desciption: source for the ICE Modbus Master
*
*****************************************************************************/
#include "global.h"
#include <stdio.h>
#include "MTSLog.h"
#include "BLEDataHandler.h"
#include "ModbusMaster.h"
#include "ModbusMasterApi.h"
#include "mod.h"
#include "MbedJSONValue.h"
DigitalOut dout1(PC_1);
DigitalOut dout2(PA_1);
DigitalIn flow_switch(PA_0);
DigitalIn dinp2(PC_13);
/*****************************************************************************
* Function: ModbusMaster
* Description: entry point for the Modbus Master
*
* @param (IN) args (user-defined arguments)
* @return none
*****************************************************************************/
std::map<std::string,VirtualCommand> VirtualCommandMap;
char ModbusMasterScratchBuf[MAX_FILE_SIZE];
void ModbusMaster(void const *args)
{
logInfo("%s ModbusMaster has started...", __func__);
bool status;
RegisterType_t regType;
mod_init();
DigitalOut mod_power(PA_11);
mod_power = 0; // provide power to the modbus
while( true ) {
MbedJSONValue json_value;
// configure modbus registers based in all files that start with "input"
std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
if( (strncmp( i->name, "vcmd", (strlen("input")-1)) == 0) ) {
status = GLOBAL_mdot->readUserFile(i->name, ModbusMasterScratchBuf, MAX_FILE_SIZE);
if( status != true ) {
logInfo("(%d)read file failed, status=%d", __LINE__, status);
} else {
logInfo("(%s:%d)loading File: %s", __func__, __LINE__, i->name );
}
parse( json_value, ModbusMasterScratchBuf );
std::string id = json_value["id"].get<std::string>().c_str();
VirtualCommandMap[id].constant = atof(json_value["constant"].get<std::string>().c_str());
VirtualCommandMap[id].opl = json_value["opl"].get<std::string>().c_str();
VirtualCommandMap[id].opr = json_value["opr"].get<std::string>().c_str();
VirtualCommandMap[id].op = json_value["op"].get<std::string>().c_str();
logInfo("Processd virtual command file: id=%s", id.c_str());
continue;
}
regType = REG_TYPE_NONE;
if( (strncmp( i->name, "input", (strlen("input")-1)) == 0) ) {
regType = REG_TYPE_INPUT;
} else if( (strncmp( i->name, "output", (strlen("output")-1)) == 0) ) {
regType = REG_TYPE_OUTPUT;
} else if( (strncmp( i->name, "vinput", (strlen("vinput")-1)) == 0) ) {
regType = REG_TYPE_VINPUT;
} else if( (strncmp( i->name, "voutput", (strlen("voutput")-1)) == 0) ) {
regType = REG_TYPE_VOUTPUT;
}
if( regType != REG_TYPE_NONE ) {
status = GLOBAL_mdot->readUserFile(i->name, ModbusMasterScratchBuf, MAX_FILE_SIZE);
if( status != true ) {
logInfo("(%d)read file failed, status=%d", __LINE__, status);
} else {
logInfo("(%s:%d)loading File: %s", __func__, __LINE__, i->name );
}
parse( json_value, ModbusMasterScratchBuf );
std::string id = json_value["id"].get<std::string>().c_str();
ModbusRegisterMap[id].name = json_value["name"].get<std::string>().c_str();
ModbusRegisterMap[id].units = json_value["units"].get<std::string>().c_str();
ModbusRegisterMap[id].min = atof(json_value["min"].get<std::string>().c_str());
ModbusRegisterMap[id].max = atof(json_value["max"].get<std::string>().c_str());
ModbusRegisterMap[id].node = atoi(json_value["node"].get<std::string>().c_str());
ModbusRegisterMap[id].reg = atoi(json_value["reg"].get<std::string>().c_str());
ModbusRegisterMap[id].rtype = atoi(json_value["rtype"].get<std::string>().c_str());
ModbusRegisterMap[id].type = atoi(json_value["type"].get<std::string>().c_str());
ModbusRegisterMap[id].size = atoi(json_value["size"].get<std::string>().c_str());
ModbusRegisterMap[id].order = atoi(json_value["order"].get<std::string>().c_str());
ModbusRegisterMap[id].fmt = json_value["fmt"].get<std::string>().c_str();
ModbusRegisterMap[id].rfreq = atoi(json_value["rfreq"].get<std::string>().c_str());
ModbusRegisterMap[id].regType = regType;
ModbusRegisterMap[id].simulated = false;
ModbusRegisterMap[id].errflag = 0;
if( (regType == REG_TYPE_VINPUT) || (regType == REG_TYPE_VOUTPUT) ) {
ModbusRegisterMap[id].vcmd = json_value["vcmd"].get<std::string>().c_str();
}
}
}
osSignalSet(mainThreadId, sig_output_continue);
// read modbus registers that have been configured.
while ( true ) {
std::map<std::string, ModbusRegister>::iterator iter;
for (iter = ModbusRegisterMap.begin(); iter != ModbusRegisterMap.end(); ++iter) {
if( iter->second.simulated == true ) {
// printf("\r\nsimulating input=%s, min=%2.2f, max=%2.2f, start_value=%2.2f, up_step=%2.2f, down_step=%2.2f moving_up=%d\r\n",iter->first.c_str(), SimulateInputMap[iter->first].min, SimulateInputMap[iter->first].max, SimulateInputMap[iter->first].start_value, SimulateInputMap[iter->first].up_step, SimulateInputMap[iter->first].down_step, SimulateInputMap[iter->first].moving_up);
if( (SimulateInputMap[iter->first].min == 0) && (SimulateInputMap[iter->first].max == 0) ) {
ModbusRegisterMap[iter->first].float_value = SimulateInputMap[iter->first].start_value;
// printf("simulating input=%s, value=%2.2f\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value);
} else {
if( ModbusRegisterMap[iter->first].float_value >= SimulateInputMap[iter->first].max ) {
SimulateInputMap[iter->first].moving_up = false;
// printf("simulating down input=%s, value=%2.2f - %2.2f\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value, SimulateInputMap[iter->first].down_step);
ModbusRegisterMap[iter->first].float_value = ModbusRegisterMap[iter->first].float_value - SimulateInputMap[iter->first].down_step;
} else if( ModbusRegisterMap[iter->first].float_value <= SimulateInputMap[iter->first].min ) {
SimulateInputMap[iter->first].moving_up = true;
// printf("simulating up input=%s, value=%2.2f + %2.2f\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value, SimulateInputMap[iter->first].up_step);
ModbusRegisterMap[iter->first].float_value = ModbusRegisterMap[iter->first].float_value + SimulateInputMap[iter->first].up_step;
} else {
if( SimulateInputMap[iter->first].moving_up == true ) {
// printf("continue simulate up input=%s, value=%2.2f + %2.2f\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value, SimulateInputMap[iter->first].up_step);
ModbusRegisterMap[iter->first].float_value = ModbusRegisterMap[iter->first].float_value + SimulateInputMap[iter->first].up_step;
} else {
// printf("continue simulate down input=%s, value=%2.2f - %2.2f\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value, SimulateInputMap[iter->first].down_step);
ModbusRegisterMap[iter->first].float_value = ModbusRegisterMap[iter->first].float_value - SimulateInputMap[iter->first].down_step;
}
}
// printf("simulating input=%s, value=%2.2f\r\n\r\n",iter->first.c_str(), ModbusRegisterMap[iter->first].float_value);
}
continue;
}
if( iter->second.node != 0 ) {
unsigned char rd_buf[16];
memset( rd_buf, 0, 16 );
logInfo("Processing Input: tag=%s, node=%d, reg=%d, size=%d, order=%d", iter->first.c_str(), iter->second.node, iter->second.reg, iter->second.size, iter->second.order );
int ret = mod_read(iter->second.node, iter->second.rtype, iter->second.reg, iter->second.size, rd_buf);
switch( iter->second.type ) {
case TYPE_32BIT_FLOAT:
float float_value;
if( ret != MOD_ERROR_NONE ) {
ModbusRegisterMap[iter->first].errflag = ret;
break;
}
status = ReadModbus_32bit_float( &float_value, iter->second.order, rd_buf );
if( status == true ) {
ModbusRegisterMap[iter->first].float_value = float_value;
ModbusRegisterMap[iter->first].errflag = 0;
// logInfo("Modbus Tag:%s value=%2.2f", iter->first.c_str(), float_value );
} else {
ModbusRegisterMap[iter->first].errflag = 1000;
// logInfo("Modbus Read Failed, tag=%s", iter->first.c_str() );
}
break;
case TYPE_32BIT_INT:
int32_t int32_value;
if( ret != MOD_ERROR_NONE ) {
ModbusRegisterMap[iter->first].errflag = ret;
break;
}
status = ReadModbus_32bit_int( &int32_value, iter->second.order, rd_buf );
if( status == true ) {
ModbusRegisterMap[iter->first].float_value = int32_value;
ModbusRegisterMap[iter->first].errflag = 0;
logInfo("Modbus Tag:%s value=%2.2f", iter->first.c_str(), ModbusRegisterMap[iter->first].float_value );
} else {
ModbusRegisterMap[iter->first].errflag = 1000;
logInfo("Modbus Read Failed, tag=%s", iter->first.c_str() );
}
break;
case TYPE_32BIT_UINT:
break;
case TYPE_16BIT_INT:
break;
case TYPE_16BIT_UINT:
break;
default:
break;
}
}
if( (iter->second.node == 0) && (iter->second.regType == REG_TYPE_OUTPUT) ) {
// logInfo("processing PIN output=%s, reg=%d, value=%d",iter->first.c_str(), ModbusRegisterMap[iter->first].reg, (bool)ModbusRegisterMap[iter->first].float_value);
if( ModbusRegisterMap[iter->first].reg == 1 ) {
dout1 = (bool)((int)ModbusRegisterMap[iter->first].float_value&0x1);
} else {
dout2 = (bool)((int)ModbusRegisterMap[iter->first].float_value&0x1);
}
}
if( (iter->second.node == 0) && (iter->second.regType == REG_TYPE_INPUT) ) {
// logInfo("processing PIN input=%s, reg=%d, value=%d",iter->first.c_str(), ModbusRegisterMap[iter->first].reg, (bool)ModbusRegisterMap[iter->first].float_value);
if( ModbusRegisterMap[iter->first].reg == 1 ) {
// digital input
ModbusRegisterMap[iter->first].float_value = (float)flow_switch.read();
} else {
ModbusRegisterMap[iter->first].float_value = (float)dinp2.read();
}
}
}
// now update all of the virtual registers
for (iter = ModbusRegisterMap.begin(); iter != ModbusRegisterMap.end(); ++iter) {
if( (ModbusRegisterMap[iter->first].regType != REG_TYPE_VINPUT) && (ModbusRegisterMap[iter->first].regType != REG_TYPE_VOUTPUT) ) {
continue;
}
logInfo("Processing Virtual Input: vcmd:%s, tag:%s, opl:%s, opr:%s, op:%s, constant:%.4f", iter->second.vcmd.c_str(), iter->first.c_str(),
VirtualCommandMap[iter->second.vcmd].opl.c_str(),
VirtualCommandMap[iter->second.vcmd].opr.c_str(),
VirtualCommandMap[iter->second.vcmd].op.c_str(),
VirtualCommandMap[iter->second.vcmd].constant
);
switch( VirtualCommandMap[iter->second.vcmd].op.c_str()[0] ) {
case '=':
ModbusRegisterMap[iter->first].float_value = ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value;
logInfo("Setting tag=%s, equal to (value=%2.2f)", iter->first.c_str(),
//VirtualCommandMap[iter->second.vcmd].opl.c_str(),
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value
);
break;
case '*':
ModbusRegisterMap[iter->first].float_value =
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value *
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opr].float_value;
logInfo("Setting tag=%s, equal to (%2.2f*%2.2f) = %2.2f", iter->first.c_str(),
//VirtualCommandMap[iter->second.vcmd].opl.c_str(),
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value,
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opr].float_value,
ModbusRegisterMap[iter->first].float_value
);
case '/':
if( ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opr].float_value != 0 ) {
ModbusRegisterMap[iter->first].float_value =
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value /
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opr].float_value;
logInfo("Setting tag=%s, equal to (%2.2f/%2.2f) = %2.2f", iter->first.c_str(),
//VirtualCommandMap[iter->second.vcmd].opl.c_str(),
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opl].float_value,
ModbusRegisterMap[VirtualCommandMap[iter->second.vcmd].opr].float_value,
ModbusRegisterMap[iter->first].float_value
);
} else {
logInfo("NOT DOING DIVIDE BY ZERO");
}
break;
case '+':
case '-':
default:
break;
}
}
osEvent evt = ModbusMasterMailBox.get(50);
if (evt.status == osEventMail) {
Message_t *mail = (Message_t*)evt.value.p;
printf("Mail Received: Action: %d, New Input File: %s\r\n", mail->action, mail->controlFile);
if( mail->action == ACTION_EXEC_CMD ) {
ModbusMasterExecCmd( mail->controlFile );
} else {
ModbusRegisterMap.clear();
}
ModbusMasterMailBox.free(mail);
break;
}
Thread::wait(5000);
}
}
}
bool ReadModbus_32bit_float( float *float_value, int order, unsigned char *rd_buf )
{
MR_REGISTER_32_BIT_FLOAT value;
switch( order ) {
case BigEndian:
value.b.lo_lo = rd_buf[3];
value.b.lo_hi = rd_buf[2];
value.b.hi_lo = rd_buf[1];
value.b.hi_hi = rd_buf[0];
break;
case BigEndianReverseWord:
value.b.lo_lo = rd_buf[1];
value.b.lo_hi = rd_buf[0];
value.b.hi_lo = rd_buf[3];
value.b.hi_hi = rd_buf[2];
break;
default:
printf("%s:%d: order not supported\r\n",__func__,__LINE__);
return false;
}
// printf("0x%x 0x%x 0x%x 0x%x (%2.2f)\r\n", value.b.hi_hi, value.b.hi_lo, value.b.lo_hi, value.b.lo_lo, value.f);
*float_value = value.f;
return true;
}
bool WriteModbus_32bit_float( float float_value, int order, unsigned char *xmt_buf )
{
MR_REGISTER_32_BIT_FLOAT value;
value.f = float_value;
switch( order ) {
case BigEndian:
xmt_buf[3] = value.b.lo_lo;
xmt_buf[2] = value.b.lo_hi;
xmt_buf[1] = value.b.hi_lo;
xmt_buf[0] = value.b.hi_hi;
break;
case BigEndianReverseWord:
xmt_buf[1] = value.b.lo_lo;
xmt_buf[0] = value.b.lo_hi;
xmt_buf[3] = value.b.hi_lo;
xmt_buf[2] = value.b.hi_hi;
break;
default:
printf("%s:%d: order not supported\r\n",__func__,__LINE__);
return false;
}
printf("%s:%d: 0x%x 0x%x 0x%x 0x%x (%2.2f)\r\n",__func__,__LINE__, value.b.hi_hi, value.b.hi_lo, value.b.lo_hi, value.b.lo_lo, value.f);
return true;
}
bool ReadModbus_32bit_int( int32_t *int32_value, int order, unsigned char *rd_buf )
{
MR_REGISTER_32BIT_INT value;
switch( order ) {
case BigEndian:
value.b.lo_lo = rd_buf[3];
value.b.lo_hi = rd_buf[2];
value.b.hi_lo = rd_buf[1];
value.b.hi_hi = rd_buf[0];
break;
case BigEndianReverseWord:
value.b.lo_lo = rd_buf[1];
value.b.lo_hi = rd_buf[0];
value.b.hi_lo = rd_buf[3];
value.b.hi_hi = rd_buf[2];
break;
default:
printf("%s:%d: order not supported\r\n",__func__,__LINE__);
return false;
}
// printf("0x%x 0x%x 0x%x 0x%x (%d)\r\n", value.b.hi_hi, value.b.hi_lo, value.b.lo_hi, value.b.lo_lo, value.i);
*int32_value = value.i;
return true;
}
bool WriteModbus_32bit_int( int32_t int32_value, int order, unsigned char *xmt_buf )
{
MR_REGISTER_32BIT_INT value;
value.i = int32_value;
switch( order ) {
case BigEndian:
xmt_buf[3] = value.b.lo_lo;
xmt_buf[2] = value.b.lo_hi;
xmt_buf[1] = value.b.hi_lo;
xmt_buf[0] = value.b.hi_hi;
break;
case BigEndianReverseWord:
xmt_buf[1] = value.b.lo_lo;
xmt_buf[0] = value.b.lo_hi;
xmt_buf[3] = value.b.hi_lo;
xmt_buf[2] = value.b.hi_hi;
break;
default:
printf("%s:%d: order not supported\r\n",__func__,__LINE__);
return false;
}
printf("%s:%d: 0x%x 0x%x 0x%x 0x%x (%d)\r\n", __func__, __LINE__, value.b.hi_hi, value.b.hi_lo, value.b.lo_hi, value.b.lo_lo, value.i);
return true;
}
bool WriteModbus_16bit_int( int16_t int16_value, int order, unsigned char *xmt_buf )
{
MR_REGISTER_16BIT_INT value;
value.w = int16_value;
switch( order ) {
case BigEndian:
case BigEndianReverseWord:
xmt_buf[1] = value.b.lo;
xmt_buf[0] = value.b.hi;
break;
default:
printf("%s:%d: order not supported\r\n",__func__,__LINE__);
return false;
}
printf("%s:%d: 0x%x 0x%x (%d)\r\n", __func__,__LINE__, value.b.hi, value.b.lo, value.w);
return true;
}
void ModbusMasterExecCmd( char *cmd )
{
MbedJSONValue json_cmd;
bool status;
int ret;
unsigned char rd_buf[16];
// printf("%s:%d: command=%s\r\n", __func__, __LINE__, cmd );
parse( json_cmd, cmd );
std::string id = json_cmd["id"].get<std::string>().c_str();
int node = atoi(json_cmd["node"].get<std::string>().c_str());
int func = atoi(json_cmd["func"].get<std::string>().c_str());
int sreg = atoi(json_cmd["sreg"].get<std::string>().c_str());
int nreg = atoi(json_cmd["nreg"].get<std::string>().c_str());
int dtype = atoi(json_cmd["dtype"].get<std::string>().c_str());
int order = atoi(json_cmd["order"].get<std::string>().c_str());
float value = atof(json_cmd["value"].get<std::string>().c_str());
switch( func ) {
case MOD_FUNC_GET_HREG: // read holding register
case MOD_FUNC_GET_IREG: // read input register
ret = mod_read(node, func, sreg, nreg, rd_buf);
switch( dtype ) {
case TYPE_32BIT_FLOAT: {
float float_value;
if( ret != MOD_ERROR_NONE ) {
printf("CMD: %s:%d: %s failed, errflag=%d\r\n", __func__, __LINE__, id.c_str(), ret );
break;
}
status = ReadModbus_32bit_float( &float_value, order, rd_buf );
if( status == true ) {
printf("CMD: %s:%d: %s value=%2.2f\r\n", __func__, __LINE__, id.c_str(), float_value );
} else {
printf("CMD: %s:%d: %s failed\r\n", __func__, __LINE__, id.c_str() );
}
break;
}
case TYPE_32BIT_INT:
case TYPE_32BIT_UINT: {
int32_t int32_value;
if( ret != MOD_ERROR_NONE ) {
printf("CMD: %s:%d: %s failed, errflag=%d\r\n", __func__, __LINE__, id.c_str(), ret );
break;
}
status = ReadModbus_32bit_int( &int32_value, order, rd_buf );
if( status == true ) {
printf("CMD: %s:%d: %s value=%d\r\n", __func__, __LINE__, id.c_str(), int32_value );
} else {
printf("CMD: %s:%d: %s failed\r\n", __func__, __LINE__, id.c_str() );
}
break;
}
case TYPE_16BIT_INT:
case TYPE_16BIT_UINT:
break;
default:
break;
}
break;
case MOD_FUNC_SET_HREG: // write holding register
case MOD_FUNC_SET_HREGS: // write multiple registers (only supports 2 right now)
case MOD_FUNC_SET_COIL: { // write coil
unsigned char xmt_buf[10];
switch( dtype ) {
case TYPE_32BIT_FLOAT: {
status = WriteModbus_32bit_float( value, order, xmt_buf );
if( status != true ) {
printf("CMD: %s:%d: %s failed\r\n", __func__, __LINE__, id.c_str() );
return;
}
printf("%s:%d: 0x%x 0x%x 0x%x 0x%x\r\n", __func__,__LINE__, xmt_buf[0], xmt_buf[1], xmt_buf[2], xmt_buf[3]);
break;
}
case TYPE_32BIT_INT:
case TYPE_32BIT_UINT: {
status = WriteModbus_32bit_int( (int32_t)value, order, xmt_buf );
if( status != true ) {
printf("CMD: %s:%d: %s failed\r\n", __func__, __LINE__, id.c_str() );
return;
}
break;
}
case TYPE_16BIT_INT:
case TYPE_16BIT_UINT:
status = WriteModbus_16bit_int( (int16_t)value, order, xmt_buf );
if( status != true ) {
printf("CMD: %s:%d: %s failed\r\n", __func__, __LINE__, id.c_str() );
return;
}
printf("%s:%d: 0x%x 0x%x\r\n", __func__,__LINE__, xmt_buf[0], xmt_buf[1]);
break;
default:
printf("CMD: %s:%d: %s NOT IMPLEMENTED\r\n", __func__, __LINE__, id.c_str() );
return;
}
printf("%s:%d: 0x%x 0x%x 0x%x 0x%x\r\n", __func__,__LINE__, xmt_buf[0], xmt_buf[1], xmt_buf[2], xmt_buf[3]);
ret = mod_write(node, func, sreg, nreg, xmt_buf);
if( ret != MOD_ERROR_NONE ) {
printf("CMD: %s:%d: %s failed, errflag=%d\r\n", __func__, __LINE__, id.c_str(), ret );
} else {
printf("CMD: %s:%d: %s wrote to modbus func=%d reg=%d value=%2.2f, errflag=%d\r\n", __func__, __LINE__, id.c_str(), func, sreg, value, ret );
}
break;
}
default:
printf("CMD: %s:%d: %s failed, errflag=%d\r\n", __func__, __LINE__, id.c_str(), ret );
break;
}
}
