Skip to content

Commit

Permalink
fixed examples in accord with the new library structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocketct committed Nov 7, 2024
1 parent c5e9eef commit 0b698ce
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 561 deletions.
124 changes: 18 additions & 106 deletions examples/T1S/ModbusT1SClient/ModbusT1SClient.ino
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,143 +9,55 @@
- Uno WiFi R4
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#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 {
Serial.print("Coil value: ");
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)
Expand Down
9 changes: 0 additions & 9 deletions examples/T1S/ModbusT1SClient/arduino_secrets.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,133 +9,46 @@
- all the terminations placed on the hardware
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#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 {
Expand All @@ -162,4 +75,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus)
Serial.println("CSMA/CD fallback");
tc6_inst->enablePlca();
}
}
}

This file was deleted.

Loading

0 comments on commit 0b698ce

Please sign in to comment.