SerialPort readString not reading line correctly

I’m trying to use the serial port from the wpilib library and it’s causing some issues. I’m working on receiving information from our camera’s print statements to do some vision tracking, but the information from the serial port readString function comes back incorrectly. Sometimes there is random empty full lines in between random letters and it reads no information from the camera. Can anyone explain why this phenomenon happens? Thank you so much for your help.

That usually means the baud rate is set incorrectly. What is the camera’s serial port specified for, and how are you setting up the serial port in code?

Does your camera print the outputs with the \n statement at the end to declare a new line?

If you post which camera you are using we can help you find what Baud rate the serial port should be set to.


We are using the OpenMV M7. I’ve increased the baud rate to 115200 and had better luck. We didn’t have the strings be split into different lines but we still sometimes get empty strings which I used a try/catch to get rid of. I had no luck trying an actual baud rate for the OpenMV’s print statements.

It does not

I’ve looked for a baud rate for the OpenMV but no luck. Here’s our little bit of test code after we increased the baud rate. Setup is the near the top. The camera should be printing !100!#100#12 but once the serial port reads it, it becomes @ 1 0 0 @ # 1 0 0 # 1 2 in the console trying to do regular system outs. Increased baud rate fixed the split strings issues.

That almost sounds like you’ve exactly doubled the baud rate. Try 57600?

I think you’d get gibberish if the baud rate is off by 2x. Sounds more like UTF-8 to me.

SerialPort decodes from bytes to characters using US_ASCII:

  public String readString(int count) {
    byte[] out = read(count);
    return new String(out, 0, out.length, StandardCharsets.US_ASCII);

This replaces unknown (non-ASCII), e.g. gibberish, characters with 0xFFFD, which is likely showing up as a space when printed.

To remove the RoboRIO from the equation, I would recommend hooking up the serial port to a computer (via a USB to RS-232 to TTL converter) and seeing if you can get a good baud rate and see the output.

Add the \n to see if it reads only if there is a new line.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.