2

TL,DR : bufferUntil() and readStringUntil() works fine when set to '\n' but creates problems for other characters.

The code that sends data to pc is below;

 Serial.print(rollF);
 Serial.print("/");
 Serial.println(pitchF);

And the relevant parts from processing are;

myPort = new Serial(this, "COM3", 9600); // starts the serial communication
 myPort.bufferUntil('\n');
void serialEvent (Serial myPort) { 
 // reads the data from the Serial Port up to the character '\n' and puts it into the String variable "data".
 data = myPort.readStringUntil('\n');
 // if you got any bytes other than the linefeed:
 if (data != null) {
 data = trim(data);
 // split the string at "/"
 String items[] = split(data, '/');
 if (items.length > 1) {
 //--- Roll,Pitch in degrees
 roll = float(items[0]);
 pitch = float(items[1]);
 }
 }
}

A picture from my incoming data(from arduino serial monitor):

0.62/-0.52
0.63/-0.52
0.63/-0.52
0.64/-0.53
0.66/-0.53
0.67/-0.53
0.66/-0.54

Until here, everything is fine as it should be. Nothing special. The problem occurs when I change the parameters of bufferUntil() and readStringUntil() functions to anything other than '\n'. Of course when I do that, I also change the corresponding parts from the arduino code. For example when replacing '\n' by 'k', the incoming data seen from arduino serial monitor looks like,

45.63/22.3k21.51/77.32k12.63/88.90k

and goes on like that. But the processing cannot get the second value in each buffer. When I check it by printing the values also on the console of processing I get the value of first one(roll) right however the second value(pitch) is shown as NaN. So what is the problem? What is the reason that it only works when it is '\n'.

asked Nov 25, 2019 at 1:07
1

1 Answer 1

1

I cannot check it right now but I think you might have two issues.

First off, you don't need to use bufferUntil() and readStringUntil() at the same time.

And second and more important, both functions take the character as an int so if you want to read until the character k you should do:

data = myPort.readStringUntil(int('k'));

Or, since k is ASCII code 107:

data = myPort.readStringUntil(107);

If you call the function with the wrong type as you are doing nothing will happen and the port will keep reading until it finds the default line feed.

answered Nov 25, 2019 at 6:31
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer, however I was also thinking the same issue but 2 drawbacks of this approach. 1: when '\n' is put directly it works, so it is just a character as 'k' is. There should not be such necessity to use the decimal ascii value. 2: I tried writing 107 and didnt work.
As I said, I cannot test this kind of thing right now, I'm basing my comments in the documentation. The fact that "it works" when you use \n might be related to the fact that the line feed is the default character? Have you tried using each function on its own?
I don't know but I am positive that I have also tried by passing 107 to as parameter to both functions.
What if you keep the bufferUntil(107) and replace data = myPort.readStringUntil('\n'); with data = myPort.readString();. This should make it easier to see if the event is being triggered
I figured out what is wrong, I think you might want to know, so here the deal: bufferUntil() or readStringUntil() was reading data up to and INCLUDING the character of interest. So when my 'k' character was being a part of the items[1] array, float() function cannot work on that. When I changed it such that it will be items[1] string will omit its last character, it works now fine. The only remaining problem is, why '\n' creates no problem with float() but 'k' creates problems?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.