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

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

            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊

            我的其它領域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205781
            • 排名 - 130

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久av无码免费网| 色综合合久久天天综合绕视看| 99久久精品无码一区二区毛片| 99久久精品免费看国产免费| 久久久噜噜噜久久中文字幕色伊伊| 欧美久久亚洲精品| 久久久免费精品re6| 国产精品成人99久久久久| 人妻少妇精品久久| 日韩久久久久久中文人妻 | 99久久99久久精品国产| 性做久久久久久久久浪潮| 国产一区二区精品久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久综合狠狠色综合伊人| 久久综合九色欧美综合狠狠| 久久精品一本到99热免费| 久久成人国产精品一区二区| 大香伊人久久精品一区二区| 久久午夜综合久久| 精品久久久久久中文字幕| A狠狠久久蜜臀婷色中文网| 99精品伊人久久久大香线蕉| 日本欧美久久久久免费播放网| 久久精品国产久精国产果冻传媒| 久久久久国产精品熟女影院| 亚洲国产精久久久久久久| 狠狠色噜噜色狠狠狠综合久久| 久久性精品| 亚洲精品久久久www| 国产成人香蕉久久久久| 免费精品99久久国产综合精品 | 久久夜色精品国产亚洲| 久久人人妻人人爽人人爽| 日产精品久久久久久久| 亚洲日韩中文无码久久| 久久久无码精品亚洲日韩蜜臀浪潮| 久久夜色撩人精品国产| 午夜视频久久久久一区 | 精品伊人久久大线蕉色首页| 久久青青草视频|