Help me for serial data processing.

Hello

One hardware on the serial port transmit this data (only when I want, and always changes the value):

0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0,1000,1000,1000,1000,0,0,0,0,11.3,0,\r\n

I want to put the data to variables.

For example (Starting from the example):

var1 = 0
var2 = 0.02
var3 = -0.01
var4 = 9.82
etc......

There is no idea, and a lot, tried a sample, but it do not work.
Can you help me?

Thanks in advance.
Broland

Check out strtok() in string.h

tried a sample, but it do not work.

Which one?

It did something. You wanted it to so something. Obviously those two somethings weren't the same thing, but what either one of them was is a mystery. We need your help solving that mystery.

Since the message is quite long, I'd process it one byte at a time. Hint: numbers (which you want to assign to variables) are separated by commas, and the whole message ends with \r\n.

Hello

Okey, I know what the problem is!
I tryed this sample:

/*
  Software serial multple serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.
 
 The circuit: 
 * RX is digital pin 2 (connect to TX of other device)
 * TX is digital pin 3 (connect to RX of other device)
 
 created back in the mists of time
 modified 9 Apr 2012
 by Tom Igoe
 based on Mikal Hart's example
 
 This example code is in the public domain.
 
 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

void setup()  
{
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(115200);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

When I send this data from the hw port serial monitor to sw port serial monitor its OK.

hw serial monitor send:
0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0, 1000,1000,1000,1000,0,0,0,0,11.3,0,
sw serial monitor show:
0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0, 1000,1000,1000,1000,0,0,0,0,11.3,0,

When I tryed reverse, from soft serial monitor to hw serial monitor, not OK.

sw serial monitor send:
0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0, 1000,1000,1000,1000,0,0,0,0,11.3,0,
hw serial monitor show:
0??KLÔ?ºÁe,9??LK?bÁ±b5??NL-&&ªÁÁX1??LK,&&I?ÁÁ??KL#¤²Åa00?&&)?ÁÁX0??JL+&)?¹ÍX0?Cá

The sw port baud must set 115200.
This is timing problem?

Try lowering the baud rate. Maybe the software serial code is missing some bits...

Software serial can run at 115200, but only if no other interrupts are happening. Sending serial data through the hardware serial port causes interrupts that interfere with software serial, causing data loss at high baud rates.

As tuxduino suggests, lower the software serial baud rate or minimize what you are printing to the hardware port. Or both.

Hello

These are my attempts :slight_smile: At least half works!
It works, but only when first receive data.

Sample for serial test:
0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0, 1000,1000,1000,1000,0,0,0,0,11.3,0,
char inData[140];
byte index;

float datas[24] = {0};
char * val;
int count = 0;

boolean received = false;

void setup(){
  Serial.begin(115200);
}

void loop(){

  while(Serial.available() > 0){	                
    char inChar = Serial.read();
    if(index < 140){
      inData[index] = inChar;
      index++;
      inData[index] = '\0';
    }    
    received = true;    		          
  }

if(received){
  val = strtok (inData,",");		
  datas[count] = atof(val);
		
  while ((val = strtok (NULL, ",")) != NULL){
    datas[++count] = atof(val);
  }
  
  for(int b=0; b<=23; b++){
    Serial.print(b);
    Serial.print(":");
    Serial.println(datas[b],2);
  }  
    
  received = false;
  }    
}

You are looking good.

What do you mean by "first receive data"

Your post processing may be taking a long time and subsequent data is overwriting the 64 character receive buffer.

Check out how many characters are available during each pass. If it is 64, you are probably missing data.

If that is the case, you may need to process one character (as was mentioned earlier) at a time and check for the ',' yourself, the strtok overhead may be too much.

If that is the case, you may need to process one character (as was mentioned earlier) at a time and check for the ',' yourself, the strtok overhead may be too much.

Not likely.

The real issue seems to be that there is an expectation that all the serial data will arrive at once, and that is not happening. The sender should append some kind of end-of-record marker on the data, and received should not be set to true until that marker arrives.

I see nowhere where count is reset to 0, either, so the array where the floats are stored is going to be overrun the second time a record arrives.