0

This sketch runs well with SoftwareSerial define and the monitor displays device modbus values. But now, i want to replace SoftwareSerial by Serial1 and i have no data.

This is the sketch with SoftwareSerial:

#include <SoftwareSerial.h>
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin
#define SSerialTxControl 40   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW


SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
byte byteSend;


void setup()   
{
  Serial.begin(9600);
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Transmit);  // Init Transceiver
  RS485Serial.begin(9600);   
}
void loop()   
{
  delay(500);
  if (RS485Serial.available()) 
  { for(int i=0; i<7; i++)
    {
    byteSend = RS485Serial.read();   // Read the byte

    Serial.print(byteSend, HEX);
    Serial.print(" ");
    }
  }
  digitalWrite(SSerialTxControl, RS485Transmit);  // Init Transceiver
  byte request[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A}; 
  RS485Serial.write(request, sizeof(request));

  digitalWrite(SSerialTxControl, RS485Receive);
  Serial.println(" ");
}

And the sketch i tried with Serial1 (pin 19(Rx) and 18(Tx)):

#define SSerialTxControl 40   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW

byte byteSend;


void setup()   
{
  Serial.begin(9600);
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Transmit);  // Init Transceiver
  Serial1.begin(9600);   
}
void loop()   
{
  delay(500);
  if (Serial1.available()) 
  { for(int i=0; i<7; i++)
    {
    byteSend = Serial1.read();   // Read the byte

    Serial.print(byteSend, HEX);
    Serial.print(" ");
    }
  }
  digitalWrite(SSerialTxControl, RS485Transmit);  // Init Transceiver
  byte request[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A}; 
  Serial1.write(request, sizeof(request));

  digitalWrite(SSerialTxControl, RS485Receive);
  Serial.println(" ");
}

Can you help me to find my mistake ?

  • 1
    your code expects 7 bytes after asking if at least one is available – Juraj Dec 21 '18 at 19:10
  • 1
  • Sorry, but i don't understand what you mean ? Is there a difference with byteSend length using SoftwareSerial or Serial1 ? – Teddol Dec 21 '18 at 19:59
  • 1
    The difference is likely due to SoftwareSerial::read() taking a full character time (appx 105uSec at 9600 baud) because individual bits are collected by the software. With a hardware UART, the ::read() call takes only a few microseconds, since the bits are collected by hardware, without holding up the software (much...). So in the hardware case, loop() can execute the ::read() call much more frequently than characters can arrive. Short answer: in the the software version, you just got lucky! – JRobert Dec 21 '18 at 20:15
  • So what is the best way to solve it, using Serial1? – Teddol Dec 21 '18 at 20:46
1

what is the best way to solve it, using Serial1?

With either serial device, you need to collect characters until you have an entire message. If every message will be a fixed number of bytes, you can wait until the return value of the Serial::available() call indicates it has that many characters, then collect them into a buffer and process them. But this assumes you'll never get a badly-formed message. If you do, the incoming bytes will get out of sync with your program. A better way is to collect bytes into your buffer (but do nothing with them) until you have a complete message (by byte-count, newline character, or however you need to recognize that it is complete) or your buffer is about to overflow, and then either process them, or if your buffer filled and the contents doesn't look like a valid message, discard it and try to re-synchronize with the source. That may mean reading and ignoring bytes until you get a newline, and dealing with the fact you got a garbled message, or telling your source to restart or reset. The exact method (or whether you even want to bother detecting bad messages) depends on the particular situation. Will someone die if you fail? Will something catch fire, burn parts, lose weeks worth of good data, ... ? Or is this an experiment whose consequence of failure are zero or at least not worth any effort to detect it?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.