I'm trying to read in and parse a message in the form of X123987
continuously streaming into a serial port. There are no new lines or control characters other than the X
which marks the beginning/end of the message. The message contains two, three-character components, ex: 123
, and 987
. I'm trying to copy bytes as their read into a char array, and then on the control character, return the result, but while this compiles, I don't get any output, and it crashes constantly.
#include <SoftwareSerial.h>
SoftwareSerial RS485Serial(10, 11);
char _byte_received;
char _buffer[10];
char _value[10];
bool OnReceive()
{
if (RS485Serial.available())
{
digitalWrite(13, HIGH);
_byte_received = RS485Serial.read();
if (_byte_received == 'X') {
strcpy(_value, _buffer);
memset(_buffer, 0, 10);
return true;
} else {
strcat(_buffer, _byte_received);
return false;
}
digitalWrite(13, LOW);
} else {
return false;
}
}
void setup() {
Serial.begin(9600);
Serial.println("Restarting...");
pinMode(13, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
RS485Serial.begin(9600);
}
void loop()
{
if (OnReceive())
{
Serial.print("value: ");
Serial.write(_value);
Serial.println("");
}
}
Output:
Restarting...
value:
Restarting...
value:
Restarting...
value:
Restarting...
value:
Restarting...
value:
I expect to see value: 123987
strcat
take two strings, and you are passing a string a char, which is not what you want.concat
may be?strcat
:char *strcat(char *dest, const char *src );
. You are trying to pass achar
where there should be achar *
for the 2nd parameter.