Extracting characters from serial data ??

Hi everyone

I have an arduino connected to a sensor via MAX3232 and a RS232 cable.
Getting the data from the sensor is working fine.

How do i take only a portion of the returned data out (from data[71] to data[78]), and store it in a variable, so i can compare it to a preset value ?
I have tried to concatenate the data in various ways, but all i get is strange characters in the output…

My data is looking like this:
MD03 011 +7900-03 00 00 001 000000 012 +1100-02 00 00 001 000000 013 +1890-02 00 00 001 000000

Here is my code:

#include <NewSoftSerial.h>

NewSoftSerial mySerial(6, 7);
char incomingByte;
char data[200];
int i = 0;  //counter

void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
}


void loop() {
  
  mySerial.print(0x02, BYTE);  // STX
  mySerial.print("DA"); 
  mySerial.print(0x0D, BYTE); // CR
  
 i = 0;
 while (mySerial.available() > 0)
 {
  char charData = (char)mySerial.read();   //Read the buffer
  data[i] = charData;                              //Fill data into array
  i++;
  if (charData == 0x0D)                          //If CR then we have a full line
  break;
 }
 if (mySerial.overflow()) Serial.print("Buffer overflow - Characters lost!"); 
 
 //int j = 0;
 //for (j=0; j < i; j++) Serial.print(data[j]);  //Send data to PC

 Serial.print(data[71]);
 Serial.print(data[72]);
 Serial.print(data[73]);
 Serial.print(data[74]);
 Serial.print(data[75]);
 Serial.print(data[76]);
 Serial.print(data[77]);
 Serial.print(data[78]);
 Serial.println(); 

 delay(500);
}

I didn't count the characters, but if you don't care about data[79] you can do this

data[79] = '\0';
Serial.println (&data[71]);

EDIT: Sorry, I just looked at the printing part. Something similar may still work, it depends on the answers to Pauls' question.


Rob

I have tried to concatenate the data in various ways, but all i get is strange characters in the output…

Copying the data from one array to another is trivial, using a for loop with two index variables.

char data[200];
char copy[9];
for(int i=71, j=0; i<=78; i++, j++)
{
   copy[j] = data[i];
}
copy[8] = '\0';

Doing something with that copied data is a different story.

so i can compare it to a preset value ?

What is this preset value? A character array (strcmp() would be useful) or an int (int copyVal = atoi(copy))?

This was exactly what i needed :slight_smile:

char copy[9];
for(int i=71, j=0; i<=78; i++, j++)
{
   copy[j] = data[i];
}
copy[8] = '\0';

What exactly does the ‘\0’ do/mean ?

I want compare the retrieved value, eg: “+1370-02” to a preset value of “+1000+01”, so i guess i should use strcmp()

What exactly does the ‘\0’ do/mean ?

It delimits the characters, essentially turning an array of chars into a NULL-terminated string.

i guess i should use strcmp()

Only if you are interested in an exact match, if you need <> numeric values then you have to convert the strings.

If you are just comparing strings then the method I proposed should work

data[79] = '\0';
if (0 == strcmp ("+1000+01", &data[71]))
   // strings are equal

Rob

Character escape sequences are in backslash notation as defined in ANSI X3.159-1989 (`ANSI C’).
The characters and their meanings are as follows:

\e Write an character.
\a Write a character.
\b Write a character.
\f Write a character.
\n Write a character.
\r Write a character.
\t Write a character.
\v Write a character.
’ Write a character.
\ Write a backslash character.
\num Write an 8-bit character whose ASCII value is the 1-, 2-,
or 3-digit octal number num.

To load the actual numbers from the string into variables, in case you don't end at comparison for exact match, use sscanf. Examples are at cplusplus.com