hamidrm
Posts: 10
Joined: Fri Aug 11, 2017 10:27 pm

UART read problem

Sat Jul 14, 2018 9:13 am

Hi ,
I'm using rpi3 and trying to build a lightweight and functional Linux Distro by 'BuildRoot'.
I wrote a daemon service that must use UART.
I can send data over UART but unable to receive!
I want to use `read()` in blocking mode but never receives any byte and blocks program forever.
this is my simplified code :

Code: Select all

....
	uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY);		//Open in blocking read/write mode
	if (uart0_filestream == -1)
	{
		printf("Error - Unable to open ttyS0!\n");
	}
	struct termios options;
	tcgetattr(uart0_filestream, &options);

	cfsetspeed(&options, B115200);
	cfmakeraw(&options);
	options.c_cflag &= ~CSTOPB;
	options.c_cflag |= CLOCAL;
	options.c_cflag |= CREAD;
	options.c_cflag |= CS8;
	
	options.c_cc[VTIME]=0;
	options.c_cc[VMIN]=1;
	
	tcsetattr(uart0_filestream, TCSANOW, &options);
	write(uart0_filestream, data, len); // Works without problem.
	printf("Program stuck here until a byte received...");
	read(uart0_filestream, (void*)&recv_byte, 1);
	printf("A byte received! %c",recv_byte); //Never reaches here!
config.txt :

Code: Select all

...
dtoverlay=pi3-miniuart-bt
enable_uart=1
cmdline.txt :

Code: Select all

root=/dev/mmcblk0p2 rootwait console=tty1
I'm using 'CP2102' to communication RPI and PC , And the kernel version is https://github.com/raspberrypi/linux/re ... 20170811-1
Where is my problem?
Thanks in advance.

KnarfB
Posts: 194
Joined: Wed Dec 14, 2016 10:47 am
Location: Germany

Re: UART read problem

Sun Jul 15, 2018 7:59 pm

What software (terminal program) are you using at the PC side for testing?
Make sure that flow control is disabled at the PC side software.
You could shortcut RX and TX pins at the Raspi and check if you get an echo if you write something.

User avatar
bensimmo
Posts: 3502
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: UART read problem

Sun Jul 15, 2018 9:09 pm

If you disable Bluetooth, then if I remember correctly, you are now using the ttyAMA0 (full UART) and not S0 (miniUART)


Either way you should

DirkS
Posts: 9524
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: UART read problem

Mon Jul 16, 2018 8:39 am

bensimmo wrote:
Sun Jul 15, 2018 9:09 pm
If you disable Bluetooth, then if I remember correctly, you are now using the ttyAMA0 (full UART) and not S0 (miniUART)


Either way you should
You should what?

Where you going to say: you should use /dev/serial0 ?

hamidrm
Posts: 10
Joined: Fri Aug 11, 2017 10:27 pm

Re: UART read problem

Mon Jul 16, 2018 8:58 am

What software (terminal program) are you using at the PC side for testing?
Make sure that flow control is disabled at the PC side software.
I’m using my own python code :

Code: Select all

import serial
import threading
import sys
from time import sleep

baudrate_list = {
	1: 1200,
	2: 2400,
	3: 4800,
	4: 9600,
	5: 19200,
	6: 38400,
	7: 57600,
	8: 115200,
}

serial_port = serial.Serial()




def th_read():
    while True:
        data = serial_port.read();
	if data:
            sys.stdout.write(data)
            sys.stdout.flush()

def main():
    name = raw_input("Python Serial Sniffer\nEnter Device Name :")
    print("Select Baudrate :\n[1]. 1200\n[2]. 2400\n[3]. 4800\n[4]. 9600\n[5]. 19200\n[6]. 38400\n[7]. 57600\n[8]. 115200\n")
    while True:
        baudrate_no = input("Enter Number : ")
        if baudrate_no > 0 and baudrate_no < 9:
            break
    serial_port.baudrate = baudrate_list.get(baudrate_no, 115200)
    serial_port.port = "/dev/"+name
    serial_port.timeout = 1
    if serial_port.isOpen(): serial_port.close()
    serial_port.open()
    t1 = threading.Thread(target=th_read, args=())
    t1.start()
    while True:
        try:
            command = raw_input()
            serial_port.write(command)
        except KeyboardInterrupt:
            break
    serial_port.close()

if __name__ == '__main__':
    main()
You could shortcut RX and TX pins at the Raspi and check if you get an echo if you write something.
I did it just now. but nothing received :cry:
Also tried shortcut TX and RX pins in PC side (CP2102) and it worked well (I could get echo of the sent text).

I can't understand how it is possible that sending works well and I can receive data correctly in PC side , but can't receive anything in RPI side...
Is it possible that RX pin driver in SoC has been damaged or broken?

User avatar
bensimmo
Posts: 3502
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: UART read problem

Mon Jul 16, 2018 9:19 am

DirkS wrote:
Mon Jul 16, 2018 8:39 am
bensimmo wrote:
Sun Jul 15, 2018 9:09 pm
If you disable Bluetooth, then if I remember correctly, you are now using the ttyAMA0 (full UART) and not S0 (miniUART)


Either way you should
You should what?

Where you going to say: you should use /dev/serial0 ?
I can't remember, lol, it might well have been but it's not on Raspbian so wouldn't work would it? That and the need to disable the system service too.
Again as its a custom build, no idea if that would be needed.

But more here https://www.raspberrypi.org/documentati ... on/uart.md
Last edited by bensimmo on Mon Jul 16, 2018 9:24 am, edited 1 time in total.

hamidrm
Posts: 10
Joined: Fri Aug 11, 2017 10:27 pm

Re: UART read problem

Mon Jul 16, 2018 9:21 am

If you disable Bluetooth, then if I remember correctly, you are now using the ttyAMA0 (full UART) and not S0 (miniUART)


Either way you should
I followed this guide : https://www.i-programmer.info/programmi ... ml?start=1

I also enabled getty on ttyS0 in /etc/inittab just now , but the receive problem exists also here and I could not login :

Code: Select all


Welcome to My Operating System
TestOS login: root
1234
ls
pwd


B.Goode
Posts: 6544
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: UART read problem

Mon Jul 16, 2018 9:23 am

I can't understand how it is possible that sending works well and I can receive data correctly in PC side , but can't receive anything in RPI side...
Is it possible that RX pin driver in SoC has been damaged or broken?
It isn't clear to me how/where the cp2102 convertor is being deployed.

If you are using the gpio pins on the 40-way Rpi header designated for use as a UART, have you taken into account the fact that they remain 3.3v limited gpio pins, and thus are not compatible with 'balanced' or 'bipolar' rs232 signalling? So, yes, applying a standard rs232 input to the gpio pin could cause fatal damage.

Raspberry Pi Foundation documentation is here: https://www.raspberrypi.org/documentati ... on/uart.md

hamidrm
Posts: 10
Joined: Fri Aug 11, 2017 10:27 pm

Re: UART read problem

Mon Jul 16, 2018 10:55 am

It isn't clear to me how/where the cp2102 convertor is being deployed.

If you are using the gpio pins on the 40-way Rpi header designated for use as a UART, have you taken into account the fact that they remain 3.3v limited gpio pins, and thus are not compatible with 'balanced' or 'bipolar' rs232 signalling? So, yes, applying a standard rs232 input to the gpio pin could cause fatal damage.

Raspberry Pi Foundation documentation is here: https://www.raspberrypi.org/documentati ... on/uart.md
Yes, I had paid attention to this.
I'm using http://hobbycomponents.com/usb-interfac ... le-adaptor as interface that its chip is a CP2102 that works at 3.3V.
The module has a silicon labs CP2102 based device and a convenient pin header which includes a 5V and 3.3V supply. Tx and Rx data pins are at 3.3V TTL levels.

User avatar
bensimmo
Posts: 3502
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: UART read problem

Mon Jul 16, 2018 4:25 pm

hamidrm wrote:
Mon Jul 16, 2018 9:21 am
If you disable Bluetooth, then if I remember correctly, you are now using the ttyAMA0 (full UART) and not S0 (miniUART)


Either way you should
I followed this guide : https://www.i-programmer.info/programmi ... ml?start=1

I also enabled getty on ttyS0 in /etc/inittab just now , but the receive problem exists also here and I could not login :

Code: Select all


Welcome to My Operating System
TestOS login: root
1234
ls
pwd

That's quite an old guide (2 years) but things have not changed much :-)

your config.sys above show
dtoverlay=pi3-miniuart-bt
enable_uart=1


So you start at this
Due to the Pi 3's support for Bluetooth the full serial interface is now used by the built-in Bluetooth device and the console uses the mini-UART
and
On a Pi 3 there are two links:
serial0 -> ttyS0
serial1 -> ttyAMA0
I don't know if you have the links as you say you have "using rpi3 and trying to build a lightweight and functional Linux Distro by 'BuildRoot'."
But either way
On a Pi 3 there are two links:
UART -> ttyS0
BlueTooth -> ttyAMA0


but as you have added
dtoverlay=pi3-miniuart-bt
and this part further in the tutorial happens
To swap the use of the UARTs you have to edit boot/config.txt
sudo nano /boot/config.txt
and add the line
dtoverlay=pi3-miniuart-bt
Save and reboot and on a Pi 3 the two serial ports will be redefined as
serial0 -> ttyAMA0
serial1 -> ttyS0
so now we actually have
UART -> ttyAMA0
BlueTooth -> ttyS0


Your code seems to be on ttyS0 when it should now be on ttyAMA0
uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY);


That's about as much as I know, I don't know why and how you are only talking in one direction.
Perhaps put Raspbain Lite on there, set it up and get it working first before you try it on your own setup.
use
/dev/serial0 for UART once you have enabled the port and (I assume you still need to disable the console, both these can be done in raspi-config under serial interface option)
Don't disable Bluetooth at the start unless you find you're getting dodgy characters coming through.

Return to “Interfacing (DSI, CSI, I2C, etc.)”