Loading...
Pages: [1]   Go Down
Author Topic: Multi-byte serial command - the cleanest way  (Read 100 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 27
Posts: 827
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
Online Online
Edison Member
*
Karma: 29
Posts: 1563
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 313
Posts: 35500
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
Tesla Member
***
Karma: 89
Posts: 6376
-
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

Offline Offline
God Member
*****
Karma: 27
Posts: 827
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
Print
 
Jump to: