Skip to content

Modbus T1S support #157

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
# Install the ArduinoModbus library from the local path
- source-path: ./
- name: ArduinoRS485
- name: Arduino_10BASE_T1S
UNIVERSAL_SKETCH_PATHS: |
- examples/RTU

Expand All @@ -42,24 +43,38 @@ jobs:
- fqbn: arduino:megaavr:uno2018:mode=off
ethernet: true
nina: true
spe: false
artifact-name-suffix: arduino-megaavr-uno2018
- fqbn: arduino:samd:mkrwifi1010
ethernet: true
nina: true
spe: false
artifact-name-suffix: arduino-samd-mkrwifi1010
- fqbn: arduino:mbed_nano:nano33ble
ethernet: false
nina: false
spe: false
artifact-name-suffix: arduino-mbed_nano-nano33ble
- fqbn: arduino:mbed_portenta:envie_m7
ethernet: false
nina: false
spe: false
artifact-name-suffix: arduino-mbed_portenta-envie_m7
- fqbn: arduino:mbed_opta:opta
ethernet: true
nina: false
spe: false
artifact-name-suffix: arduino-mbed_opta-opta

- fqbn: arduino:renesas_uno:unor4wifi
ethernet: false
nina: false
spe: true
artifact-name-suffix: arduino-renesas_uno-unor4wifi
- fqbn: arduino:renesas_uno:minima
ethernet: false
nina: false
spe: true
artifact-name-suffix: arduino-renesas_uno-minima
# Make board type-specific customizations to the matrix jobs
include:
- board:
Expand Down Expand Up @@ -90,6 +105,21 @@ jobs:
nina: false
nina-libraries: ""
nina-sketch-paths: ""
- board:
# Boards with T1S shield
spe: true
# Install these libraries in addition to the ones defined by env.UNIVERSAL_LIBRARIES
spe-libraries: ""
# Compile these sketches in addition to the ones defined by env.UNIVERSAL_SKETCH_PATHS
nina-sketch-paths: |
- examples/T1S
- board:
# Boards with T1S shield
spe: false
# Install these libraries in addition to the ones defined by env.UNIVERSAL_LIBRARIES
spe-libraries: ""
# Compile these sketches in addition to the ones defined by env.UNIVERSAL_SKETCH_PATHS
nina-sketch-paths: ""

steps:
- name: Checkout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void loop()
if (ModbusRTUClient.available())
{
int16_t const temperature_raw = ModbusRTUClient.read();
float const temperature_deg = temperature_raw / 100.f;
float const temperature_deg = temperature_raw / 10.f;
Serial.println(temperature_deg);
}

Expand All @@ -68,7 +68,7 @@ void loop()
if (ModbusRTUClient.available())
{
int16_t const humidity_raw = ModbusRTUClient.read();
float const humidity_per_cent = humidity_raw / 100.f;
float const humidity_per_cent = humidity_raw / 10.f;
Serial.println(humidity_per_cent);
}

Expand Down
77 changes: 77 additions & 0 deletions examples/T1S/ModbusT1SClient/ModbusT1SClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Modbus T1S Client

This sketch demonstrates how to send commands to a Modbus T1S server connected
via T1S Single Pair Ethernet.

Circuit:
- T1S shield
- Uno WiFi R4
*/

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

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);

ModbusT1SClient.setT1SClient(udp_client);
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
ModbusT1SClient.setModbusId(MODBUS_ID);
ModbusT1SClient.setCallback(OnPlcaStatus);

if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
Serial.println("Failed to start Modbus T1S Client!");
while (1);
}
}

void loop() {
ModbusT1SClient.update();

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(0, 0x01);
if (res == -1) {
Serial.println("Failed to write coil! ");
} else {
Serial.println("write done");
}

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)
{
if (!success)
{
Serial.println("PLCA status register read failed");
return;
}

if (plcaStatus) {
Serial.println("PLCA Mode active");
} else {
Serial.println("CSMA/CD fallback");
tc6_inst->enablePlca();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Modbus T1S Client Temperature Humidity sensor

This sketch creates a Modbus T1S Client and demonstrates
how to use the ModbusT1S API to communicate.
- Arduino Uno Wifi R4
- T1S shield
- SPE ethernet connected to the client
- all the terminations placed on the hardware
*/

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

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;

Arduino_10BASE_T1S_UDP udp_client;

void setup() {
Serial.begin(115200);

ModbusT1SClient.setT1SClient(udp_client);
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
ModbusT1SClient.setCallback(OnPlcaStatus);

if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
Serial.println("Failed to start Modbus T1S Client!");
while (1);
}
ModbusT1SClient.disablePOE();
}

unsigned long start = 0;
void loop() {
ModbusT1SClient.update();

if ((millis() - start) > 1000)
{
int res = ModbusT1SClient.inputRegisterRead(1, 0x01);
if (res == -1) {
Serial.println("Failed to read temperature! ");
} else {
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);
if (res == -1) {
Serial.println("Failed to read humidity! ");
} else {
int16_t const humidity_raw = res;
float const humidity_per_cent = humidity_raw / 10.f;
Serial.print("Humidity: ");
Serial.println(humidity_per_cent);
}
start = millis();
}
}

static void OnPlcaStatus(bool success, bool plcaStatus)
{
if (!success)
{
Serial.println("PLCA status register read failed");
return;
}

if (plcaStatus) {
Serial.println("PLCA Mode active");
} else {
Serial.println("CSMA/CD fallback");
tc6_inst->enablePlca();
}
}
55 changes: 55 additions & 0 deletions examples/T1S/ModbusT1SServer/ModbusT1SServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Modbus T1S Server


This sketch demonstrates how to receive commands from a Modbus T1S Client connected
via T1S Single Pair Ethernet.

Circuit:
- T1S shield
- Uno WiFi R4
*/

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

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;

Arduino_10BASE_T1S_UDP udp_server;

void setup() {
Serial.begin(115200);

ModbusT1SServer.setT1SServer(udp_server);
ModbusT1SServer.setT1SPort(UDP_SERVER_PORT);
ModbusT1SServer.setCallback(OnPlcaStatus);

if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID, 9600, SERIAL_8N1, serial485)) {
Serial.println("Failed to start Modbus T1S Server!");
while (1);
}
ModbusT1SServer.disablePOE();
}

void loop() {
ModbusT1SServer.update();
}

static void OnPlcaStatus(bool success, bool plcaStatus)
{
if (!success)
{
Serial.println("PLCA status register read failed");
return;
}

if (plcaStatus) {
Serial.println("PLCA Mode active");
} else {
Serial.println("CSMA/CD fallback");
tc6_inst->enablePlca();
}
}
17 changes: 17 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ModbusRTUClient KEYWORD1
ModbusRTUServer KEYWORD1
ModbusRTUClient KEYWORD1
ModbusTCPServer KEYWORD1
ModbusT1SClient KEYWORD1
ModbusT1SServer KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -44,6 +46,21 @@ configureInputRegisters KEYWORD2
discreteInputWrite KEYWORD2
inputRegisterWrite KEYWORD2

setServerIp KEYWORD2
setServerPort KEYWORD2
setModbusId KEYWORD2
setT1SServer KEYWORD2
setT1SClient KEYWORD2
setGateway KEYWORD2
setRxTimeout KEYWORD2
setT1SPort KEYWORD2
update KEYWORD2
setCallback KEYWORD2
enablePOE KEYWORD2
disablePOE KEYWORD2
checkStatus KEYWORD2
parsePacket KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ version=1.0.9
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Use Modbus equipment with your Arduino.
paragraph=Using TCP or RS485 shields, like the MKR 485 Shield. This library depends on the ArduinoRS485 library.
paragraph=Using TCP, RS485 or T1S shields, like the MKR 485 Shield. This library depends on the ArduinoRS485 library.
category=Communication
url=https://www.arduino.cc/en/ArduinoModbus/ArduinoModbus
architectures=megaavr,samd,mbed_nano,mbed_portenta,mbed_opta
architectures=megaavr,samd,mbed_nano,mbed_portenta,mbed_opta,renesas_uno
includes=ArduinoModbus.h
depends=ArduinoRS485
4 changes: 4 additions & 0 deletions src/ArduinoModbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
#include "ModbusTCPClient.h"
#include "ModbusTCPServer.h"

#ifndef __AVR__
#include "ModbusT1SClient.h"
#include "ModbusT1SServer.h"
#endif
#endif
Loading
Loading