• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 45,  comments - 232,  trackbacks - 0

            Linux平臺上的串口通訊API很簡單,也很適用,使用C語言實(shí)現(xiàn)。我特別覺得它的讀數(shù)據(jù)函數(shù)很好用,因?yàn)槭褂昧?/span>select機(jī)制,可以很容易的做讀數(shù)據(jù)超時(shí)。代碼簡單,不說了,自己下載了看就明白了。

            /**

             * @file serialport.c

             * @brief serial port API implementation

             * @author Deng Yangjun

             * @date 2007-1-9

             * @version 0.1

             */

            #include       <stdio.h>        

            #include       <stdlib.h>

            #include       <sys/types.h>

            #include       <sys/stat.h>  

            #include       <fcntl.h>         

            #include       <termios.h>     

            #include       <errno.h>        

            #include       <sys/ioctl.h>      

            #include       <assert.h>         

             

            #include "serialport.h"

             

            #ifdef __cplusplus

            extern "C" {

            #endif

             

            #define MAX_DEV_NAME 256

             

            int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

                       B38400, B19200, B9600, B4800, B2400, B1200, B300, };

            int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,

                       38400,  19200,  9600, 4800, 2400, 1200,  300, };

                     

            /**

            * @brief  set speed of the serial port

            * @param  fd [in] file description of the serial port

            * @param  speed [in] speed that want to set value in speed array

            * @return error code

            */       

            int set_serial_port_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]);

                       cfsetospeed(&opt, speed_arr[i]);

                       status = tcsetattr(fd, TCSANOW, &opt);

                       if(status != 0)

                                      return status;

                               else

                                      return 0;

                        }

                         tcflush(fd,TCIOFLUSH);

                  }

              

               //invalid baud rate

               assert(FALSE);

            }

             

            /**

            * @brief  set serial port control flag

            * @param  fd [in] file description of the serial port

            * @param  databits [in] data bits, it's value is 7 or 8

            * @param  stopbits [in] stop bits, it's value is 1 or 2

            * @param  parity   [in] parity char, it's value is onef of 'N','E','O','S'

            * @return error code

            */

            int set_serial_port_ctrl_flag(int fd,int databits,int stopbits,int parity)

            {

                   struct termios options;

                   int result = tcgetattr(fd,&options);

                   if(result != 0){

                          return result;

                   }

                  

                   /*8N1*/

                  options.c_cflag &= ~CSIZE; /* Mask the character size bits */

                  switch (databits)

                  {

                         case 7:

                         options.c_cflag |= CS7;

                         break;

                         case 8:

                         options.c_cflag |= CS8;

                         break;

                         default:

                         assert(FALSE);

                         break;

                   }

                  

                   switch (parity)

                  {

                         case 'n':

                         case 'N':

                                options.c_cflag &= ~PARENB;   /* Clear parity enable */

                                 options.c_iflag &= ~INPCK;     /* Enable parity checking */

                                 break;

                          case 'o':

                          case 'O':

                                 options.c_cflag |= (PARODD | PARENB);  /* Set odd checking*/

                                 options.c_iflag |= INPCK;             /* Disnable parity checking */

                                 break;

                          case 'e':

                          case 'E':

                                        options.c_cflag |= PARENB;     /* Enable parity */

                                        options.c_cflag &= ~PARODD;   /* Set event checking*/ 

                                        options.c_iflag |= INPCK;       /* Disnable parity checking */

                                        break;

                          case 'S':

                          case 's':  /*as no parity*/

                                        options.c_cflag &= ~PARENB;

                                        options.c_cflag &= ~CSTOPB;

                                        break;

                          default:

                                        assert(FALSE);

                                        break;

                          }

                         

                   //set stop bits

                   switch (stopbits)

                  {

                         case 1:

                                options.c_cflag &= ~CSTOPB;

                                 break;

                          case 2:

                                 options.c_cflag |= CSTOPB;

                                 break;

                          default:

                                 assert(FALSE);

                                 break;

                   }

                  

              /* Set input parity option */

                   if (parity != 'n') {

                         options.c_iflag |= INPCK;

                  }

             

                  options.c_cc[VTIME] = 150; // 15 seconds

                   options.c_cc[VMIN] = 0;

                  

                   options.c_cflag &= ~CRTSCTS;//disable hardware flow control;

                   options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/

                   options.c_oflag  &= ~OPOST;   /*raw output*/

                  

                   tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */

                   result = tcsetattr(fd,TCSANOW,&options);

                   if ( result != 0)

                   {

                          return result;

                   }

                  

                   return 0;

             }

             

            /**

            * @breif open serial port device

            * @param dev [in] name of the device

            * @return file description of the device

            */

            static int open_dev(const char *dev)

            {

                   int   fd = open( dev, O_RDWR | O_NOCTTY | O_NDELAY );

                   if (fd != -1 ){

                          fcntl(fd, F_SETFL,0); //set to block

                   }

                   return fd;

            }

             

            /**

            * @breif open serial port

            * @param port_num [in] number of the serial port, from 0~255

            * @return file description of the serial port

            */

             int open_serial_port(int port_num)

             {

                   assert(port_num > -1 && port_num < 256);

                  

                   char dev[MAX_DEV_NAME]={0};

                   // dev/ttyS0 = serial port 0

                   sprintf(dev,"/dev/ttyS%d",port_num);

                   return open_dev(dev);

             }

             

             /**

              * @brief write serial port data

              * @param fd [in] file description of serial port

              * @param buffer [in] write data buffer

              * @param size [in] write data buffer size

              * @return write data count,return -1, occur error

              */

             int write_serial_port(int fd,unsigned char *buffer, size_t size)

             {

                   int writecount = write(fd,buffer,size);

                  

                   return writecount;

             }

             

            /**

            * @brief read N data from serial port in time out

            * @param    fd [in] file description of the serial port

            * @param    buffer [in] read data buffer

            * @param    size [in] read data buffer size

            * @param     readcount [in,out] at last read (readcount) size data.

            *                   set it equal to 0, read random count data.

            *                   it will return received data count.

            *                   return -1, the serial port closed

            *                   return N, count of the serial port received

            * @param     timeout [in] read N count data's time out

            * @return     return 0 : time out\n

            *                   return -1 : error\n

            *                   return N: received data count\n

            */

            int read_serial_port(int fd, unsigned char *buffer,size_t size, size_t *readcount,

                                                      struct timeval *timeout)

            {

                   assert(size >= *readcount);

                  

                   fd_set inputs;

                   int result;     //select result

                   int nread;            //totla read count

                   int iread;              //ont time read count

                   int maxfd;

                  

                   maxfd = fd + 1; //NOTE

                   nread = 0 ;  

                  

                  

                   FD_ZERO(&inputs);

                   FD_SET(fd, &inputs);

                  

                   do {

                          result = select(maxfd, &inputs,(fd_set *)NULL, (fd_set *)NULL, timeout);

                          switch(result)

                          {

                                 case 0: //time out

                                 case -1:        //error

                                 return result;

                                 default:

                                 if(FD_ISSET(fd,&inputs)) {

                                        ioctl(fd,FIONREAD,&iread);

                                        if(iread == 0) {

                                               *readcount = -1; //port closed

                                               return TRUE;

                                        }

                                        int count = 0;

                                        if(*readcount != 0) {

                                               //only care buffer size

                                               count = min(iread, size - nread);

                                        }

                                       

                                        nread += read(fd, buffer+nread, count);

                                

                                 }

                                 else {

                                        assert(FALSE);

                                 }

                          }//end of switch

                   }while((*readcount != 0) && (nread < *readcount));

                  

                   //receive random data, return data count

                   if( 0 == *readcount) {

                          *readcount = nread;

                   }

                   return TRUE;

            }

             

            /*

             * @brief close serial port

             * @param fd [in] file description of the serial port

             */

            void close_serial_port(int fd)

            {

                   close(fd);

            }

             

            #ifdef __cplusplus

            } /* extern "C" */

            #endif

            posted on 2007-08-24 09:31 天下無雙 閱讀(3030) 評論(2)  編輯 收藏 引用

            FeedBack:
            # re: 串口編程(3)--實(shí)現(xiàn)Linux、Unix串口通訊
            2008-07-11 16:54 | KC
            # re: 串口編程(3)--實(shí)現(xiàn)Linux、Unix串口通訊
            2013-07-13 16:56 | chekliang
            學(xué)習(xí)了,非常感謝!  回復(fù)  更多評論
              

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊

            我的其它領(lǐng)域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205789
            • 排名 - 130

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久婷婷六月丁香| 国产叼嘿久久精品久久| 久久久久国产精品人妻| 99蜜桃臀久久久欧美精品网站| 久久99久久99精品免视看动漫 | 久久婷婷色综合一区二区| 久久午夜福利无码1000合集| 久久无码人妻一区二区三区午夜| 久久精品九九亚洲精品| 久久综合狠狠色综合伊人| 久久久久久久国产免费看| 久久无码AV一区二区三区| 东京热TOKYO综合久久精品| 亚洲国产精久久久久久久| 伊人久久大香线蕉成人| 国产∨亚洲V天堂无码久久久| 99热成人精品免费久久| 精品国产乱码久久久久久呢| 久久se精品一区精品二区| 色偷偷91久久综合噜噜噜噜| 人妻无码αv中文字幕久久琪琪布| 色噜噜狠狠先锋影音久久| 亚洲国产日韩欧美久久| 久久久久99精品成人片试看 | 久久婷婷五月综合97色| 国产免费久久久久久无码| 亚洲国产精品久久久天堂| 国产精品美女久久久久AV福利| 中文字幕精品久久| 国产精品免费看久久久 | 久久夜色精品国产噜噜噜亚洲AV| 51久久夜色精品国产| 无码专区久久综合久中文字幕| 久久国产免费| 久久精品成人免费网站| 一本色道久久HEZYO无码| 久久婷婷五月综合色99啪ak| 国产99久久精品一区二区| 色妞色综合久久夜夜 | 久久婷婷激情综合色综合俺也去| 亚洲美日韩Av中文字幕无码久久久妻妇|