How to speed up serial communication (with Serial.parseInt()) ?

Hi there,

I use Serial.parseInt() to receive incoming bytes from another programm. My code looks like that:

unsigned long byte1 unsigned long byte2 unsigned long byte3 unsigned long byte4

void setup(){ .... }

void loop(){ ... if (Serial.available() > 0){ byte1 = Serial.parseInt(); byte2 = Serial.parseInt(); byte3 = Serial.parseInt(); byte4 = Serial.parseInt();

// 1. start to do something with the incoming bytes... } }

Since I need to save times, the bytes have to be of type "unsigned long". The point is, I have to wait around 5 seconds to get the code running after the reading of incoming bytes and to start the actual task of the programm.

So, this is definitely too long! Do you know how to fix that in order to process the incoming bytes faster??

many thanks in advance!

You need to change your terminology. A byte cannot be an unsigned long, because an unsigned long consists ot 4 bytes.

So, are you: 1. reading 4 bytes that are going to be put into an unsigned long variable? 2. Reading 4 unsigned long intetegers? 3. Just trying to confuse yourself with incredibly poorly named variables?

As for speed, well, I agree that 5 seconds is too long, but WHEN do the bytes come in? You can't actually get them until they do, and you are trying to read into four variables when there is likely ony one byte available.

Please clarify what it is you are trying to read, and we can get started on helping you.

Oh, and please place your code inside code tags (highlight it, then click the # icon above.

The point is, I have to wait around 5 seconds to get the code running after the reading of incoming bytes and to start the actual task of the programm.

Don't use parseInt(). Send an end of packet marker, read and store data until that happens, and then parse the data that arrived. Using a blocking function, and then complaining about the blocking, is silly.

ok, thanks for the hints.

sorry naming the variables "bytes" was confusing, I admit - I´m reading 4 unsigned long variables. I´m actually sending comma seperated integers from Matlab (i.e. 1, 58, 90, 2) and want them to be saved in four variables.

Do you have any idea what´s slowing down that process?

Do you have any idea what´s slowing down that process?

Sure. You failed to post all of your code. That really slows down helping you.

ok code looks like that:

unsigned long variable1
unsigned long variable2
unsigned long variable3
unsigned long variable4

void setup(){
....
}

void loop(){
...
// arduino receives incoming comma seperated data (i.e. 1, 34, 56, 2)
if (Serial.available() > 0){
variable1 = Serial.parseInt();
variable2 = Serial.parseInt();
variable3 = Serial.parseInt();
variable4 = Serial.parseInt();

// 1. start to do something with the incoming bytes...

if(variable1 == 1){
// so something
}

if(variable2 == 34){
// do something
}
....
}
}

This is still not working code

major problem: Serial.available > 0 implies that there is at least 1 byte, not that there are enough to parse 4 integers Serial.available > 7 implies that there are at least 8 bytes

solution: you must read all characters in a buffer e.g. char buffer[32]; until you read a \n (return) then split the buffer in its individual numbers (search for strtok) then parse those numbers into your variables.

@robtillaart

Look at the user guide for parseInt() etc they all block awaiting data so there no need to use available and/or an additional buffer.

@OP When PaulS said post all your code thats just what you need to do code with ....... init is no use.

Having said that it looks to me that your problem is with Matlab sending the data and not with the arduino end of things. Of course that's only based on the snippet of code you did post.

Mark

holmes4: @robtillaart

Look at the user guide for parseInt() etc they all block awaiting data so there no need to use available and/or an additional buffer.

Look at the code how it is implemented (Stream.cpp/.h) and you see there is a timeout (of 1000 millis), so yes you are right they block but max for one second. So you have no guarantee all data is in so you cannot trust this code that it does what the documentation says, even if it had worked well 1K times.

In case of difference between the code and the documentation, the code always wins

blinkerlicht: I´m actually sending comma seperated integers from Matlab (i.e. 1, 58, 90, 2) and want them to be saved in four variables.

How are these messages separated? If they're separated by newlines then buffering a complete line and then parsing it to extract comma separated numbers is relatively simple using scanf() or strtok() and atol(). There are plenty of examples in the playground showing how to buffer a line of text, and examples have been posted here on the forum many times.