关于最近串口编程的一个简单总结

      前一阵忙于一个linux串口编程的小项目,一开始一窍不通,网上搜了不少资料,慢慢的啃,终于也算入门了。

这里介绍一下入门资料,Serial Programming Guide forPOSIX Operating Systems ,很不错,非常详尽!

还有 Linux串口编程HOWTO中英文简体对照beta版 也不错,基本上把上面两个资料搞通就基本OK了

     下面是我网上down下来的一段测试代码:

 

#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <asm/ioctl.h>

/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
#define FALSE  -1
#define TRUE   0

int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
                          B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400,  19200,  9600, 4800, 2400, 1200,  300,  38400,  19200,  9600, 4800, 2400, 1200,  300, };
     
void set_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)

                     {      
                          perror("tcsetattr fd1");
                          return;   
                     }  
                     tcflush(fd,TCIOFLUSH); 
               }
          }
     }


int set_Parity(int fd,int databits,int stopbits,int parity)
    {
          struct termios options;
          if  ( tcgetattr( fd,&options)  !=  0)

      {
               perror("SetupSerial 1");   
               return(FALSE);
          }
          options.c_cflag &= ~CSIZE;
          switch (databits)
          { 
               case 7:
                     options.c_cflag |= CS7;
                     break;
               case 8:   
                    options.c_cflag |= CS8;
                    break; 
               default:  
                    fprintf(stderr,"Unsupported data size ");

                return (FALSE);
           }
           switch (parity)
           { 
                case 'n':
                case 'N':  
                     options.c_cflag &= ~PARENB; 
                     options.c_iflag &= ~INPCK;   
                     break;
                case 'o': 
                case 'O':   
                     options.c_cflag |= (PARODD | PARENB); 
                     options.c_iflag |= INPCK;           
                     break;
                case 'e':
                case 'E': 
                     options.c_cflag |= PARENB;       
                     options.c_cflag &= ~PARODD;      
                     options.c_iflag |= INPCK;     
                     break;
                case 'S':
                case 's':   
                     options.c_cflag &= ~PARENB;
                     options.c_cflag &= ~CSTOPB;break;
                default: 
                     fprintf(stderr,"Unsupported parity ");  
                     return (FALSE);
           }
 
          switch (stopbits)
          { 
              case 1:  
                    options.c_cflag &= ~CSTOPB;
                    break;
              case 2:  
                    options.c_cflag |= CSTOPB;
                    break;
              default:  
                    fprintf(stderr,"Unsupported stop bits ");
                    return (FALSE);
          }

          if (parity != 'n') 
                  options.c_iflag |= INPCK;
           tcflush(fd,TCIFLUSH);
           options.c_cc[VTIME] = 0;//只要有数据就可以读  
           options.c_cc[VMIN] = 0;
           if (tcsetattr(fd,TCSANOW,&options) != 0) 
           {
                 perror("SetupSerial 3"); 
                 return (FALSE);
           }
           return (TRUE);
      }

 


int OpenDev(char *Dev)
{
    struct termios ti; 
    int i;
    int fd = open( Dev, O_RDWR|O_NOCTTY );
        //| O_NOCTTY | O_NDELAY
    if (-1 == fd)
    {  
        perror("Can't Open Serial Port");
        return -1;
    }
    tcflush(fd, TCIOFLUSH);
    if (tcgetattr(fd, &ti) < 0)
    {
  perror("Can't get port settings");
  return -1;
    }
    cfmakeraw(&ti);                //设置传输原始数据,而不是字符

    ti.c_cflag |= CLOCAL|CREAD;
    ti.c_cflag |= CRTSCTS;
   
/* 
 if (u->flags & FLOW_CTL)
  ti.c_cflag |= CRTSCTS;
 else
  ti.c_cflag &= ~CRTSCTS;
*/
   
    if (tcsetattr(fd, TCSANOW, &ti) < 0) {
  perror("Can't set port settings");
  return -1;
 }
   
    set_speed(fd,115200);
    i = 15;
/*  
   if (ioctl(fd, 0x5423, &i) < 0) {
  perror("Can't set line discipline");
  return -1;
 }
*/
/*
 if (ioctl(fd, HCIUARTSETPROTO, 0) < 0) {
  perror("Can't set device");
  return -1;
 } 
  */   
  
    return fd; 
  
}
int main(int argc, char **argv)
{
    int fd;
    int nread;
    int size=0;
    char buff[512]={"sosladfjlkasdfadsfjjjjjjjjjjjj"};
    char *dev  = "/dev/ttyUSB0"; //usb转串口

   
    fd = OpenDev(dev);
    set_speed(fd,115200);
    if (set_Parity(fd,8,1,'N') == FALSE)
    {
        printf("Set Parity Error ");
        exit (0);
    }

     tcflush(fd, TCIOFLUSH); 
    
     size = write(fd, buff, 512);

     printf("size = %d/n", size); 
     tcflush(fd, TCIOFLUSH);  
 
     close(fd);
}

 

刚开始调试代码总是读不到数据,后来总结有以下几个原因:

1.没有设置读写原始数据------------------------------->  cfmakeraw(&ti);

2.options.c_cc[VTIME] ,     options.c_cc[VMIN] 的设置也很重要,具体意思参考Serial Programming Guide forPOSIX Operating Systems ,23页,这里有详细说明。

3.还有就是要保证硬件连接正确!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值