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 天下无双 阅读(3002) 评论(2)  编辑 收藏 引用

FeedBack:
# re: 串口编程(3)--实现Linux、Unix串口通讯
2008-07-11 16:54 | KC
.  回复  更多评论
  
# re: 串口编程(3)--实现Linux、Unix串口通讯
2013-07-13 16:56 | chekliang
学习了,非常感谢!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



常用链接

留言簿(15)

随笔分类

随笔档案

相册

我的其它领域Blog

搜索

  •  

积分与排名

  • 积分 - 202566
  • 排名 - 128

最新评论

阅读排行榜

评论排行榜