SerialPort readLine() returning empty string(s)

I'd like to use the p5.SerialPort object to read and display data from a USB connected embedded processor. But so far, no joy.

The Symptom

serial.readLine() returns an empty string.

The Environment

The Serial Source

I have a KL27Z running a trivial program:

    while (1) {
      PRINTF("%d\r\n", i++);
      delay(1000);
    }

The KL27Z connects to my MacBook Pro via USB, appearing on /dev/cu.usbmodem1421, and I've verified that it is working by running the screen command line app in a terminal window:

$ screen /dev/cu.usbmodem1421 9600
75
76
77
...

The Serial Server

I'm running the p5.serialcontrol pre-built serial server. It lists the following ports (as expected):

Available Ports
  /dev/cu.Bluetooth-Incoming-Port
  /dev/cu.rdPhone-WirelessiAP
  /dev/cu.usbmodem1421

The p5 sketch

I'm running Tom Igoe's p5.serialport-master/examples/readAndAnimate/sketch.js nearly verbatim. The only change I've made is to specify the serial port as /dev/cu.usbmodem1421 and the baud rate to be 9600.

The Browser

I'm running Chrome Version 58.0.3029.96 (64-bit) and using the Developer tools to set breakpoints, see the javascript console, observe the state of variables, etc.

What I observe

The javascript console shows that the sketch connects to the Serial Control server:

opened socket
p5.js:11798 List of Serial Ports:
p5.js:11798 0 /dev/cu.Bluetooth-Incoming-Port
p5.js:11798 1 /dev/cu.rdPhone-WirelessiAP
p5.js:11798 2 /dev/cu.usbmodem1421

The gotData() method in sketch.js:55 gets called, but its call to serial.readLine() always returns an empty string.

Digging down into serial.readLine() => serial.readStringUntil('\r\n') is also getting called, but this.serialBuffer[i] always returns 0. This repeats until serialBuffer.length is exceeded (40), and the resulting joined string is null.

What's suspicious is that gotData() is supposed to be triggered by serial.on('data', gotData), and it appears to be getting called even with the KL27Z is held in reset (i.e. not sending any serial data).

Any ideas of where I should be looking?

Answers

  • Answer ✓

    Hup! Never mind! I had a baud rate mismatch in my code. (Board was transmitting 9600 and sketch was expecting 115200, despite what I'd said in my comments.)

  • @fearless_fool Thanks for sharing you answer. Good luck!

    Kf

Sign In or Register to comment.