chipace
Posts: 41
Joined: Sat Jun 29, 2019 2:56 am

raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Sun Jul 07, 2019 10:06 pm

I am using python serial on the latest raspbian buster and have noticed that my uart script does not run properly on my new RPi4. I take the SD card out and plug it into my RP3B+ and it works as expected.
When I test the RPi4 uart in loopback, it performs as expected. When I use a USB uart to talk with it, the serial stream is corrupted. The same USB uart works just fine on the RPi3B+.

The only difference is the RPi4 and RPi3B+.
Any ideas as to what could be happening would be appreciated.

Code: Select all

#!/usr/bin/python

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser_0 = serial.Serial(
#	port='/dev/ttyUSB0',
	port='/dev/ttyS0',
	baudrate=115200,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.SEVENBITS,
	xonxoff=False,
	rtscts=False,
	dsrdtr=False
)

ser_1 = serial.Serial(
	port='/dev/ttyUSB0',
#	port='/dev/ttyS0',
	baudrate=115200,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.SEVENBITS,
	xonxoff=False,
	rtscts=False,
	dsrdtr=False
)

ser_0.isOpen()
ser_0.reset_input_buffer()
ser_0.reset_output_buffer()
ser_1.isOpen()
ser_1.reset_input_buffer()
ser_1.reset_output_buffer()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
    # get keyboard input
    input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
    if input == 'exit':
        ser_0.close()
        ser_1.close()
        exit()
    else:
        # send the character to the device
	ser_0.write(input)
        out = ''
        # let's wait one second before reading output (let's give device time to answer)
        time.sleep(1)
        while ser_1.inWaiting() > 0:
            out += ser_1.read(1)

        if out != '':
            print "ser_1 rx >>" + out
	
	    ser_1.write(input)
            out = ''
            # let's wait one second before reading output (let's give device time to answer)
            time.sleep(1)
            while ser_0.inWaiting() > 0:
                out += ser_0.read(1)

            if out != '':
                print "ser_0 rx >>" + out

Kai-wob
Posts: 2
Joined: Sun Jul 07, 2019 11:12 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Sun Jul 07, 2019 11:21 pm

My older C program work again on pi4 when I set the double baudrate as needed. Seems like a bug with the clock source of the uart I use ttyS0.
I found out by looking at my. All bit timings were x2 long then set.

chipace
Posts: 41
Joined: Sat Jun 29, 2019 2:56 am

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Mon Jul 08, 2019 2:16 am

Kai-wob wrote:
Sun Jul 07, 2019 11:21 pm
My older C program work again on pi4 when I set the double baudrate as needed. Seems like a bug with the clock source of the uart I use ttyS0.
I found out by looking at my. All bit timings were x2 long then set.
Thank you Kai-wob! Doubling the ttyS0 baudrate fixed my python script.

chipace
Posts: 41
Joined: Sat Jun 29, 2019 2:56 am

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Tue Jul 09, 2019 12:41 am

Firmware update ("sudo rpi-update") has fixed this issue. I no longer need to specify twice the baud rate for ttyS0.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2226
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Tue Jul 09, 2019 7:55 am

That's good to hear. We've been through a number of clocking changes with the 4B, but things are settling down now.

Kai-wob
Posts: 2
Joined: Sun Jul 07, 2019 11:12 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu Jul 11, 2019 10:27 pm

Yes, fixed now by the last update/upgrade.

Return to “Device Tree”