Skip to content
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
341 changes: 116 additions & 225 deletions ModbusRtu.h

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions examples/RS485_slave/RS485_slave.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ uint16_t au16data[16] = {
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1,0,TXEN); // this is slave @1 and RS-485
Modbus slave(1,Serial,TXEN); // this is slave @1 and RS-485

void setup() {
slave.begin( 19200 ); // baud-rate at 19200
Serial.begin( 19200 ); // baud-rate at 19200
slave.start();
}

void loop() {
Expand Down
9 changes: 5 additions & 4 deletions examples/advanced_master/advanced_master.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ uint8_t u8query; //!< pointer to message query
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI

/**
* This is an structe which contains a query to an slave device
Expand All @@ -50,8 +50,9 @@ void setup() {
telegram[1].u16RegAdd = 4; // start address in slave
telegram[1].u16CoilsNo = 1; // number of elements (coils or registers) to read
telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino

master.begin( 19200 ); // baud-rate at 19200

Serial.begin( 19200 ); // baud-rate at 19200
master.start();
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
u32wait = millis() + 1000;
u8state = u8query = 0;
Expand Down
5 changes: 3 additions & 2 deletions examples/advanced_slave/advanced_slave.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define ID 1

//Crear instancia
Modbus slave(ID, 0, 0); //ID del nodo. 0 para el master, 1-247 para esclavo
Modbus slave(ID, Serial, 0); //ID del nodo. 0 para el master, 1-247 para esclavo
//Puerto serie (0 = TX: 1 - RX: 0)
//Protocolo serie. 0 para RS-232 + USB (default), cualquier pin mayor a 1 para RS-485
boolean led;
Expand All @@ -29,7 +29,8 @@ uint16_t au16data[9]; //La tabla de registros que se desea compartir por la red
void setup() {
io_setup(); //configura las entradas y salidas

slave.begin(19200); //Abre la comunicación como esclavo
Serial.begin(19200); //Abre la comunicación como esclavo
slave.start();
tempus = millis() + 100; //Guarda el tiempo actual + 100ms
digitalWrite(13, HIGH ); //Prende el led del pin 13 (el de la placa)
}
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_master/simple_master.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ uint8_t u8state;
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI

/**
* This is an structe which contains a query to an slave device
Expand All @@ -37,7 +37,8 @@ modbus_t telegram;
unsigned long u32wait;

void setup() {
master.begin( 19200 ); // baud-rate at 19200
Serial.begin( 19200 ); // baud-rate at 19200
master.start();
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_slave/simple_slave.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ uint16_t au16data[16] = {
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
slave.begin( 19200 ); // baud-rate at 19200
Serial.begin( 19200 ); // baud-rate at 19200
slave.start();
}

void loop() {
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_slave2/simple_slave2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ uint16_t au16data[16] = {
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
slave.begin( 19200, SERIAL_8E1 ); // 19200 baud, 8-bits, even, 1-bit stop
Serial.begin( 19200, SERIAL_8E1 ); // 19200 baud, 8-bits, even, 1-bit stop
slave.start();
}

void loop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@
uint16_t au16data[16];
uint8_t u8state;

SoftwareSerial mySerial(3, 5);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.

/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial
Modbus master(0, mySerial); // this is master and RS-232 or USB-FTDI via software serial

/**
* This is an structe which contains a query to an slave device
Expand All @@ -64,11 +66,9 @@ modbus_t telegram;

unsigned long u32wait;

SoftwareSerial mySerial(3, 5);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.

void setup() {
Serial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
master.begin( &mySerial, 9600 ); // begin the ModBus object. The first parameter is the address of your SoftwareSerial address. Do not forget the "&". 9600 means baud-rate at 9600
mySerial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
master.start(); // start the ModBus object.
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
Expand Down
Loading