Go Down

Topic: Converting old hardware serial code to newsofwareserial code (Read 411 times) previous topic - next topic

JT007

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: [Select]
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. */
}


johnwasser

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: [Select]

#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. */
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

JT007


Go Up