Skip to content

Commit

Permalink
Fix modbus
Browse files Browse the repository at this point in the history
* Support control modbus json
  • Loading branch information
hung-eoh committed Aug 1, 2024
1 parent 6968e18 commit 1318de6
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 51 deletions.
7 changes: 6 additions & 1 deletion linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static uint16_t port;
static const char* user;
static const char* pass;

ERaSocket mbTcpClient;

/* This function will run every time ERa is connected */
ERA_CONNECTED() {
printf("ERa connected!\r\n");
Expand All @@ -52,6 +54,9 @@ void setup() {
/* Setup Zigbee */
setupZigbee();

/* Setup Client for Modbus TCP/IP */
ERa.setModbusClient(mbTcpClient);

ERa.setAppLoop(false);
ERa.setBoardID(boardID);
ERa.begin(auth, host, port, user, pass);
Expand All @@ -74,4 +79,4 @@ int main(int argc, char* argv[]) {
}

return 0;
}
}
7 changes: 6 additions & 1 deletion src/ERa/ERaProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@ bool ERaProto<Transp, Flash>::processActionChip(const char* payload, cJSON* root
item = cJSON_GetObjectItem(dataItem, "control");
if (cJSON_IsString(item)) {
Base::Modbus::parseModbusConfig(item->valuestring, hash, payload, true, false);
break;
}
item = cJSON_GetObjectItem(dataItem, "modbus_control");
if (cJSON_IsObject(item)) {
Base::Modbus::parseModbusConfig(item, hash, payload, true, true);
}
break;
case ERaChipCfgT::CHIP_CONTROL_ALIAS: {
Expand All @@ -645,7 +650,7 @@ bool ERaProto<Transp, Flash>::processActionChip(const char* payload, cJSON* root
if (cJSON_IsString(keyItem)) {
// Handle command
Base::Modbus::addModbusAction(keyItem->valuestring,
typeAction, paramAction);
typeAction, paramAction);
}
}
}
Expand Down
58 changes: 44 additions & 14 deletions src/Modbus/ERaModbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,19 @@ class ERaModbus
void initModbusConfig() {
char* ptr = nullptr;
ptr = this->thisApi().readFromFlash(FILENAME_CONFIG);
this->updateConfig(this->modbusConfig, ptr, "configuration", "modbus_configuration");
this->updateConfig(this->modbusConfig, ptr, "configuration", "modbus_configuration", false);
if (this->initialized) {
ModbusStream::setBaudRate(this->modbusConfig->baudSpeed);
}
free(ptr);
ptr = this->thisApi().readFromFlash(FILENAME_CONTROL);
this->updateConfig(this->modbusControl, ptr, "control", nullptr);
this->updateConfig(this->modbusControl, ptr, "control", "modbus_control", true);
free(ptr);
ptr = nullptr;
}

void updateConfig(ERaModbusEntry* config, const char* buf, const char* name, const char* jsName) {
void updateConfig(ERaModbusEntry* config, const char* buf, const char* name,
const char* jsName, bool isControl = false) {
cJSON* root = cJSON_Parse(buf);
if (!cJSON_IsObject(root)) {
cJSON_Delete(root);
Expand All @@ -381,16 +382,19 @@ class ERaModbus

cJSON* data = cJSON_GetObjectItem(root, "data");
cJSON* item = cJSON_GetObjectItem(data, name);
if (item == nullptr) {
item = cJSON_GetObjectItem(data, jsName);
}
if (cJSON_IsString(item)) {
config->parseConfig(item->valuestring, false);
}
else {
item = cJSON_GetObjectItem(data, jsName);
if (cJSON_IsObject(item)) {
config->parseConfig(item, true);
ModbusTransp::parseConfig(config, true);
else if (cJSON_IsObject(item)) {
config->parseConfig(item, true);
if (!isControl) {
ModbusTransp::parseConfig(item, true);
}
}

item = cJSON_GetObjectItem(data, "hash_id");
if (cJSON_IsString(item)) {
config->updateHashID(item->valuestring, true);
Expand Down Expand Up @@ -519,6 +523,7 @@ class ERaModbus
bool checkPubDataInterval();
void delayModbus(ERaUInt_t address, bool unlock = false, bool skip = false);
void delays(MillisTime_t ms);
void delaysMutex(MillisTime_t ms);
ModbusConfigAlias_t* getModbusAlias(const char* key);
ModbusConfig_t* getModbusConfig(ERaUInt_t id);
ModbusConfig_t* getModbusConfigWithAddress(uint8_t addr);
Expand Down Expand Up @@ -586,7 +591,8 @@ class ERaModbus
if (this->clientTCP == nullptr) {
return;
}
if (!this->modbusConfig->hasTcpIp) {
if (!this->modbusConfig->hasTcpIp &&
!this->modbusControl->hasTcpIp) {
return;
}
MillisTime_t startMillis = ERaMillis();
Expand Down Expand Up @@ -692,9 +698,11 @@ void ERaModbus<Api>::readModbusConfig() {
ERA_F(MODBUS_STRING_TOTAL_WRITE), this->totalWrite);
this->addScanData();
this->dataBuff.done();
if (!this->skipPubModbus && !ModbusTransp::isNewReport()) {
this->thisApi().modbusDataWrite(&this->dataBuff);

if (this->skipPubModbus || ModbusTransp::isNewReport()) {
return;
}
this->thisApi().modbusDataWrite(&this->dataBuff);
}

template <class Api>
Expand Down Expand Up @@ -775,10 +783,29 @@ void ERaModbus<Api>::delays(MillisTime_t ms) {
} while (ERaRemainingTime(startMillis, ms));
}

template <class Api>
void ERaModbus<Api>::delaysMutex(MillisTime_t ms) {
if (ms <= ERA_MODBUS_MUTEX_MS) {
this->delays(ms);
ERaGuardUnlock(this->mutex);
return;
}

ms -= ERA_MODBUS_MUTEX_MS;
this->delays(ERA_MODBUS_MUTEX_MS);
ERaGuardUnlock(this->mutex);
this->delays(ms);
}

template <class Api>
ModbusConfigAlias_t* ERaModbus<Api>::getModbusAlias(const char* key) {
ERaList<ModbusConfigAlias_t*>::iterator* b = this->modbusConfig->modbusConfigAliasParam.begin();
const ERaList<ModbusConfigAlias_t*>::iterator* e = this->modbusConfig->modbusConfigAliasParam.end();
for (ERaList<ModbusConfigAlias_t*>::iterator* it = this->modbusConfig->modbusConfigAliasParam.begin(); it != e; it = it->getNext()) {
if (this->modbusControl->isJson) {
b = this->modbusControl->modbusConfigAliasParam.begin();
e = this->modbusControl->modbusConfigAliasParam.end();
}
for (ERaList<ModbusConfigAlias_t*>::iterator* it = b; it != e; it = it->getNext()) {
if (it->get() == nullptr) {
continue;
}
Expand Down Expand Up @@ -920,9 +947,12 @@ bool ERaModbus<Api>::actionModbus(ModbusAction_t& request) {
ERaGuardLock(this->mutex);
this->eachActionModbus(request, alias->action[i], config);
if (config == nullptr) {
continue;
ERaGuardUnlock(this->mutex);
}
else if (alias->action[i].delay) {
this->delaysMutex(alias->action[i].delay);
}
if (i != (alias->readActionCount - 1)) {
else if (i != (alias->readActionCount - 1)) {
this->delayModbus(config->addr, true);
}
else {
Expand Down
14 changes: 7 additions & 7 deletions src/Modbus/ERaModbusData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,24 +486,24 @@ void ERaModbusData::processParseConfigSensorParam(const cJSON* const root, uint8
it.setInt16();
}
else if (ERaStrCmp(transformer->valuestring, "uint_32")) {
it.setUint32().setLength(2);
it.setUint32().setMidLittleEndian().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "int_first4") ||
ERaStrCmp(transformer->valuestring, "int_last4")) {
it.setInt32().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "float_abcd")) {
it.setBigEndian().setLength(2);
it.setFloat().setBigEndian().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "float_dcba")) {
it.setLittleEndian().setLength(2);
it.setFloat().setLittleEndian().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "float_badc")) {
it.setMidBigEndian().setLength(2);
it.setFloat().setMidBigEndian().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "float_cdab") ||
ERaStrCmp(transformer->valuestring, "float_cdba")) {
it.setMidLittleEndian().setLength(2);
it.setFloat().setMidLittleEndian().setLength(2);
}
else if (ERaStrCmp(transformer->valuestring, "convert_ai")) {
it.setUint16();
Expand Down Expand Up @@ -575,7 +575,7 @@ void ERaModbusData::parseConfig(const void* ptr, bool json) {
if (!cJSON_IsNumber(version)) {
return;
}
if (version->valueint != 10) {
if (version->valueint < MODBUS_VERSION_SUPPORT_JSON) {
return;
}

Expand Down Expand Up @@ -694,7 +694,7 @@ bool ERaModbusData::handlerBytes(ERaModbusRequest* request, ERaModbusResponse* r

uint8_t expectedLen {0};
uint8_t byteIndex = (BUILD_WORD(pReg->sa1, pReg->sa2) - request->getAddress());
byteIndex = ((byteIndex + BUILD_WORD(pReg->len1, pReg->len2) - 1) * 2);
byteIndex *= 2;
uint8_t* pData = response->getData();
switch (pReg->type) {
case ModbusDataT::MODBUS_INT16:
Expand Down
3 changes: 3 additions & 0 deletions src/Modbus/ERaModbusTransp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ class ERaModbusResponse
}

void updateCRC() {
if (!this->request->isRTU()) {
return;
}
uint16_t crc = this->request->modbusCRC(this->buffer, this->length - 2);
this->buffer[this->length - 1] = HI_WORD(crc);
this->buffer[this->length - 2] = LO_WORD(crc);
Expand Down
Loading

0 comments on commit 1318de6

Please sign in to comment.