Pages: [1]   Go Down
Author Topic: Multi-byte serial command - the cleanest way  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to process commands that look like this is serial:

P001 (001-199)
and
V31 (0-31)
V01

The first number is a command, and the second part are values. I need to convert those values to a single byte.

Bonus if I can also recognize P1, P01 as the same thing as P001.

What is the cleanest way to do this? I am messing around with a Union data type, but all I get is 0.

Code:
#include <serMP3.h>

union serial_data {
   unsigned long number;
   byte read_byte[3];
} data;



serMP3 MP3(11,10);


void setup(){
  MP3.begin(31);
  Serial.begin(9600);
 
}

void loop(){
  byte sc = 0;
  byte n =0;
 
  if(Serial.available()){
    sc = Serial.read();   
  }
 
  if(sc == 'p' || sc == 'P'){
   
    while(Serial.available() <=0); // wait for incoming serial data

      if (Serial.available() >= 3)  // wait for four bytes
      {
        for(int i=0;i <=3; i++) data.read_byte[i]=Serial.read();
      }
      n=byte(data.number);
     
   // Serial.print(Serial.available()); 
    Serial.print(n, DEC);
    MP3.play(n);
  }
  if(sc == 's' || sc == 'S'){
    MP3.stop();
  }
 
}
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try something along the lines of this.

Look at the incoming char P or V, if 'P' read incoming chars and store them in ArrayP, like wise for 'V'. Then convert the arrays to integers. You will need to add a Null terminator to the array, to use "atoi(ArrayP)" this will convert the chars into an actual integer.
« Last Edit: February 14, 2013, 01:26:31 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    while(Serial.available() <=0); // wait for incoming serial data

      if (Serial.available() >= 3)  // wait for four bytes
Drop out of the while loop as soon as one byte arrives. Then, if 3 or more have arrived (2 would have had to arrive within 100 nanoseconds or so), read them.

There's not a snowball's chance of that if statement ever evaluating to true.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to process commands that look like this is serial:

P001 (001-199)
and
V31 (0-31)
V01

Those look like ascii character sequences with fixed lengths that can be determined from the first character.

The simplest way to handle them would be to read the first character, work out how many characters are expected to follow and wait until that many were available, then read and process them. Rinse and repeat.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, it turns out to be extremely simple. lol. So far it is working as I want. Testing it some more to see if I break it

Code:
void loop(){
  byte sc = 0;
  unsigned long number = 0;
  byte n =0;
  
  if(Serial.available()){
    sc = Serial.read();  
  }
  
  if(sc == 'p' || sc == 'P'){
    number=Serial.parseInt();
    n = byte(number);
    MP3.play(n);
  }
  if(sc == 's' || sc == 'S'){
    MP3.stop();
  }
  
}
Logged

Pages: [1]   Go Up
Jump to: