Pages: [1]   Go Down
Author Topic: Converting old hardware serial code to newsofwareserial code  (Read 304 times)
0 Members and 1 Guest are viewing this topic.
Lake District, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Electronics needs smoke to work, if it escapes its broken
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im working away on some old modbus communication examples but unfortunately they use the hardware serial port to transmit/receive.
could someone please suggest how it can be changed to use NewSoftwareSerial instead.

Code:
void setup()
{
        const int baudrate = 9600;
        if (baudrate <= 19200)
                interframe_delay = (unsigned long)(3.5 * 11 / baudrate);  /* Modbus t3.5 */
        Serial.begin(baudrate); /* format 8N1, DOES NOT comply with Modbus spec. */
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) Use Arduino 1.0 or newer and SoftwareSerial
2) Create a SoftwareSerial object (http://arduino.cc/en/Reference/SoftwareSerial)
3) Use that in place of Serial.
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
        const int baudrate = 9600;
        if (baudrate <= 19200)
                interframe_delay = (unsigned long)(3.5 * 11 / baudrate);  /* Modbus t3.5 */
        mySerial.begin(baudrate); /* format 8N1, DOES NOT comply with Modbus spec. */
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Lake District, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Electronics needs smoke to work, if it escapes its broken
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John,
much appreciated
Logged

Pages: [1]   Go Up
Jump to: