Go Down

Topic: Serial Data interpreting/comparing (Read 1 time) previous topic - next topic

aaron09

Okay,

So I have done a good bit of research (I'm new) and I haven't been able to get a grasp on the correct approach to analyze incoming serial data.

What i have is something similar to this "     5.4,    PSI"     

I know that seems odd. However it is simply sending a stream of serial data out in that form 3 times a second, including the spaces incase the number increases to further decimal points. What I want this to do is to send the data to a SerLCD in a specific spot, (which data type do I use?) and also at the same time, compare the number within that data set to a specified number and compare it.

So my question is, how do I analyze this data within Arduino?

Forgive my ignorance if it is simply  than I think.


Thanks in advance,

Aaron

CrossRoads

Check out asciitable.com
filter out the character for spaces,  other letters, just keep the numbers you want- values between 0x30 and 0x39 (0 to 9), plus 0x2E (period).

Haven't done any LCD work myself yet. Got plans for 2 displays, need some time for programming.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

aaron09

Sounds like a good start.

It seems like that from there I am basically pulling the entire serial message apart character by character, filtering, and then I will have to "reassemble it" somehow and then compare that numerical value with a predefined value.

My programming skills are rusty, 7 years ago I did BASIC, and then about 5 years ago, C programming. So this is  bit new here.

Thanks for the help thus far.


CrossRoads

My programming not much better - code I wrote in August really ugly compared to now.

Code should be straightforward, this should get you going down the right path.
You'll need to add the variable declaration, Serial.begin (speed), etc.void setup(), void loop(), etc.
Code: [Select]

if (serial.Available) >0){
incomingByte = serial.Read();
if ( (incomingByte >=30) && (incomingByte <=39)) {
if (upperByte_received == 1){
lowerByte = incomingByte - 0x30;  // convert from character to Hex
upperByte_recieved = 0;  // I might have this order swapped, check this
Serial.print (upperByte, HEX); // check what was grabbed
Serial.print (lowerbyte, HEX);}
else {upperByte = incomingByte - 0x30; // convert from character to Hex
upperByte = 1);}
}
}  // check that all the ( )s and { }s are paired up
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Texy

#4
Sep 08, 2011, 10:34 am Last Edit: Sep 08, 2011, 10:36 am by Texy Reason: 1

My programming not much better - code I wrote in August really ugly compared to now.

Code should be straightforward, this should get you going down the right path.
You'll need to add the variable declaration, Serial.begin (speed), etc.void setup(), void loop(), etc.
Code: [Select]

upperByte_received = 0;  // I might have this order swapped, check this



...corrected typo. Texy

CrossRoads

I can't spell any better apparently either!
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

radman

Quote
It seems like that from there I am basically pulling the entire serial message apart character by character, filtering, and then I will have to "reassemble it" somehow and then compare that numerical value with a predefined value.


You might want to look at functions like sprintf and sscanf to make and read data from strings.

PaulS

Quote
You might want to look at functions like sprintf and sscanf to make and read data from strings.

Neither of which supports, on the Arduino, floating point numbers.

radman

I had seen posts mentioning the functions but did not realise floating point had not been implemented :(
Why is that, memory restrictions?

Texy

If the data in the incoming string is always in the same location, then it is quite easy to extract a float/int/etc from a string. Look up 'atof' function, this can be used for your purpose :

Code: [Select]
   for (i=0; i<4; i++)
    {
      data[i]=buf[i];
    }
    data[4]=0;
    Temperature=atof(data);


Where data is a temporary buffer, buf is a char array which holds the complete incoming serial string and Temperature is a float variable.
This will also depend on which function you are using to read the incoming serial data, eg VirtualWire.

Texy

PaulS

Quote
Why is that, memory restrictions?

Yes. Dealing with floating point conversion adds a lot of code.

aaron09

Looks like I have a missed a couple of days in checking in on this thread.

Interesting advice,

I have progressed in the aspect of accepting the data in the arduino, (each set of data arrives in a 21 segment, so i set the "If Serial.available >20 then serial.read, etc...)

Currently i have it set up in the most unnecessarily inefficient way possibly; That is pulling each byte separately to a char value defined globally and then printing each only line by line to the LCD. Since the speed of the data coming is so slow at 3 times a second, it works good. It actually just as well as twice the speed.

Anyway, my next dilemma is streamlining using (strings?) better commands to pull specific amounts of bytes from the buffer, and having  the ability to compare the number coming in to a predefined value and changing output pin states based on that.


I know this is probably simple for most of you, however I was an ME major, not EE, so programming is limited.

The data coming in is always in the format of "XXXXXXXXXX,XXXXXXXXXX" and then a carraige return I believe.

On the left is the pressure value which can change it's decimal position based on units, which is on the right.


You'll have to bear with me, I was an ME major, not EE, so programming skills are limited to BASIC (which has helped a lot)and a bit of C.


Any insite or further direction would be greatly appreciated, please let me know if any further information is needed,



Thanks so much thus far.


-Aaron

Texy

Which serial input function are you planning on using?
Then, modify my code to capture the whole xxx,xxx string and display it on the serial monitor.
Thats a first step.

Texy

aaron09

Okay,

So, I have spent a bunch of time trying to understand how I can implement the code that you showed me when I realized the issue it was probably going to create.

When the data is coming in, it comes in with these types of formats based on pressure units at a rate of 3 times per second.
Its always 21 characters.  


"      9.08,       PSI"
"       0.1,     inH2O"
"      0.01,      inHg"
"      10.5,       kpa"
"     0.000,       Bar"
"        1.,      mBar"

So basically as it sits (and if you'll take a peak at the code), It reads each byte (as long as there are more than 20 bytes available in the buffer, assigns it to a variable, and then prints only the ones that I want so that it fits nicely on the 16x2 SerLCD I am using.

Right now, it works perfectly without any flaw.

The code could be so simplified I'm sure, however it made it easy to picture what was going to happen for the output.

The reason I am asking about the converting bytes to a string value is because I want to be able to compare the numerical value of the pressure, say 10.00 psi to a stored number of say 8. This way I can have it print something else out like "high pressure alarm" or whatever.

Forgive me needing my hand held through this, I am trying, but some of the syntax isn't exactly clicking with me. Check out my code, I
m sure it's very easy to implement.


Thanks again folks.

Aaron

Code: [Select]

int ib1 = 0;
int ib2 = 0;
int ib3 = 0;
int ib4 = 0;
int ib5 = 0;
int ib6 = 0;
int ib7 = 0;
int ib8 = 0;
int ib9 = 0;
int ib10 = 0;   // half of these varaiables are the "int" type for when I was recently messing with trying to get the adding bytes to string thing working.
char ib11 = 0;
char ib12 = 0;
char ib13 = 0;
char ib14 = 0;
char ib15 = 0;
char ib16 = 0;
char ib17 = 0;
char ib18 = 0;
char ib19 = 0;
char ib20 = 0;
char ib21 = 0;
char ib22 = 0;


void setup()
{
 Serial.begin(9600);
 Serial.print(0x7C, BYTE);  //special character
 Serial.print(0x12, BYTE);  //reset character
 delay(50);
 Serial.print(0x7C, BYTE);   //backlight ON
 delay(10);
 Serial.print(128, BYTE);
 delay(500);
 Serial.print(0x7C, BYTE);   //backlight ON
 delay(10);
 Serial.print(157, BYTE);
 delay(500);
 Serial.print(0xFE, BYTE);   //command flag
 delay(10);
 Serial.print(0x01, BYTE);   //clear command.
 delay(1000);
 

}

void loop(){  
 checkincoming();
 delay(10);
 setposition();  
 delay(10);  
 setposition();
 printing();
 
}

void setposition(){
 Serial.print(0xFE, BYTE);   //command flag
 delay(10);
 Serial.print(128, BYTE);    //position
 delay(10);
}

void checkalarm()  {
// This check alarm section is only in here as a start, I obviously haven't finished it and dont even have the program calling to it anyway.
 if (highal < ib8) {
   Serial.print(0xFE, BYTE);   //command flag
   delay(10);
   Serial.print(192, BYTE);    //position
   delay(10);
   Serial.println("     High Alarm!");
 }
 else {
   Serial.print(0xFE, BYTE);   //command flag
   delay(10);
   Serial.print(192, BYTE);    //position
   delay(10);
   Serial.println("                ");
   
}
 
}
void printing() {
 
 
 Serial.print(ib1, BYTE);
 Serial.print(ib6, BYTE);
 Serial.print(ib7, BYTE);
 Serial.print(ib8, BYTE);
 Serial.print(ib9, BYTE);
 Serial.print(ib10, BYTE);
 Serial.print(ib12, BYTE);
 Serial.print(ib17, BYTE);
 Serial.print(ib18, BYTE);
 Serial.print(ib19, BYTE);
 Serial.print(ib20, BYTE);
 Serial.print(ib21, BYTE);
 
 
 Serial.flush();
 
 
 delay(10);
}
 
 
void checkincoming(){  
 
 if (Serial.available() > 20){
    delay(10);
   
    ib1 = Serial.read();
    ib2 = Serial.read();
    ib3 = Serial.read();
    ib4 = Serial.read();
    ib5 = Serial.read();
    ib6 = Serial.read();
    ib7 = Serial.read();
    ib8 = Serial.read();
    ib9 = Serial.read();
    ib10 = Serial.read();
    ib11 = Serial.read();
    ib12 = Serial.read();
    ib13 = Serial.read();
    ib14 = Serial.read();
    ib15 = Serial.read();
    ib16 = Serial.read();
    ib17 = Serial.read();
    ib18 = Serial.read();
    ib19 = Serial.read();
    ib20 = Serial.read();
    ib21 = Serial.read();
    ib22 = Serial.read();

    delay(10);
   
   
  }
   else
   {
     checkincoming();
   }
}






AWOL

Arrays.
Arrays are the way ahead - they'll reduce your typing and debug overheads.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up