[Serial port of Embedded Development Technology] serial port Host Program in Linux

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

# Include <stdio. h>/* standard input/output definition */# include <stdlib. h>/* standard function library definition */# include <unistd. h>/* UNIX standard function definition */# include <sys/types. h>/**/# include <sys/STAT. h>/**/# include <fcntl. h>/* file control definition */# include <termios. h>/* ppsix terminal control definition */# include <errno. h>/* Error Code definition * // *** @ brief sets the serial communication rate * @ Param FD type int opens the serial port file handle * @ Param Speed Type int serial port speed *@ return void */INT speed_arr [] = {b38400, b19200, b9600, b4800, b2400, b1200, b300, b38 400, b19200, b9600, b4800, b2400, b1200, b300,}; int name_arr [] = {38400,192 00, 9600,480 0, 2400,120 0, 300,384 00, 19200,960 0, 4800,240 0, 0, 1200,300 ,}; void set_speed (int fd, int speed) {int I; int status; struct termios OPT; tcgetattr (FD, & OPT); for (I = 0; I <sizeof (speed_arr)/sizeof (INT); I ++) {If (speed = name_arr [I]) {tcflush (FD, tcioflush); cfsetispeed (& OPT, speed_arr [I]); cfsetospe Ed (& OPT, speed_arr [I]); status = tcsetattr (FD, tcsanow, & OPT); If (status! = 0) perror ("tcsetattr fd1"); return;} tcflush (FD, tcioflush) ;}/ *** @ brief sets the serial data bit, stop bits and check bits * @ Param FD type int open serial port file handle ** @ Param databits type int data bits value is 7 or 8 ** @ Param stopbits type int Stop bits value is 1 or 2 ** @ Param parity type int verification type value: N, e, O, S */INT set_parity (int fd, int databits, int stopbits, int parity) {struct termios options; If (tcgetattr (FD, & options )! = 0) {perror ("setupserial 1"); Return (false);} options. c_cflag & = ~ Csize; Switch (databits)/* set the number of digits */{Case 7: options. c_cflag | = cs7; break; case 8: options. c_cflag | = cs8; break; default: fprintf (stderr, "unsupported data size/N"); Return (false);} switch (parity) {Case 'N ': case 'N': options. c_cflag & = ~ Parenb;/* clear parity enable */options. c_iflag & = ~ Inpck;/* enable parity checking */break; Case 'O': options. c_cflag | = (parodd | parenb);/* set it to an odd test */options. c_iflag | = inpck;/* disnable parity checking */break; Case 'E': options. c_cflag | = parenb;/* enable parity */options. c_cflag & = ~ Parodd;/* convert to an even test */options. c_iflag | = inpck;/* disnable parity checking */break; Case's ':/* as no parity */options. c_cflag & = ~ Parenb; options. c_cflag & = ~ Cstopb; break; default: fprintf (stderr, "unsupported parity/N"); Return (false) ;}/ * set the stop bit */switch (stopbits) {Case 1: options. c_cflag & = ~ Cstopb; break; Case 2: options. c_cflag | = cstopb; break; default: fprintf (stderr, "unsupported Stop bits/N"); Return (false);}/* set input parity option */If (parity! = 'N') options. c_iflag | = inpck; options. c_cc [vtime] = 150; // 15 seconds options. c_cc [Vmin] = 0; tcflush (FD, tciflush);/* update the options and do it now */If (tcsetattr (FD, tcsanow, & options )! = 0) {perror ("setupserial 3"); Return (false);} return (true );} /*** @ breif open the serial port */INT opendev (char * Dev) {int FD = open (Dev, o_rdwr ); // | o_noctty | o_ndelay if (-1 = FD) {/* set the number of data digits */perror ("can't open serial port"); Return-1 ;} else return FD;}/*** @ breif main () */INT main (INT argc, char ** argv) {int FD; int nread; char buff [512]; char * Dev = "/dev/ttys1"; FD = opendev (Dev); If (FD> 0) set_speed (FD, 19200); else {printf ("can't open serial port! /N "); exit (0);} If (set_parity (FD, 8, 1, 'n') = false) {printf (" set parity error/N "); exit (1);} while (1) {While (nread = read (FD, buff, 512)> 0) {printf ("/nlen % d/N ", nread); buff [nread + 1] = '/0'; printf ("/n % s", buff) ;}// close (FD ); // exit (0 );}

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.