Raspberry Pi resources-using the UART

Source: Internet
Author: User

Reference: RPi Serial Connection

This article from: Http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port/using-the-uart

Using the UART

If you is running Raspbian or similar then the UART would 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 a-line as if you we ' re using a keyboard a  ND screens connected to it. However that's no use if your want to use the UART interface for your own application running on the RPi.

Turning off the UART functioning as a serial console

See here.

Using the UART in Your C Code

(This was based on the example code given here).

Headers Required
#include <stdio.h>#include <unistd.h>//Used for UART#include <fcntl.h>//Used for UART#include <termios.h>//Used for UART
Setting up the UART
-------------------------//-----SETUP USART 0-----//-------------------------//at bootup, pins 8 and ten are already SE  T to Uart0_txd, uart0_rxd (ie the alt0 function) respectivelyint Uart0_filestream = -1;//open the uart//the flags (defined In fcntl.h)://access modes (use 1 of the These)://o_rdonly-open for reading Only.//o_rdwr-open for reading and writing.// O_wronly-open for writing only.////o_ndelay/o_nonblock (same function)-Enables nonblocking mode. When set read requests on the file can return immediately with a failure status//if there is no input immediately availabl E (instead of blocking). Likewise, write requests can also return//immediately with a failure status if the output can ' t is written immediately./// /o_noctty-when set and path identifies a terminal device, open () shall not cause the terminal device to become the CONTR Olling terminal for the Process.uart0_filestream = open ("/dev/ttyama0", O_RDWR | O_noctty | O_ndelay);//open in non blocking read/write modeif (Uart0_filestream = =-1) {//error-can ' T OPEN SERIAL portprintf ("error-unable to open UART. Ensure it is not on use by another application\n ");} CONFIGURE the Uart//the flags (defined In/usr/include/termios.h-see http://pubs.opengroup.org/onlinepubs/007908799  /xsh/termios.h.html)://baud rate:-B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, b4000000//csize:-CS5, CS6, CS7, Cs8//clocal-ignore Modem status Lines//cread-enable receiver//ignpar = Ignore characters with parity errors//icrnl-m AP CR to NL in input (use for ASCII comms where want to auto correct end of line Characters-don ' t use for bianry com ms!) Parenb-parity enable//parodd-odd Parity (else even) struct Termios options;tcgetattr (Uart0_filestream, &options) ; Options.c_cflag = B9600 | CS8 | clocal | Cread;//<set baud rateoptions.c_iflag = Ignpar;options.c_oflag = 0;optionS.c_lflag = 0;tcflush (Uart0_filestream, Tciflush); Tcsetattr (Uart0_filestream, Tcsanow, &options); 
Transmitting Bytes
//----- TX BYTES -----unsigned char tx_buffer[20];unsigned char *p_tx_buffer;p_tx_buffer = &tx_buffer[0];*p_tx_buffer++ = ‘H‘;*p_tx_buffer++ = ‘e‘;*p_tx_buffer++ = ‘l‘;*p_tx_buffer++ = ‘l‘;*p_tx_buffer++ = ‘o‘;if (uart0_filestream != -1){int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));//Filestream, bytes to write, number of bytes to writeif (count < 0){printf("UART TX error\n");}}
Receiving Bytes

Because O_ndelay have been used this would exit if there is no receive bytes waiting (non blocking read), so if you want to Hold waiting for input simply put the A while loop

//----- CHECK FOR ANY RX BYTES -----if (uart0_filestream != -1){// Read up to 255 characters from the port if they are thereunsigned char rx_buffer[256];int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);//Filestream, buffer to store in, number of bytes to read (max)if (rx_length < 0){//An error occured (will occur if there are no bytes)}else if (rx_length == 0){//No data waiting}else{//Bytes receivedrx_buffer[rx_length] = ‘\0‘;printf("%i bytes read : %s\n", rx_length, rx_buffer);}}
Closing the UART if no longer needed
//----- CLOSE THE UART -----close(uart0_filestream);
Using minicom on the UART

Install minicom:

sudo apt-get install minicom

Running minicom:

minicom -b 115200 -o -D /dev/ttyAMA0

To test the UART are working you can simply link the TX and RX pins to each other and verify minicom receives what are you type .

Troubleshooting UART Problems

The above code works (we ' ve used it for TX and RX). If you can ' t get to work for your and you ' ve been through the steps to release the UART from being used for the console Try the following:


This command would set read and write access permissions for all users on the uart–it shouldn ' t is needed but can be used Just to be sure there are not a permissions problem:

sudo chmod a+rw /dev/ttyAMA0
Baud Rate Error

Try using a slower BAUD rate (or a single 0xFF byte which only have the start bit low) and if it works. We had a problem using 115K2 baud rate where we microcontroller communicating with the RPi could hits 113636baud or 119047  Baud. 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 is 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 in a scope we got 78uS, showing an actual BA  UD rate of 115384 from the RPi (8bits + the start bit all low). This is odd as 113636baud still had to lower error margin but that is the finding.

Is your over or under clocking the RPi? If so does you need to adjust the baud rate to compensate for this?

General UART Programming Resources


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.