• <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語言實現。我特別覺得它的讀數據函數很好用,因為使用了select機制,可以很容易的做讀數據超時。代碼簡單,不說了,自己下載了看就明白了。

            /**

             * @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 天下無雙 閱讀(3028) 評論(2)  編輯 收藏 引用

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

            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊

            我的其它領域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205724
            • 排名 - 130

            最新評論

            閱讀排行榜

            評論排行榜

            欧美午夜A∨大片久久 | 91精品国产91久久久久久蜜臀| 久久大香香蕉国产| 久久精品国产亚洲一区二区| 亚洲午夜久久影院| 理论片午午伦夜理片久久| 久久久久亚洲精品天堂久久久久久| 久久亚洲av无码精品浪潮| 一级a性色生活片久久无少妇一级婬片免费放 | 国产叼嘿久久精品久久| 香蕉久久夜色精品国产尤物| 久久久亚洲裙底偷窥综合| 久久99精品久久只有精品| 九九久久精品国产| 天天影视色香欲综合久久| 久久精品黄AA片一区二区三区| 亚洲国产成人久久综合一| 思思久久好好热精品国产| 国产精品久久久久jk制服| 久久午夜福利电影| 波多野结衣中文字幕久久 | 中文字幕乱码久久午夜| 久久免费线看线看| 综合人妻久久一区二区精品| 久久综合狠狠综合久久激情 | 韩国三级中文字幕hd久久精品 | 久久99精品久久久久久齐齐| 久久婷婷五月综合97色直播 | 久久免费的精品国产V∧| 久久久久国产精品麻豆AR影院| 伊人久久无码中文字幕| 久久久久综合国产欧美一区二区| 久久丫精品国产亚洲av| 无码任你躁久久久久久| 色综合久久中文色婷婷| 亚洲欧美伊人久久综合一区二区 | 日韩十八禁一区二区久久| 国产精品久久自在自线观看| 久久中文字幕人妻丝袜| 久久播电影网| 亚洲综合精品香蕉久久网97|