![](/uploads/1/2/6/5/126584940/638402460.jpg)
If you are running Raspbian or similar then the UART will be used as a serial console. Using a suitable cable, such as the TTL-232R-3V3-WE, you can connect it to your PC and using some simple terminal software set to 115200-8-N-1 use the command line interface to the Raspberry Pi in the same way as if you we’re using a keyboard and screen connected to it. However that’s no use if you want to use the UART interface for your own application running on the RPi.
Serial Port, Port Communication, Read data from port, Visual C Samples. The serial data, so I encapsulated this serial communication class with a thread to. E-XD CAD Drawing and Printing Solution Source Code Solution for C/C,. I have a sensor connected to an ODROID UART (Basically an RPi just a different name for the UART port) and using the WiringPi Serial library. I am able to echo/cat to the sensor via two terminal windows and read it's data but using my C code I cannot. The sensor requires me to send it a ' r' to send back data.
Turning off the UART functioning as a serial console
See here.
Using The UART In Your C Code
(This is based on the example code given here).
Headers required
Setting Up The UART
Transmitting Bytes
Transmit string function
Receiving Bytes
Because O_NDELAY has been used this will exit if there are no receive bytes waiting (non blocking read), so if you want to hold waiting for input simply put this in a while loop
Closing the UART if no longer needed
Using minicom on the UART
Install minicom:
Running minicom:
![Communication Communication](http://www.sharetechnote.com/image/Arduino_Serial_01.png)
To test the UART is working you can simply link the TX and RX pins to each other and verify minicom receives what you type.
Troubleshooting UART Problems
The above code works (we’ve used it for TX and RX). If you can’t get to to work for you and you’ve been through the steps to release the UART from being used for the console try the following:
UART Name
The SoCs used on the Raspberry Pis have two built-in UARTs, a PL011 and a mini UART.
/dev/serial0 is a symlink which always refers to the primary UART (if enabled). This is the UART assigned to the Linux console (which depends on the Raspberry Pi model).
/dev/serial1 is a symlink which always refers to the secondary UART (if enabled).
/dev/ttyS0 refers to the mini UART (Primary UART on RPi’s with wireless/Bluetooth module, e.g. RPi3, RPi Zero W)
/dev/ttyAMA0 refers to the PL011 (Primary UART on RPi’s without wireless/Bluetooth module)
Permissions
This command will set read and write access permissions for all users on the UART – it shouldn’t be needed but can be used just to be sure there is not a permissions problem:
Baud Rate Error
Try using a slower BAUD rate (or a single 0xFF byte which only has the start bit low) and see if it works. We had a problem using 115k2 baud rate where our microcontroller communicating with the RPi could hit 113636baud or 119047baud. 113636baud had the lowest error margin so we used it and TX from the RPi being received by the microcontroller worked fine. However when transmitting to the RPi nothing was ever received. Changing the microcontroller to use 119047baud caused RX to work. We then tested the RPi transmitting a byte of 0x00 and measured the low state on a scope we got 78uS, showing an actual baud rate of 115384 from the RPi (8bits + the start bit all low). This was odd as 113636baud still had to lower error margin but that was the finding.
Are you over or under clocking the RPi? If so do you need to adjust the baud rate to compensate for this?
General UART Programming Resources
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.
![](/uploads/1/2/6/5/126584940/638402460.jpg)