diff --git a/examples/T1S/ModbusT1SClient/ModbusT1SClient.ino b/examples/T1S/ModbusT1SClient/ModbusT1SClient.ino index 94693e5..7fe6c3f 100644 --- a/examples/T1S/ModbusT1SClient/ModbusT1SClient.ino +++ b/examples/T1S/ModbusT1SClient/ModbusT1SClient.ino @@ -1,5 +1,5 @@ /* - Modbus T1S Client Toggle + Modbus T1S Client This sketch demonstrates how to send commands to a Modbus T1S server connected via T1S Single Pair Ethernet. @@ -9,121 +9,34 @@ - Uno WiFi R4 */ -#include // ArduinoModbus depends on the ArduinoRS485 library +#include #include -#include "arduino_secrets.h" -/************************************************************************************** - CONSTANTS - **************************************************************************************/ -static uint8_t const T1S_PLCA_NODE_ID = 2; - -static IPAddress const ip_addr { - 192, 168, 42, 100 + T1S_PLCA_NODE_ID -}; -static IPAddress const network_mask { - 255, 255, 255, 0 -}; -static IPAddress const gateway { - 192, 168, 42, 100 -}; - -static T1SPlcaSettings const t1s_plca_settings { - T1S_PLCA_NODE_ID -}; -static T1SMacSettings const t1s_default_mac_settings; - -static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0}; -/************************************************************************************** - GLOBAL VARIABLES - **************************************************************************************/ -auto const tc6_io = new TC6::TC6_Io -( SPI - , CS_PIN - , RESET_PIN - , IRQ_PIN); -auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io); Arduino_10BASE_T1S_UDP udp_client; - +static uint8_t const T1S_PLCA_NODE_ID = 2; +static uint16_t const UDP_SERVER_PORT = 8889; +static uint16_t const UDP_CLIENT_PORT = 8888; +#define MODBUS_ID 42 void setup() { - Serial.begin(115200); - while (!Serial); - - Serial.println("Modbus T1S Client Toggle"); + Serial.begin(115200); - /* Initialize digital IO interface for interfacing - with the LAN8651. - */ - pinMode(IRQ_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), - []() { - tc6_io->onInterrupt(); - }, - FALLING); - - /* Initialize IO module. */ - if (!tc6_io->begin()) - { - Serial.println("'tc6_io::begin(...)' failed."); - for (;;) { } - } - - MacAddress const mac_addr = MacAddress::create_from_uid(); - - if (!tc6_inst->begin(ip_addr - , network_mask - , gateway - , mac_addr - , t1s_plca_settings - , t1s_default_mac_settings)) - { - Serial.println("'TC6::begin(...)' failed."); - for (;;) { } - } - - Serial.print("IP\t"); - Serial.println(ip_addr); - Serial.println(mac_addr); - Serial.println(t1s_plca_settings); - Serial.println(t1s_default_mac_settings); - - if (!udp_client.begin(UDP_CLIENT_PORT)) - { - Serial.println("begin(...) failed for UDP client"); - for (;;) { } - } - - /* A0 -> LOCAL_ENABLE -> DO NOT feed power from board to network. */ - tc6_inst->digitalWrite(TC6::DIO::A0, false); - /* A1 -> T1S_DISABLE -> Open the switch connecting network to board by pulling EN LOW. */ - tc6_inst->digitalWrite(TC6::DIO::A1, false); - - ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR); + ModbusT1SClient.setT1SClient(&udp_client); + ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT); ModbusT1SClient.setServerPort(UDP_SERVER_PORT); ModbusT1SClient.setModbusId(MODBUS_ID); + ModbusT1SClient.setCallback(OnPlcaStatus); - Serial.println("UDP_Client"); + if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) { + Serial.println("Failed to start Modbus T1S Client!"); + while (1); + } } void loop() { - tc6_inst->service(); - - static unsigned long prev_beacon_check = 0; - static unsigned long prev_udp_packet_sent = 0; - - auto const now = millis(); - - if ((now - prev_beacon_check) > 1000) - { - prev_beacon_check = now; - if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) { - Serial.println("getPlcaStatus(...) failed"); - } - } - // for (slave) id 1: write the value of 0x01, to the coil at address 0x00 - int res = ModbusT1SClient.coilRead(0x00, &udp_client, UDP_READ_COIL_PORT); + ModbusT1SClient.checkPLCAStatus(); + int res = ModbusT1SClient.coilRead(0x00); if (res == -1) { Serial.println("Failed to read coil! "); } else { @@ -131,21 +44,20 @@ void loop() { Serial.println(res); } - res = ModbusT1SClient.coilWrite(0x00, 1, &udp_client, UDP_WRITE_COIL_PORT); + res = ModbusT1SClient.coilWrite(0, 0x01); if (res == -1) { Serial.println("Failed to write coil! "); } else { Serial.println("write done"); } - res = ModbusT1SClient.inputRegisterRead(0x00, &udp_client, UDP_READ_IR_PORT); + res = ModbusT1SClient.inputRegisterRead(0x00); if (res == -1) { Serial.println("Failed to read Input Register! "); } else { Serial.print("Input Register value: "); Serial.println(res); } - } static void OnPlcaStatus(bool success, bool plcaStatus) diff --git a/examples/T1S/ModbusT1SClient/arduino_secrets.h b/examples/T1S/ModbusT1SClient/arduino_secrets.h deleted file mode 100644 index c134b4e..0000000 --- a/examples/T1S/ModbusT1SClient/arduino_secrets.h +++ /dev/null @@ -1,9 +0,0 @@ -static uint16_t const UDP_CLIENT_PORT = 8888; -static uint16_t const UDP_SERVER_PORT = 8889; -#define UDP_READ_COIL_PORT 1 -#define UDP_WRITE_COIL_PORT 2 -#define UDP_READ_DI_PORT 3 -#define UDP_READ_IR_PORT 4 -#define UDP_READ_HR_PORT 5 -#define UDP_WRITE_HR_PORT 6 -#define MODBUS_ID 42 \ No newline at end of file diff --git a/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino b/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino index 793a319..8d14f3a 100644 --- a/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino +++ b/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino @@ -9,133 +9,46 @@ - all the terminations placed on the hardware */ -#include // ArduinoModbus depends on the ArduinoRS485 library +#include #include -#include "arduino_secrets.h" -/************************************************************************************** - CONSTANTS - **************************************************************************************/ -static uint8_t const T1S_PLCA_NODE_ID = 2; - -static IPAddress const ip_addr { - 192, 168, 42, 100 + T1S_PLCA_NODE_ID -}; -static IPAddress const network_mask { - 255, 255, 255, 0 -}; -static IPAddress const gateway { - 192, 168, 42, 100 -}; -static T1SPlcaSettings const t1s_plca_settings { - T1S_PLCA_NODE_ID -}; -static T1SMacSettings const t1s_default_mac_settings; - -static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0}; +static uint8_t const T1S_PLCA_NODE_ID = 2; +static uint16_t const UDP_SERVER_PORT = 8889; +static uint16_t const UDP_CLIENT_PORT = 8888; -/************************************************************************************** - GLOBAL VARIABLES - **************************************************************************************/ -auto const tc6_io = new TC6::TC6_Io -( SPI - , CS_PIN - , RESET_PIN - , IRQ_PIN); -auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io); Arduino_10BASE_T1S_UDP udp_client; - void setup() { Serial.begin(115200); - while (!Serial); - - Serial.println("Modbus T1S Client Toggle"); - - /* Initialize digital IO interface for interfacing - with the LAN8651. - */ - pinMode(IRQ_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), - []() { - tc6_io->onInterrupt(); - }, - FALLING); - - /* Initialize IO module. */ - if (!tc6_io->begin()) - { - Serial.println("'tc6_io::begin(...)' failed."); - for (;;) { } - } - - MacAddress const mac_addr = MacAddress::create_from_uid(); - - if (!tc6_inst->begin(ip_addr - , network_mask - , gateway - , mac_addr - , t1s_plca_settings - , t1s_default_mac_settings)) - { - Serial.println("'TC6::begin(...)' failed."); - for (;;) { } - } - - Serial.print("IP\t"); - Serial.println(ip_addr); - Serial.println(mac_addr); - Serial.println(t1s_plca_settings); - Serial.println(t1s_default_mac_settings); - if (!udp_client.begin(UDP_CLIENT_PORT)) - { - Serial.println("begin(...) failed for UDP client"); - for (;;) { } - } - - /* A0 -> LOCAL_ENABLE -> DO NOT feed power from board to network. */ - tc6_inst->digitalWrite(TC6::DIO::A0, false); - /* A1 -> T1S_DISABLE -> Open the switch connecting network to board by pulling EN LOW. */ - tc6_inst->digitalWrite(TC6::DIO::A1, true); - - ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR); + ModbusT1SClient.setT1SClient(&udp_client); + ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT); ModbusT1SClient.setServerPort(UDP_SERVER_PORT); + ModbusT1SClient.setCallback(OnPlcaStatus); - - Serial.println("UDP_Client"); + if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) { + Serial.println("Failed to start Modbus T1S Client!"); + while (1); + } } unsigned long start = 0; void loop() { - tc6_inst->service(); - - static unsigned long prev_beacon_check = 0; - static unsigned long prev_udp_packet_sent = 0; - - auto const now = millis(); - - if ((now - prev_beacon_check) > 1000) - { - prev_beacon_check = now; - if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) { - Serial.println("getPlcaStatus(...) failed"); - } - } + ModbusT1SClient.checkPLCAStatus(); if ((millis() - start) > 1000) { - int res = ModbusT1SClient.inputRegisterRead(1, 0x01, &udp_client, UDP_READ_IR_PORT); + int res = ModbusT1SClient.inputRegisterRead(1, 0x01); if (res == -1) { Serial.println("Failed to read temperature! "); } else { - int16_t const temperature_raw =res; + int16_t const temperature_raw = res; float const temperature_deg = temperature_raw / 10.f; Serial.print("Temperature: "); Serial.println(temperature_deg); } - - res = ModbusT1SClient.inputRegisterRead(1, 0x02, &udp_client, UDP_READ_IR_PORT); + + res = ModbusT1SClient.inputRegisterRead(1, 0x02); if (res == -1) { Serial.println("Failed to read humidity! "); } else { @@ -162,4 +75,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus) Serial.println("CSMA/CD fallback"); tc6_inst->enablePlca(); } -} \ No newline at end of file +} diff --git a/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/arduino_secrets.h b/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/arduino_secrets.h deleted file mode 100644 index b6af131..0000000 --- a/examples/T1S/ModbusT1SClientTemperatureHumiditySensor/arduino_secrets.h +++ /dev/null @@ -1,8 +0,0 @@ -static uint16_t const UDP_CLIENT_PORT = 8888; -static uint16_t const UDP_SERVER_PORT = 8889; -#define UDP_READ_COIL_PORT 1 -#define UDP_WRITE_COIL_PORT 2 -#define UDP_READ_DI_PORT 3 -#define UDP_READ_IR_PORT 4 -#define UDP_READ_HR_PORT 5 -#define UDP_WRITE_HR_PORT 6 \ No newline at end of file diff --git a/examples/T1S/ModbusT1SServer/ModbusT1SServer.ino b/examples/T1S/ModbusT1SServer/ModbusT1SServer.ino index 3c001b6..1d156fd 100644 --- a/examples/T1S/ModbusT1SServer/ModbusT1SServer.ino +++ b/examples/T1S/ModbusT1SServer/ModbusT1SServer.ino @@ -1,5 +1,5 @@ /* - Modbus T1S Server LED + Modbus T1S Server This sketch demonstrates how to receive commands from a Modbus T1S Client connected @@ -10,176 +10,30 @@ - Uno WiFi R4 */ -#include // ArduinoModbus depends on the ArduinoRS485 library +#include #include -#include "arduino_secrets.h" -/************************************************************************************** - CONSTANTS - **************************************************************************************/ -#define RS485_SERIAL Serial1 -#define RS485_TX_PIN 1 -#define RS485_RX_PIN 0 -#define RS485_DE_PIN 8 -#define RS485_RE_PIN 7 -#define PRE_DELAY 100 -#define POST_DELAY 100 -#define PWR_CARRIER_RATIO 0.18f -RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN); +static uint8_t const T1S_PLCA_NODE_ID = 0; +static uint16_t const UDP_SERVER_PORT = 8889; -static unsigned int const MODBUS_BAUDRATE = 9600; -static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE; -static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6; -static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6; - -static int const MODBUS_DEVICE_ID = 1; -static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001; -static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002; - -static uint8_t const T1S_PLCA_NODE_ID = 0; /* The UDP server doubles as PLCA coordinator. */ - -static IPAddress const ip_addr { - 192, 168, 42, 100 + T1S_PLCA_NODE_ID -}; -static IPAddress const network_mask { - 255, 255, 255, 0 -}; -static IPAddress const gateway { - 192, 168, 42, 100 -}; - -static T1SPlcaSettings const t1s_plca_settings { - T1S_PLCA_NODE_ID -}; -static T1SMacSettings const t1s_default_mac_settings; - -/************************************************************************************** - GLOBAL VARIABLES - **************************************************************************************/ -auto const tc6_io = new TC6::TC6_Io -( SPI - , CS_PIN - , RESET_PIN - , IRQ_PIN); -auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io); Arduino_10BASE_T1S_UDP udp_server; -/************************************************************************************** - SETUP/LOOP - **************************************************************************************/ void setup() { Serial.begin(115200); - Serial.println("Modbus RTU Server LED"); - - /* Initialize digital IO interface for interfacing - with the LAN8651. - */ - pinMode(IRQ_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), - []() { - tc6_io->onInterrupt(); - }, - FALLING); - - /* Initialize IO module. */ - if (!tc6_io->begin()) - { - Serial.println("'TC6_Io::begin(...)' failed."); - for (;;) { } - } - - MacAddress const mac_addr = MacAddress::create_from_uid(); - - if (!tc6_inst->begin(ip_addr - , network_mask - , gateway - , mac_addr - , t1s_plca_settings - , t1s_default_mac_settings)) - { - Serial.println("'TC6::begin(...)' failed."); - for (;;) { } - } - - Serial.print("IP\t"); - Serial.println(ip_addr); - Serial.println(mac_addr); - Serial.println(t1s_plca_settings); - Serial.println(t1s_default_mac_settings); - - if (!udp_server.begin(UDP_SERVER_PORT)) - { - Serial.println("begin(...) failed for UDP coil read server "); - for (;;) { } - } - - tc6_inst->digitalWrite(TC6::DIO::A0, true); - /* A1 -> T1S_DISABLE -> close the switch connecting network to board. */ - tc6_inst->digitalWrite(TC6::DIO::A1, true); + ModbusT1SServer.setT1SServer(&udp_server); + ModbusT1SServer.setT1SPort(UDP_SERVER_PORT); + ModbusT1SServer.setBadrate(9600); + ModbusT1SServer.setCallback(OnPlcaStatus); - serial485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR); - if (!ModbusT1SServer.begin(serial485, MODBUS_BAUDRATE, SERIAL_8N1)) { - Serial.println("Failed to start Modbus RTU Client!"); + if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) { + Serial.println("Failed to start Modbus T1S Server!"); while (1); } - - ModbusT1SServer.setT1SServer(&udp_server); - Serial.println("UDP_Server"); } void loop() { - /* Services the hardware and the protocol stack. - Must be called cyclic. The faster the better. - */ - tc6_inst->service(); - - static unsigned long prev_beacon_check = 0; - - auto const now = millis(); - if ((now - prev_beacon_check) > 1000) - { - prev_beacon_check = now; - if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) { - Serial.println("getPlcaStatus(...) failed"); - } - } - - switch (ModbusT1SServer.parsePacket()) - { - case UDP_READ_COIL_PORT: - Serial.println("Read coil"); - ModbusT1SServer.coilRead(); - break; - - case UDP_WRITE_COIL_PORT: - Serial.println("Write coil"); - ModbusT1SServer.coilWrite(); - break; - - case UDP_READ_DI_PORT: - Serial.println("Read discrete input"); - ModbusT1SServer.discreteInputRead(); - break; - - case UDP_READ_IR_PORT: - Serial.println("Read input register"); - ModbusT1SServer.inputRegisterRead(); - break; - - case UDP_READ_HR_PORT: - Serial.println("Read holding register"); - ModbusT1SServer.holdingRegisterRead(); - break; - - case UDP_WRITE_HR_PORT: - Serial.println("Write holding register"); - ModbusT1SServer.holdingRegisterWrite(); - break; - - default: - break; - } + ModbusT1SServer.update(); } static void OnPlcaStatus(bool success, bool plcaStatus) @@ -196,4 +50,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus) Serial.println("CSMA/CD fallback"); tc6_inst->enablePlca(); } -} \ No newline at end of file +} diff --git a/examples/T1S/ModbusT1SServer/arduino_secrets.h b/examples/T1S/ModbusT1SServer/arduino_secrets.h deleted file mode 100644 index 20e6fec..0000000 --- a/examples/T1S/ModbusT1SServer/arduino_secrets.h +++ /dev/null @@ -1,7 +0,0 @@ -static uint16_t const UDP_SERVER_PORT = 8889; -#define UDP_READ_COIL_PORT 1 -#define UDP_WRITE_COIL_PORT 2 -#define UDP_READ_DI_PORT 3 -#define UDP_READ_IR_PORT 4 -#define UDP_READ_HR_PORT 5 -#define UDP_WRITE_HR_PORT 6 diff --git a/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino b/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino index eb4d711..226f59e 100644 --- a/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino +++ b/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino @@ -15,180 +15,29 @@ - all the terminations placed on the hardware */ -#include // ArduinoModbus depends on the ArduinoRS485 library +#include #include -#include "arduino_secrets.h" -/************************************************************************************** - CONSTANTS - **************************************************************************************/ -#define RS485_SERIAL Serial1 -#define RS485_TX_PIN 1 -#define RS485_RX_PIN 0 -#define RS485_DE_PIN 8 -#define RS485_RE_PIN 7 -#define PRE_DELAY 100 -#define POST_DELAY 100 -#define PWR_CARRIER_RATIO 0.18f -RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN); +static uint8_t const T1S_PLCA_NODE_ID = 0; +static uint16_t const UDP_SERVER_PORT = 8889; -static unsigned int const MODBUS_BAUDRATE = 9600; -static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE; -static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6; -static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6; - -static int const MODBUS_DEVICE_ID = 1; -static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001; -static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002; - -static uint8_t const T1S_PLCA_NODE_ID = 0; /* The UDP server doubles as PLCA coordinator. */ - -static IPAddress const ip_addr { - 192, 168, 42, 100 + T1S_PLCA_NODE_ID -}; -static IPAddress const network_mask { - 255, 255, 255, 0 -}; -static IPAddress const gateway { - 192, 168, 42, 100 -}; - -static T1SPlcaSettings const t1s_plca_settings { - T1S_PLCA_NODE_ID -}; -static T1SMacSettings const t1s_default_mac_settings; - -/************************************************************************************** - GLOBAL VARIABLES - **************************************************************************************/ -auto const tc6_io = new TC6::TC6_Io -( SPI - , CS_PIN - , RESET_PIN - , IRQ_PIN); -auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io); Arduino_10BASE_T1S_UDP udp_server; -/************************************************************************************** - SETUP/LOOP - **************************************************************************************/ void setup() { Serial.begin(115200); - Serial.println("Modbus RTU Server LED"); - - /* Initialize digital IO interface for interfacing - with the LAN8651. - */ - pinMode(IRQ_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), - []() { - tc6_io->onInterrupt(); - }, - FALLING); - - /* Initialize IO module. */ - if (!tc6_io->begin()) - { - Serial.println("'TC6_Io::begin(...)' failed."); - for (;;) { } - } - - MacAddress const mac_addr = MacAddress::create_from_uid(); - - if (!tc6_inst->begin(ip_addr - , network_mask - , gateway - , mac_addr - , t1s_plca_settings - , t1s_default_mac_settings)) - { - Serial.println("'TC6::begin(...)' failed."); - for (;;) { } - } - - Serial.print("IP\t"); - Serial.println(ip_addr); - Serial.println(mac_addr); - Serial.println(t1s_plca_settings); - Serial.println(t1s_default_mac_settings); - - if (!udp_server.begin(UDP_SERVER_PORT)) - { - Serial.println("begin(...) failed for UDP coil read server "); - for (;;) { } - } - - tc6_inst->digitalWrite(TC6::DIO::A0, false); - /* A1 -> T1S_DISABLE -> close the switch connecting network to board. */ - tc6_inst->digitalWrite(TC6::DIO::A1, true); - - serial485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR); - unsigned long br = MODBUS_BAUDRATE; - uint16_t config = SERIAL_8N1; - if (!ModbusT1SServer.begin(serial485, br, config)) { - Serial.println("Failed to start Modbus RTU Client!"); + ModbusT1SServer.setT1SServer(&udp_server); + ModbusT1SServer.setT1SPort(UDP_SERVER_PORT); + ModbusT1SServer.setBadrate(9600); + ModbusT1SServer.setCallback(OnPlcaStatus); + if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) { + Serial.println("Failed to start Modbus T1S Server!"); while (1); } - - ModbusT1SServer.setT1SServer(&udp_server); - Serial.println("UDP_Server"); - Serial.println(MODBUS_PRE_DELAY_BR); - Serial.println(MODBUS_POST_DELAY_BR); } void loop() { - /* Services the hardware and the protocol stack. - Must be called cyclic. The faster the better. - */ - tc6_inst->service(); - - static unsigned long prev_beacon_check = 0; - - auto const now = millis(); - if ((now - prev_beacon_check) > 1000) - { - prev_beacon_check = now; - if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) { - Serial.println("getPlcaStatus(...) failed"); - } - } - - switch (ModbusT1SServer.parsePacket()) - { - case UDP_READ_COIL_PORT: - Serial.println("Read coil"); - ModbusT1SServer.coilRead(); - break; - - case UDP_WRITE_COIL_PORT: - Serial.println("Write coil"); - ModbusT1SServer.coilWrite(); - break; - - case UDP_READ_DI_PORT: - Serial.println("Read discrete input"); - ModbusT1SServer.discreteInputRead(); - break; - - case UDP_READ_IR_PORT: - Serial.println("Read input register"); - ModbusT1SServer.inputRegisterRead(); - break; - - case UDP_READ_HR_PORT: - Serial.println("Read holding register"); - ModbusT1SServer.holdingRegisterRead(); - break; - - case UDP_WRITE_HR_PORT: - Serial.println("Write holding register"); - ModbusT1SServer.holdingRegisterWrite(); - break; - - default: - break; - } + ModbusT1SServer.update(); } static void OnPlcaStatus(bool success, bool plcaStatus) @@ -205,4 +54,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus) Serial.println("CSMA/CD fallback"); tc6_inst->enablePlca(); } -} \ No newline at end of file +} diff --git a/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/arduino_secrets.h b/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/arduino_secrets.h deleted file mode 100644 index 47405ff..0000000 --- a/examples/T1S/ModbusT1SServerTemperatureHumiditySensor/arduino_secrets.h +++ /dev/null @@ -1,7 +0,0 @@ -static uint16_t const UDP_SERVER_PORT = 8889; -#define UDP_READ_COIL_PORT 1 -#define UDP_WRITE_COIL_PORT 2 -#define UDP_READ_DI_PORT 3 -#define UDP_READ_IR_PORT 4 -#define UDP_READ_HR_PORT 5 -#define UDP_WRITE_HR_PORT 6 \ No newline at end of file