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
Diff: src/ModbusMaster/ModbusMaster.cpp
- Revision:
- 197:594afd088f32
- Parent:
- 190:af7ab603c9fe
- Child:
- 199:d65ed41d4dd4
diff -r af7ab603c9fe -r 594afd088f32 src/ModbusMaster/ModbusMaster.cpp
--- a/src/ModbusMaster/ModbusMaster.cpp Thu Oct 06 15:24:10 2016 +0000
+++ b/src/ModbusMaster/ModbusMaster.cpp Thu Oct 06 20:59:59 2016 +0000
@@ -9,6 +9,7 @@
#include "MTSLog.h"
#include "BLEDataHandler.h"
#include "ModbusMaster.h"
+#include "ModbusMasterApi.h"
#include "mod.h"
#include "MbedJSONValue.h"
@@ -144,8 +145,8 @@
}
if( iter->second.node != 0 ) {
- unsigned char rd_buf[10];
- memset( rd_buf, 0, 10 );
+ 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 ) {
@@ -265,9 +266,13 @@
osEvent evt = ModbusMasterMailBox.get(50);
if (evt.status == osEventMail) {
Message_t *mail = (Message_t*)evt.value.p;
- logInfo("Mail Received: Action: %d, New Input File: %s", mail->action, mail->controlFile);
+ 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);
- ModbusRegisterMap.clear();
break;
}
Thread::wait(5000);
@@ -293,6 +298,7 @@
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);
@@ -300,6 +306,33 @@
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;
@@ -318,9 +351,170 @@
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;
+ }
+
}
\ No newline at end of file
