Linux serial programming based on Raspberry Pi to realize spontaneous self-collection

Source: Internet
Author: User

Serial port is a computer on a very general device communication protocol, commonly used PC contains RS232 specifications of the serial port, with a few connections, simple communication, has been widely used.
Linux access to all devices through the device file, the serial port is the same, in order to access the serial port, just open its device file can operate the serial device. Under the Linux system, each serial device has a device file associated with it, and the device file is located under the system's/dev directory. such as Linux under the/TTYS0,/TTYS1 respectively is the serial port 1 and serial 2.
Location of the Raspberry pi UART port: see GPIO14 (TXD), GPIO (RXD)
This article is based on the Raspberry Pi environment, the Raspberry Pi can use the serial port /dev/ttyama0
To use this serial port, you must first set:
1. Modify/boot/cmdline.txtEnter the following command: sudo nano/boot/cmdline.txt remove the red part: dwc_otg.lpm_enable=0console=ttyama0,115200 kgdboc=ttyama0,115200Console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait eventually becomes dwc_otg.lpm_enable=0 console=tty1 ROOT=/DEV/MMCBLK0P2 ROOTFSTYPE=EXT4 Elevator=deadline rootwait2. Modify /etc/inittab Enter the following instruction: sudo nano/etc/inittab comment out the last line:    #t0:23:respawn:/sbin/getty-l ttyAMA0 115200 vt100
OK, restart the next Raspberry Pi now can write their own program to test the serial port. I am the TXD and rxd short, realize spontaneous self-collection.
The procedure is as follows:
Recv.h
#ifndef _recv_h#define _recv_h#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include < fcntl.h> #include <termios.h> #define baudrate B115200///baud rate:115200#define DEVICE "/dev/ttyama0" #define SIZE 1024#endif

Recv.c
#include "Recv.h" int nFd = 0;struct Termios stnew;struct termios stold;//open Port & Set portint serialinit () {nFd = op En (DEVICE, o_rdwr| o_noctty| O_ndelay); if ( -1 = = nFd) {perror ("Open Serial Port error!\n"); return-1;} if ((Fcntl (nFd, F_SETFL, 0) < 0) {perror ("Fcntl F_SETFL error!\n"); return-1;} if (Tcgetattr (nFd, &stold)! = 0) {perror ("tcgetattr error!\n"); return-1;} Stnew = Stold;cfmakeraw (&stnew);//sets the terminal to the original mode, all input data in this mode is processed in bytes//set speedcfsetispeed (&stnew, baudrate) ;//115200cfsetospeed (&stnew, baudrate);//set databits Stnew.c_cflag |= (clocal|      Cread); Stnew.c_cflag &= ~csize;stnew.c_cflag |= cs8;//set parity stnew.c_cflag &= ~parenb; Stnew.c_iflag &= ~inpck;//set stopbits stnew.c_cflag &= ~cstopb;stnew.c_cc[vtime]=0;//Specifies the minimum number of characters to read Stnew.c_c c[vmin]=1;//Specifies the wait time for the first character to be read, the unit of time is n*100ms//if the vtime=0 is set, then no character is entered when the read () operation is blocked indefinitely Tcflush (Nfd,tciflush);//empty terminal unfinished input/    Output requests and data. if (tcsetattr (nfd,tcsanow,&stnew)! = 0) {perror ("tcsetattr error!\n"); return-1;} return nFd;} int main (int argc, char **argv) {int nret = 0;char buf[size];if (serialinit () = =-1) {perror ("Serialinit error!\n"); return -1;} Bzero (buf, SIZE), while (1) {nret = Read (NFd, buf, size), if ( -1 = = nret) {perror ("read Data error!\n"); if (0 < Nret) {Buf[nret] = 0;printf ("Recv Data:%s\n", buf);}} Close (nFd); return 0;}

The above is to accept the program, you can accept the string information and print, the sender is the same as above, as long as the read to write on the line, here is not recorded.
Program Run Result:
Reference Documentation:
http://blog.csdn.net/leaglave_jyan/article/details/6656389
Http://www.ibm.com/developerworks/cn/linux/l-serials/index.html
 

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.