Got stuck for 4 days on a tiny Serial bug

I’ve spent 4 days trying to find a bug in a piece of code :frowning:

It eventually boiled down to the below (incorrect) line

      _myPort = new Serial(_parent, _port, 115200, 'N', '8', '1');

And yes, I’ve done serial stuff with Processing and Arduinos before. When I eventually found it I could cry :cry:

We’ve updated the title of your post to better reflect the content and help others who might run into a similar issue. Thanks for sharing your debugging story, these kinds of posts are valuable for the community!

For more on how to write helpful titles, check out this section of the guidelines for asking questions: Does your title give a good summary of your problem?

Thanks for sharing this. Looks like the issue came from using '8' and '1' as characters instead of 8 (int) and 1.0 (float). Easy mistake to make, especially since everything looks right at a glance!

Out of curiosity, did the error message help at all in finding this, or was it something you had to track down by trial and error?

Hello,

I always take an interest in Serial related topics.

This is error generated on W10 and Processing 4.4.4:

Code:


import processing.serial.*;

// The serial port:
Serial _myPort;       

// List all the available serial ports:
printArray(Serial.list());

// This genererated errors:
//_myPort = new Serial(_parent, _port, 115200, 'N', '8', '1');

// Reference: https://processing.org/reference/libraries/serial/Serial.html

// This works:

// Long Version
PApplet parent = this;
String portName = "COM3"; 
int baudRate = 9600; 
char parity = 'N';
int dataBits = 8;
float stopBits = 1.0;

_myPort = new Serial(parent, portName, baudRate, parity, dataBits, stopBits); 

// Short version that does not require variables above
//_myPort = new Serial(this, "COM3", 9600, 'N', 8, 1.0); 

_myPort.write("Yay!");

Reference:

:)

Have you tried the keyword this instead of this unknown _parent variable?

1 Like

There is no error message when I ‘compile’. I can see that the other side receives data (I can not see which data) but it does not send an expected reply back so it is not receiving correct data. If I use any terminal program instead of my Processing application it worked as expected.

Indeed. It was my stupidity to get that wrong.

It would be nice if the Serial library would check for invalid parameters and simply throw an exception.

@glv and @GoToLoop
The problem was not with the _parent.
The line was in a separate class in a separate file. I passed the parent (this) as an argument to the constructor when creating the object.

I was not clear about this because it was not relevant for my original topic title where I only wanted to vent some frustration about my stupidity.

1 Like