Skip to content

Commit df2fc9b

Browse files
authored
Merge pull request #45 from alextingle/conditional_software_serial
Conditional software serial
2 parents a0989f7 + d98f14f commit df2fc9b

File tree

8 files changed

+142
-247
lines changed

8 files changed

+142
-247
lines changed

ModbusRtu.h

Lines changed: 112 additions & 223 deletions
Large diffs are not rendered by default.

examples/RS485_slave/RS485_slave.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ uint16_t au16data[16] = {
1919
/**
2020
* Modbus object declaration
2121
* u8id : node id = 0 for master, = 1..247 for slave
22-
* u8serno : serial port (use 0 for Serial)
22+
* port : serial port
2323
* u8txenpin : 0 for RS-232 and USB-FTDI
2424
* or any pin number > 1 for RS-485
2525
*/
26-
Modbus slave(1,0,TXEN); // this is slave @1 and RS-485
26+
Modbus slave(1,Serial,TXEN); // this is slave @1 and RS-485
2727

2828
void setup() {
29-
slave.begin( 19200 ); // baud-rate at 19200
29+
Serial.begin( 19200 ); // baud-rate at 19200
30+
slave.start();
3031
}
3132

3233
void loop() {

examples/advanced_master/advanced_master.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ uint8_t u8query; //!< pointer to message query
2323
/**
2424
* Modbus object declaration
2525
* u8id : node id = 0 for master, = 1..247 for slave
26-
* u8serno : serial port (use 0 for Serial)
26+
* port : serial port
2727
* u8txenpin : 0 for RS-232 and USB-FTDI
2828
* or any pin number > 1 for RS-485
2929
*/
30-
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
30+
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI
3131

3232
/**
3333
* This is an structe which contains a query to an slave device
@@ -50,8 +50,9 @@ void setup() {
5050
telegram[1].u16RegAdd = 4; // start address in slave
5151
telegram[1].u16CoilsNo = 1; // number of elements (coils or registers) to read
5252
telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino
53-
54-
master.begin( 19200 ); // baud-rate at 19200
53+
54+
Serial.begin( 19200 ); // baud-rate at 19200
55+
master.start();
5556
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
5657
u32wait = millis() + 1000;
5758
u8state = u8query = 0;

examples/advanced_slave/advanced_slave.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define ID 1
1515

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

32-
slave.begin(19200); //Abre la comunicación como esclavo
32+
Serial.begin(19200); //Abre la comunicación como esclavo
33+
slave.start();
3334
tempus = millis() + 100; //Guarda el tiempo actual + 100ms
3435
digitalWrite(13, HIGH ); //Prende el led del pin 13 (el de la placa)
3536
}

examples/simple_master/simple_master.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ uint8_t u8state;
2323
/**
2424
* Modbus object declaration
2525
* u8id : node id = 0 for master, = 1..247 for slave
26-
* u8serno : serial port (use 0 for Serial)
26+
* port : serial port
2727
* u8txenpin : 0 for RS-232 and USB-FTDI
2828
* or any pin number > 1 for RS-485
2929
*/
30-
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
30+
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI
3131

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

3939
void setup() {
40-
master.begin( 19200 ); // baud-rate at 19200
40+
Serial.begin( 19200 ); // baud-rate at 19200
41+
master.start();
4142
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
4243
u32wait = millis() + 1000;
4344
u8state = 0;

examples/simple_slave/simple_slave.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ uint16_t au16data[16] = {
1616
/**
1717
* Modbus object declaration
1818
* u8id : node id = 0 for master, = 1..247 for slave
19-
* u8serno : serial port (use 0 for Serial)
19+
* port : serial port
2020
* u8txenpin : 0 for RS-232 and USB-FTDI
2121
* or any pin number > 1 for RS-485
2222
*/
23-
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI
23+
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
2424

2525
void setup() {
26-
slave.begin( 19200 ); // baud-rate at 19200
26+
Serial.begin( 19200 ); // baud-rate at 19200
27+
slave.start();
2728
}
2829

2930
void loop() {

examples/simple_slave2/simple_slave2.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ uint16_t au16data[16] = {
1616
/**
1717
* Modbus object declaration
1818
* u8id : node id = 0 for master, = 1..247 for slave
19-
* u8serno : serial port (use 0 for Serial)
19+
* port : serial port
2020
* u8txenpin : 0 for RS-232 and USB-FTDI
2121
* or any pin number > 1 for RS-485
2222
*/
23-
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI
23+
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
2424

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

2930
void loop() {

examples/software_serial_simple_master/software_serial_simple_master.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@
4848
uint16_t au16data[16];
4949
uint8_t u8state;
5050

51+
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.
52+
5153
/**
5254
* Modbus object declaration
5355
* u8id : node id = 0 for master, = 1..247 for slave
54-
* u8serno : serial port (use 0 for Serial)
56+
* port : serial port
5557
* u8txenpin : 0 for RS-232 and USB-FTDI
5658
* or any pin number > 1 for RS-485
5759
*/
58-
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial
60+
Modbus master(0, mySerial); // this is master and RS-232 or USB-FTDI via software serial
5961

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

6567
unsigned long u32wait;
6668

67-
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.
68-
6969
void setup() {
70-
Serial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
71-
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
70+
mySerial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
71+
master.start(); // start the ModBus object.
7272
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
7373
u32wait = millis() + 1000;
7474
u8state = 0;

0 commit comments

Comments
 (0)