3 years, 5 months ago.

Cant read serial port


I use the following code to read the outputs from a mbus converter. It is attached to a mbus master, that check for its slaves by sending a fixed fattern. The mbus levels are converted to 3.3 V logic, are observed on an oscilloscope and seem correct.

I have connected the output to PD_6 as a DigitalIn and let the input drive a led on the board. The led blink as expected.

The same output is connected to PC_10/PC_11 as a serial port. The output is measured by oscilloscope: levels seem fine. The led still blinks as expected. But, no output may be read from the serial port.


1 Answer

3 years, 5 months ago.

Hello Per,

  • Have you configured also the serial terminal monitor (running on your PC) to 8-bits, even-parity, 1-stop bit? If no then I would suggest to remove pc.format(8, SerialBase::Even, 1); from your code.
  • pc.printf("%s", data) requires data to be a C-style string (aka null terminated char array). Make sure that data has enough room for the null char which has to be appended (unless the modbus message already contains such).

while (mbusSerial.readable())
    data[i++] = mbusSerial.getc();

if (i > 0)
    data[i] = '\0';  // terminate the char array with null char (to become a C-style string)
    pc.printf("%s\r\n", data);
  • Rather than using an image it's better to copy and paste your code here and mark it up as below. Tthen it's going to displayed as a formatted code:
your source code

Click on the Editing tips in the right-bottom corner for more details.

Accepted Answer

Thanks Zoltan, very helpful comments. The mbusSerial port was hard to read. I had to abandon the .readable to get data through. I will try your much more compact coding. I think I got lost in the string handling. Best regards, Per

posted by Per Olav Dypvik 31 Mar 2018