0

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

  • 3
    You wrote: “while this compiles [...]”. No. The code you posted does not compile. – Edgar Bonet Jul 20 '17 at 18:08
  • 2
    I don't know what library are you using, but strcat take two strings, and you are passing a string a char, which is not what you want. concat may be? – user31481 Jul 20 '17 at 18:11
  • I updated the code to make it complete so it now does actually compile. I'm very likely using the char array functions wrong, and that's probably my issue. Should I creating a one char string before using strcat? I think I just don't know how to ask my question properly. – waspinator Jul 20 '17 at 18:47
  • 1
    Look at the prototype for strcat: char *strcat(char *dest, const char *src );. You are trying to pass a char where there should be a char * for the 2nd parameter. – Johnny Mopp Jul 20 '17 at 18:53
  • How much data will be received ? What else is the sketch doing ? The SoftwareSerial takes over the Arduino, and there is not a lot that the Arduino can do any more. The words "continuously streaming" and "SoftwareSerial" rings an alarm bell. You might need an other Arduino board. – Jot Jul 21 '17 at 2:12
1

In OnReceive, you are concatening a string with a char. This may be the cause of the resets.

As a side note, you don't really need to use strcat, strcpy and other str functions. You can make your sketch lean by accesing the arrays directly, like this:

char _buffer[10];
int index = 0;

bool OnReceive()
{
  bool status = false;
  if (RS485Serial.available())
  {
    digitalWrite(Pin13LED, HIGH);
    char _byte_received = RS485Serial.read();
    if (_byte_received == 'X') {
      _buffer[index] = 0; // Mark the end of string.
      index = 0;
      status = true;
    } else {
      _buffer[index++] = _byte_received;
      // ToDo: check for overflow;
    }
    digitalWrite(Pin13LED, LOW);
  }

  return status;
}
void setup() {
  Serial.begin(9600);
  Serial.println("Restarting...");
  pinMode(Pin13LED, OUTPUT);
  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);
  RS485Serial.begin(9600);
}

void loop()
{
  if (OnReceive())
  {
    Serial.print("value: ");
    Serial.print(_bufer);
    Serial.println("");
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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