0

I'm doing a sanity check with some write/ read data operation on serial port, on my raspi-3. My data are read nicely for the 2nd/3th time, but after that, the data are corrupted, as if there was a misalignement of data.

The idea here is to transmit data to my usb to ttl converter (mine is a ft232r breakout), then reading it back on the ttyUSB0.

So I write data to /dev/ttyS0 (as suggested) instead of /dev/ttyAMA0 because /dev/ttyAMA0 is already taken by the bluetooth module. The data is transmited to TX pin of raspi and get back to the RX pin on my ft232r, then read back on the ttyUSB0 file. When I launch my read code, it outputs the data nice the 2 / 3 third time. After that, I only get corrupted data. If I relaunch my read program, it work nice again, the 2/3 time, and corrupted data again.

Here my read.py code:

#!/usr/bin/env python


import time
import serial


ser = serial.Serial(
       port='/dev/ttyUSB0',
       baudrate = 9600,
       parity=serial.PARITY_NONE,
       stopbits=serial.STOPBITS_ONE,
       bytesize=serial.EIGHTBITS,
       timeout=1
       )

while True:
    for line in ser.read():
        print line

And my write.py:

 #!/usr/bin/env python


import time
import serial
import io

ser = serial.Serial(
       port='/dev/ttyS0',
       baudrate = 9600,
       parity=serial.PARITY_NONE,
       stopbits=serial.STOPBITS_ONE,
       bytesize=serial.EIGHTBITS,
       timeout=1
       )

while 1:
        ser.write('hello\n')
        time.sleep(1)

Now my output are:

hello-hello-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�-��p�nn6�

What is strange is that the first output are ok. So this is not a baud problem.But after that, my data is not encoded as expected, I always get the same ouput(��p�nn6�), so I don't know where is the problem.

I'm asking this question because I have bad soldered my rx / tx pin of my ft232r. And the tx pin is kind of floating. So I was wondering if it could be a hardware problem. But since I can get the first output nicely formed, I'm guessing this is not this case. Can someone help me understanding the problem? I'm totally lost. Thanks.

edit Thank to Milliway, it appears that it is a baudrate problem. Enabling uart in boot/config wasn't enought. You have to add this extra line at the end of /boot/config.txt:

enable_uart=1
core_freq=250

You have to add the rate the clock frequency of the raspi board. The solution is here: http://raspberrypi.stackexchange.com/questions/45570/how-do-i-make-serial-work-on-the-raspberry-pi3/45571#45571

1 Answer 1

1

Assuming you have disabled the serial console, this looks like you are using an old version of the firmware. This seems likely, as serial is disabled by default on the recent firmware. The clock rate changes dynamically affecting the serial baud rate. How-do-i-make-serial-work-on-the-raspberry-pi3

The new firmware requires serial to be specifically enabled, and fixes the clock rate. You can just fix the clock rate on older firmware or upgrade to latest.

1
  • Ok, thanks Milliways. You have pointed out the exact problem. I read lot of topic on how to enable the miniUART. But even if heard about baudrate problem, I didn't think it could affect that this way (I just assumed I only get trash value).
    – Mr Bonjour
    Commented Sep 15, 2016 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.