Serial Port Communication (synchronize)

Hi everyone, I'm trying to send and receive data through serial port, but it seems that I can't make the Arduino talk properly to the computer and the data that appears in the serial monitor is not in order. Is there an easy way (i'm noob) to allow to write and read through serial port so that there is not a conflict between them?

For example:
If I receive 145, I need to split the number in 1 (mode) and 45(temperature) so the order in which I receive info is crucial.

Right now, Im using this code, but does not seem to work...

while(Serial.available() && i<=2){

incomingRawData[i++] = Serial.read(); //Recibimos los 3 valores del servidor
}
if (i==3){
incomingRawData[3]= '\0'; //Serial.println(incomingRawData);
}

for (int i=1; i<3; i++) {
tempvolt = tempvolt10 + incomingRawData-'0';*

  • }*

  • if (tempvolt>0){*

  • b=tempvolt;*

  • }*

  • if (incomingRawData[0]>0){*

  • a=incomingRawData[0];*

  • }*

  • Serial.println(a);*

  • tensentrada=analogRead(analogPin);*
    _ tref=(tensentrada0.1465)-50;_
    _ d=tref
    100;_
    (...)
    Thanks for your time!

  1. you need to post code inside of code tags. In the post edit window, the # button makes code tags.
    without the tags not everything shows properly.

  2. the Arduino IDE has AutoFormat as the top tool in the Tools menu. you really need to use it.

This

while(Serial.available() && i<=2){
    
    incomingRawData[i++] = Serial.read(); //Recibimos los 3 valores del servidor
  }

is not going to work simply because Arduino is enormously faster than serial data arrives.
The first time your while loop gets to the top and the next character you expect hasn't arrived yet, it exits.

Salting in delays is a poor, assumption based solution that you may find or get advised "this will fix it".

Here is a good tutorial on the subject, better than what I could give you here:

How to process incoming serial data without blocking

macster:
Hi everyone, I'm trying to send and receive data through serial port, but it seems that I can't make the Arduino talk properly to the computer and the data that appears in the serial monitor is not in order. Is there an easy way (i'm noob) to allow to write and read through serial port so that there is not a conflict between them?

For example:
If I receive 145, I need to split the number in 1 (mode) and 45(temperature) so the order in which I receive info is crucial.

Right now, Im using this code, but does not seem to work...

with a lot of help from @GoForSmoke I built a serial slave on a clock project. It collects data in several forms in intervals that vary by the data (the data is updated at different intervals)

There is a lot in the code, but mainly it processes the serial in.

Perhaps you can glean something from this. Ignore the large arrays, they are for LED displays of weather. Focus on the serial processing...

ClockSlaveFinal.ino (15.8 KB)

Thanks a lot for your time, I think this will solve the problem!!

FYI t will accept individual messages like:

T75c (temperature is 75)

or strung together:

T75N0W4c (temperature is 75, positive temperature, weather is "4")

c is the end of message

If you read 1 char at time and match those chars to successive chars in an array string then you can match text patterns like words without using a buffer or any string functions. Let that sink in for a while if you have only ever used the buffer, parse and lex route... no buffer ... no strcmp(), strchr(), str-anything.

How freaking much learning and practice is that worth? Because then you invent your own twists and turns.