Hello, I've made a function to receive a string via SoftwareSerial. In the function I've had to include a short delay <delay(5)> or the incoming string shows random ascii characters at seemingly random times. Can anyone explain to me why this happens? If I make the delay any shorter than 5 milliseconds the errors occur. The shorter the delay, the more frequent the errors occur.
Below is my test program which I will eventually implement on an RS485 network, so the send_message() function also sends a signal to the MAX485 chip to transmit on the network. Please ignore this part of the example.
An aside: Although I'm no software developer, I hope this example helps others in their Arduino serial communication. I've learned so much about programming from this forum that I hope to be able to give back. If this is somehow a poor example of code, please correct it for the benefit of everyone else.
Why do I need that small delay in receive_message()?
#include <SoftwareSerial.h>
#define RS485RX 2
#define RS485TX 3
#define TxControl 4 //RS485 Direction control
#define Transmit HIGH
#define Receive LOW
String message;
byte start_of_text = 2;
byte end_of_text = 3;
SoftwareSerial RS485(RS485RX, RS485TX); // RX, TX
void setup() {
Serial.begin(9600);
RS485.begin(9600);
delay(100);
Serial.println("Connection established at 9600 baud from SLAVE to COMPUTER.");
}
void loop() {
message = receive_message();
if( message != "" ) //only report meaningful strings
{
Serial.println(message);
send_message(message);
}
}
/*********************************************************************************************************
* This function checks for the "start of text" ascii char <2> and then reads in each char to a string.
* The function breaks out of reading into the string if the "end of text" char <3> is read or the maximum buffer size is reached
*********************************************************************************************************/
String receive_message(){
char incoming_char;
int max_size = 64; //size of serial buffer on UNO
String message_string = "";
if( RS485.read() == start_of_text )
{
delay(5); //Not sure why I need this, but it needs to be at least 5ms, as per experimentation
while( message_string.length() <= max_size ) //end the while loop if the "end of text" char is read or buffer maximum is reached
{
incoming_char = RS485.read();
if( incoming_char == end_of_text ) break;
else
{
message_string += incoming_char;
}
}
}
return message_string;
}
/*********************************************************************************************************
* This function outputs the message, msg, preceded by a start-text char and followed by an end-text char
* Since this will be used on an RS485 network, the enable-transmission line is also controlled.
*********************************************************************************************************/
void send_message(String msg){
digitalWrite(TxControl, Transmit);
RS485.write(start_of_text);
RS485.print(msg);
RS485.write(end_of_text);
digitalWrite(TxControl, Receive);
}