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:
- 149:950c90425f7c
- Parent:
- 148:ee3cbaafe355
- Child:
- 150:4db35c8234b4
--- a/src/ModbusMaster/ModbusMaster.cpp Wed Sep 28 17:57:36 2016 +0000
+++ b/src/ModbusMaster/ModbusMaster.cpp Fri Sep 30 12:22:20 2016 +0000
@@ -23,12 +23,14 @@
* @param (IN) args (user-defined arguments)
* @return none
*****************************************************************************/
+std::map<std::string,VirtualCommand> VirtualCommandMap;
+
char ModbusMasterScratchBuf[1024];
void ModbusMaster(void const *args)
{
logInfo("%s ModbusMaster has started...", __func__);
bool status;
- bool input;
+ RegisterType_t regType;
bool move_up = true;
MbedJSONValue json_value;
@@ -42,13 +44,38 @@
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) {
- input = true;
- if( (strncmp( i->name, "input", (strlen("input")-1)) == 0) ||
- (strncmp( i->name, "output", (strlen("output")-1)) == 0)) {
+ if( (strncmp( i->name, "vcmd", (strlen("input")-1)) == 0) ) {
+ status = GLOBAL_mdot->readUserFile(i->name, ModbusMasterScratchBuf, 1024);
+ if( status != true ) {
+ logInfo("(%d)read file failed, status=%d", __LINE__, status);
+ } else {
+ logInfo("(%d)Read File SUCCESS: %s", __LINE__, ModbusMasterScratchBuf );
+ }
- if( (strncmp( i->name, "output", (strlen("output")-1)) == 0) ) {
- input = false;
- }
+ 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();
+ printf("\rProcessd virtual command file: id=%s\r\n", 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 ) {
logInfo("(%d)FOUND FILE: %s", __LINE__, i->name);
@@ -74,9 +101,12 @@
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].input = input;
+ ModbusRegisterMap[id].regType = regType;
ModbusRegisterMap[id].simulated = false;
ModbusRegisterMap[id].valid = true;
+ if( (regType == REG_TYPE_VINPUT) || (regType == REG_TYPE_VOUTPUT) ) {
+ ModbusRegisterMap[id].vcmd = json_value["vcmd"].get<std::string>().c_str();
+ }
}
}
@@ -86,8 +116,9 @@
std::map<std::string, ModbusRegister>::iterator iter;
for (iter = ModbusRegisterMap.begin(); iter != ModbusRegisterMap.end(); ++iter) {
if( iter->second.simulated == true ) {
- std::map<std::string, SimulateInput>::iterator iter;
- for (iter = SimulateInputMap.begin(); iter != SimulateInputMap.end(); ++iter) {
+ std::map<std::string, SimulateInput>::iterator simiter;
+ for (simiter = SimulateInputMap.begin(); simiter != SimulateInputMap.end(); ++simiter) {
+// logInfo("simulating input=%s, min=%2.2f, max=%2.2f, start_value=%2.2f, step=%2.2f",iter->first.c_str(), SimulateInputMap[iter->first].min, SimulateInputMap[iter->first].max, SimulateInputMap[iter->first].start_value, SimulateInputMap[iter->first].step);
if( (SimulateInputMap[iter->first].min == 0) && (SimulateInputMap[iter->first].max == 0) ) {
ModbusRegisterMap[iter->first].float_value = SimulateInputMap[iter->first].start_value;
} else {
@@ -112,7 +143,7 @@
if( iter->second.node != 0 ) {
-// logInfo("Processing Input: node=%d, reg=%d, size=%d, order=%d", iter->second.node, iter->second.reg, iter->second.size, iter->second.order );
+ 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 );
SendModbusCommand(iter->second.node, iter->second.reg, iter->second.size);
Thread::wait(30);
switch( iter->second.type ) {
@@ -141,7 +172,7 @@
}
}
- if( (iter->second.node == 0) && (iter->second.input == false) ) {
+ 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)ModbusRegisterMap[iter->first].float_value;
@@ -149,7 +180,7 @@
dout2 = (bool)ModbusRegisterMap[iter->first].float_value;
}
}
- if( (iter->second.node == 0) && (iter->second.input == true) ) {
+ 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
@@ -160,6 +191,34 @@
}
}
+ // 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 '*':
+ case '/':
+ case '+':
+ case '-':
+ default:
+ break;
+ }
+ }
+
osEvent evt = ModbusMasterMailBox.get(50);
if (evt.status == osEventMail) {
Message_t *mail = (Message_t*)evt.value.p;
