Help Reading Serial Data Stream

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
chrisab508
 
Posts: 21
Joined: Tue Jul 13, 2010 6:22 pm

Help Reading Serial Data Stream

Post by chrisab508 »

Hi Guys,
I am sending a serial data stream to my Arduino (Lilypad) containing a sequence of start characters (to identify the transmission) followed by RGB values for LEDS (12mm x 25 strand) followed by a sequence of stop characters to identify the end of transmission. A typical stream looks like this:

Start characters: " ** " (note the spaces),
RGB values: R: 0xFF, G: 0x00, B: 0x00 (just examples)
End characters: " ^^ " (note the spaces).

So the total length of each transmission/command in bytes is: 4xStart Char, 3xRGB value, 4x Stop char = 11.

I'm having trouble capturing/parsing the whole data stream to get my LEDs to illuminate to the correct color. Note, the following bits of code all occur in a "void loop" function; I've tried some code as follows:

Code: Select all

  if ( Serial.available() > 0) { // if there are bytes waiting on the serial port
  char inByte = Serial.read();
  delay(10);
  if (inByte == ' ')
  {
    int len = 10;
    char inString[len];
    for (int i = 0; i < len; i++)
    {
      inString[i] = Serial.read();
    }
    int serv_cmd = atoi(&inString[5]);
    
    R = inString[6];
    G = inString[7];
    B = inString[8];
    
    colorWipe(Color(R,G,B), 10);
    colorWipe(Color(0,0,0), 10);
  }

  } 
I've also tried just hard-coding Serial reads into the code as follows

Code: Select all

  if ( Serial.available() > 0) { // if there are bytes waiting on the serial port
  Serial.read();
  Serial.read();
  Serial.read();
  Serial.read();
    
    R = Serial.read();
    G = Serial.read();
    B = Serial.read();
    

  Serial.read();
  Serial.read();
  Serial.read();
  Serial.read();
    
    colorSet(Color(R,G,B));
    delay(2000);
    colorSet(Color(0,0,0));
    delay(2000);
  }
Or, I've tried attempting to identify that start transmission symbols, then try to capture the RGB values:

Code: Select all

if ( Serial.available() > 0) { // if there are bytes waiting on the serial port
   char inByte = Serial.read(); // read a byte
   delay(10); // *** added to make it work ***
   if (inByte == ' ') { // if that byte is the desired character
   int len = 10; // expected string is 6 bytes long
   char inString[len]; // declare string variable
   for (int i = 0; i < len; i++) 
   {
     inString[i] = Serial.read();
   }
    if ( strstr(inString, "** ") != NULL )
    {
     R = Serial.read();
     G = Serial.read();
     B = Serial.read();
    }
 }
}
Various of the above codes work, in the sense that when I send a command the LEDs turn on, but it is never ever the correct color. I suspect I'm looking at the wrong bytes when setting the colors, and I don't think I'm accurately capturing the whole stream. I was wondering if you guys have any suggestions on how I can improve/make this work. It is also worth noting that it is very hard to debug this, as the commands are coming from a bluetooth device which is external to my computer. My process is thus: program the Arduino, connect Arduino to external device via bluetooth, send color command, observe whether this updated code worked, if not, disconnect bluetooth, connect FTDI cable, re-program, repeat. I don't seem to have way to monitor the bluetooth/serial traffic unfortunately.

Thank you in advance for any help.

User avatar
cstratton
 
Posts: 294
Joined: Wed Sep 29, 2010 3:52 pm

Re: Help Reading Serial Data Stream

Post by cstratton »

Perhaps you could use a bluetooth adapter on the PC (USB ones are quite cheap) to capture some of the data and verify it is exactly as expected.

Then you could write a pc program to send simulated data via the usb-serial cable to the arduino during testing.

(please disregard the bit about confusing start/stop with data that was here earlier, I'd misread part of the post)
Last edited by cstratton on Sat Dec 24, 2011 5:39 pm, edited 1 time in total.

User avatar
baldengineer
 
Posts: 127
Joined: Sun Sep 26, 2010 11:49 pm

Re: Help Reading Serial Data Stream

Post by baldengineer »

The problem I see is that you assuming any time Serial.available is greater than 0, your entire string is available. That's really not a safe assumption.

I'm not sure how you count 11 characters.

Your stream as you wrote it would be:
" **0xFF0x000x00 ^^ " that's 19

I think you meant:
" **FF0000 ^^ " which is 13 characters.

So I'm not sure where you see 11 bytes. Further more, in your code you only define a string buffer length of 10?

Once option is to wait until Serial.available() returns 13. No robust but is better than assuming that because 1 character is in the buffer that all of them are.

User avatar
chrisab508
 
Posts: 21
Joined: Tue Jul 13, 2010 6:22 pm

Re: Help Reading Serial Data Stream

Post by chrisab508 »

I suppose I was a little confused about how many bytes of data were being sent. In the code which sends the data via bluetooth it sends the colors as two hex values which is eight bits. Im now realizing that the data is likely being sent as two ascii characters rather than a single byte of two hex values. Also in my loop I only captured ten bytes because I had already captured the first one to determine whether it was part of the first start stream.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”