LINUX serial programming experience

Source: Internet
Author: User

LINUX serial programming experience

I have just completed a serial port project, and I have a little bit of experience. Let's talk about the experience during this time!

At the beginning, I searched for information on the Internet, which was translated by LINUX serial howto at most. Visit: http://linux.cis.nctu.edu.tw/chinese/how-to/serial-howto.html. As well as the selection of many versions, the content is similar.

Later I found that the essence of serial port programming is the setting of multiple serial ports.
The attributes are as follows:
C_cflag control options
C_lflag line options
C_iflag input options
C_oflag output options
C_cc control characters
C_ispeed input BAUD (New Interface)
C_ospeed output BAUD (New Interface)
The key is to understand which attribute parameters can be set and what they mean.

Continue to find information. I found the following classic article. It can be said that basically all the articles on serial programming are more or less referenced in this article. Serial programming guide for POSIX operating systems must be viewed, I am reading 5th edition, 3rd revision-updated March 11,200 3,: http://www.easysw.com /~ Mike/serial/index.html.

After reading this article, we can basically solve the problem of serial port settings. However, this is an English version. I plan to translate it into a Chinese version during the summer vacation.

Specific examples:
Http://www.comptechdoc.org/ OS /linux/programming/c/linux_pgcserial.html is good, very detailed, but more complex.
There are many Chinese translated versions of LINUX serial howto, which are also worth your reference.

The following is my program, which reads data from a serial port and writes data to the MySQL database:
# Include <sys/types. h> <br/> # include <sys/STAT. h> <br/> # include <fcntl. h> <br/> # include <termios. h> <br/> # include <errno. h> <br/> # include <ctype. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <time. h> </P> <p> # include "MySQL. H "</P> <p> # define baudrate b9600 <br/> # define device"/dev/ttys0 "<br/> # DEFINE _ posix_source 1 <br/> # define false 0 <br/> # define true 1 </P> <p> int insertdb (INT D1, int D2, int D3, int D4) {<br/> MySQL connect; <br/> int res, No1, NO2, sd1, SD2; <br/> char * query = "insert into mydata (stime, sno1, SNS, sdata1, sdata2) values ('% s', % d, % d, % x, % x) "; <br/> char * SQL, * ST1; <br/> struct TM * PTR; <br/> time_t lt; </P> <p> No1 = D1; <br/> NO2 = d2; <br/> sd1 = D3; <br/> SD2 = D4; <br/> lT = Time (null); <br/> PTR = localtime (<); <br /> ST1 = (char *) asctime (PTR); <br/> ST1 [strlen (ST1)-1] = '/0 '; <br/> SQL = (char *) malloc (255 * sizeof (char); <br/> sprintf (SQL, query, ST1, No1, NO2, sd1, SD2); </P> <p>/* debug here <br/> printf ("% C:", ST1 [strlen (ST1)]); <br/> printf ("% s: % d:", SQL, strlen (ST1); <br/> return exit_success; <br/> */</P> <p> mysql_init (& Connect); </P> <p> If (mysql_real_connect (& connect, "localhost ", "root", "root ", "Mytest", 0, null, 0) {<br/> printf ("Connect success! /N "); </P> <p> res = mysql_query (& connect, SQL); </P> <p> If (! Res) {<br/> printf ("insert success! /N "); <br/>}else {<br/> fprintf (stderr," insert error % d: % s/n ", mysql_errno (& Connect ), mysql_error (& Connect); <br/> return exit_failure; <br/>}</P> <p> mysql_close (& Connect ); <br/>} else {<br/> fprintf (stderr, "Connect fail! /N "); <br/> return exit_failure; <br/>}</P> <p> return exit_success; <br/>}</P> <p> int main (void) {<br/> int FD, res_w, res_r, I, J, K; <br/> struct termios oldtio, newtio; <br/> char inbuf [255]; <br/> char cbuf [4]; <br/> int Buf [4]; </P> <p> res_w = 0; <br/> res_r = 0; </P> <p> FD = open (device, o_rdwr | o_noctty ); // | o_ndelay); <br/> If (FD <0) {<br/> perror (device); <br/> exit (-1 ); <br/>}</P> <p> Tcgetattr (FD, & oldtio); </P> <p> bzero (& newtio, sizeof (struct termios); </P> <p> newtio. c_cflag | = (clocal | cread); <br/> newtio. c_cflag | = baudrate; <br/> newtio. c_cflag & = ~ Cstopb; <br/> newtio. c_cflag & = ~ Parenb; <br/> newtio. c_cflag & = ~ Csize; <br/> newtio. c_cflag | = cs8; <br/> newtio. c_cflag & = ~ Crtscts; </P> <p> newtio. c_lflag = 0; </P> <p> newtio. c_oflag = 0; </P> <p> newtio. c_cc [Vmin] = 4; <br/> newtio. c_cc [vtime] = 0; </P> <p> newtio. c_iflag & = ~ (Ixon | ixoff | ixany); </P> <p> cfsetispeed (& newtio, baudrate); <br/> cfsetospeed (& newtio, baudrate ); </P> <p> tcsetattr (FD, tcsanow, & newtio); </P> <p> tcflush (FD, tciflush ); </P> <p> cbuf [0] = 0x00; <br/> // cbuf [1] = 0x00; </P> <p> J = 0; </P> <p> for (k = 0; k <4; k ++) {<br/> switch (j) {<br/> case 0: <br/> default: <br/> cbuf [1] = 0x00; <br/> J = 2; <br/> break; <br/> case 2: <br/> cbuf [1] = 0x02; <br/> J = 0; <B R/> break; <br/>}</P> <p> res_w = write (FD, cbuf, 2 ); </P> <p>/* debug here <br/> printf ("cbuf: % x/N", cbuf [0], cbuf [1]); <br/> printf ("Buf: % x/N", inbuf [0], inbuf [1], inbuf [2], inbuf [3]); <br/> */<br/> res_r = read (FD, & inbuf, 255); </P> <p> If (res_r! =-1) {<br/> for (I = 0; I <res_r; I ++) {<br/> Buf [I] = (INT) inbuf [I]; <br/> Buf [I] = Buf [I] & 0xff; <br/> // printf ("% x", Buf [I]); <br/>}< br/> printf ("/N"); <br/> If (insertdb (BUF [0], Buf [1], Buf [2], buf [3]) <br/> printf ("insert into DB success! "); <Br/>}< br/> else {<br/> perror (" read fail "); <br/> exit (-1 ); <br/>}// if end here <br/>}// for end here </P> <p> tcsetattr (FD, tcsanow, & oldtio ); </P> <p> close (FD); <br/> exit (0); <br/>}// main end here

 

Link: http://space.itpub.net/22893636/viewspace-662999

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.