Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 7 months ago.
Cant read serial port
Hello,
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
7 years, 7 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 removepc.format(8, SerialBase::Even, 1);from your code.
- pc.printf("%s", data)requires- datato be a C-style string (aka null terminated char array). Make sure that- datahas 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:
<<code>> your source code <</code>>
Click on the Editing tips in the right-bottom corner for more details.
