Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Function Code 08 (DIAGNOSTICS_SERIAL) #5

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions include/localmodbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

#include <WiFiManager.h>
#include <ModbusBridgeWiFi.h>
#include <ModbusClientRTU.h>
#include <Update.h>
#include "config.h"

enum SubFunctionCode : uint16_t {
RETURN_QUERY_DATA = 0x00,
RESTART_COMMUNICATION_OPTION = 0x01,
RETURN_QUERY_DATA = 0x00,
RESTART_COMMUNICATION_OPTION = 0x01,
RETURN_DIAGNOSTIC_REGISTER = 0x02,
CLEAR_COUNTERS_AND_DIAGNOSTIC_REGISTER = 0x0a,
RETURN_BUS_MESSAGE_COUNT = 0x0b,
RETURN_BUS_COMMUNICATION_ERROR_COUNT = 0x0c,
RETURN_SLAVE_MESSAGE_COUNT = 0x0e,

};

void setupLocalModbus(uint8_t serverID, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm);
void setupLocalModbus(uint8_t serverID, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm);
#endif /* LOCALMODBUS_H */
3 changes: 2 additions & 1 deletion include/pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
void sendButton(AsyncResponseStream *response, const char *title, const char *action, const char *css = "");
void sendTableRow(AsyncResponseStream *response, const char *name, uint32_t value);
void sendTableRow(AsyncResponseStream *response, const char *name, String value);
void sendDebugForm(AsyncResponseStream *response, String slaveId, String reg, String function, String count);
void sendDebugForm_read(AsyncResponseStream *response, String slaveId, String reg, String function, String count);
void sendDebugForm_diagnosticSerial(AsyncResponseStream *response, String slaveId, String subFunction, String data);
void sendMinCss(AsyncResponseStream *response);
const String ErrorName(Modbus::Error code);
const String WiFiQuality(int rssiValue);
Expand Down
48 changes: 43 additions & 5 deletions src/localmodbus.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "localmodbus.h"
#define ETAG "\"" __DATE__ "" __TIME__ "\""

static ModbusClientRTU* _rtu = NULL;
static ModbusBridgeWiFi* _bridge = NULL;


// FC03: worker do serve Modbus function code 0x03 (READ_HOLD_REGISTER)
ModbusMessage FC03(ModbusMessage request) {
uint16_t address; // requested register address
Expand Down Expand Up @@ -32,32 +36,66 @@ ModbusMessage FC03(ModbusMessage request) {
ModbusMessage FC08(ModbusMessage request) {
uint16_t subFunctionCode; // Sub-function code
ModbusMessage response; // response message to be sent back
uint16_t resultWord = 0;

LOG_D("WORKER CALLED FC08");
LOG_D("WORKER CALLED FC08\n");

// get request values
request.get(2, subFunctionCode);

switch(subFunctionCode) {
case RETURN_QUERY_DATA:
LOG_D("Return Query Data");
LOG_D("Return Query Data\n");
response = request;
break;
case RESTART_COMMUNICATION_OPTION:
LOG_D("Restart Communications Option");
LOG_D("Restart Communications Option\n");
response = request;
break;
case CLEAR_COUNTERS_AND_DIAGNOSTIC_REGISTER:
LOG_D("Restart Communications Option\n");
if(_rtu != NULL) {
_rtu->resetCounts();
}
if(_bridge != NULL) {
_bridge->resetCounts();
}
response = request;
break;
case RETURN_BUS_MESSAGE_COUNT:
LOG_D("Return Bus Message Count\n");
if(_bridge != NULL) {
resultWord = _bridge->getMessageCount();
}
response = ModbusMessage(request.getServerID(), DIAGNOSTICS_SERIAL, RETURN_BUS_MESSAGE_COUNT, resultWord);
break;
case RETURN_BUS_COMMUNICATION_ERROR_COUNT:
LOG_D("Return Bus Communication Error Count\n");
if(_bridge != NULL) {
resultWord = _bridge->getErrorCount();
}
response = ModbusMessage(request.getServerID(), DIAGNOSTICS_SERIAL, RETURN_BUS_COMMUNICATION_ERROR_COUNT, resultWord);
break;
case RETURN_SLAVE_MESSAGE_COUNT:
LOG_D("Return Slave Message Count\n");
if(_rtu != NULL) {
resultWord = _rtu->getMessageCount();
}
response = ModbusMessage(request.getServerID(), DIAGNOSTICS_SERIAL, RETURN_SLAVE_MESSAGE_COUNT, resultWord);
break;
default:
LOG_D("default: ILLEGAL_FUNCTION");
LOG_D("default: ILLEGAL_FUNCTION\n");
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_FUNCTION);
}

return response;
}

void setupLocalModbus(uint8_t serverID, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm) {
void setupLocalModbus(uint8_t serverID, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm) {
String EfuseMac = String(ESP.getEfuseMac(), 16);
dbgln(EfuseMac);
_rtu = rtu;
_bridge = bridge;
bridge->registerWorker(serverID, READ_HOLD_REGISTER, &FC03);
bridge->registerWorker(serverID, DIAGNOSTICS_SERIAL, &FC08);
//bridge->registerWorker(serverID, REPORT_SERVER_ID_SERIAL, &FC08);
Expand Down
22 changes: 9 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,16 @@ boolean WiFiConnect() {
// Callback for reconnecting
void WiFiLostIP(WiFiEvent_t event, WiFiEventInfo_t info) {
dbgln("[WiFi] (possibly) disconnected");
wm.disconnect();
dbgln("[WiFi] trying to reconnect");
MDNS.end();

if(WiFi.status() != WL_CONNECTED) {
dbgln("[WiFi] trying to reconnect");
MDNS.end();

isConnected = false;
isConnected = WiFiConnect();
isConnected = false;
isConnected = WiFiConnect();

// Start mDNS
if(isConnected) {
startMDNS(80, config.getTcpPort());
}
} else {
dbgln("[WiFi] actually is connected");
// Start mDNS
if(isConnected) {
startMDNS(80, config.getTcpPort());
}
}

Expand Down Expand Up @@ -113,7 +109,7 @@ void setup() {

// register worker for local Modbus function
if(skipAddress) {
setupLocalModbus(config.getLocalModbusAddress(), &MBbridge, &config, &wm);
setupLocalModbus(config.getLocalModbusAddress(), MBclient, &MBbridge, &config, &wm);
}

// Start Modbus Bridge
Expand Down
Loading
Loading