Chinaunix首页 | 论坛 | 博客
  • 博客访问: 240893
  • 博文数量: 131
  • 博客积分: 259
  • 博客等级: 二等列兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 21:15
文章分类

全部博文(131)

文章存档

2013年(3)

2011年(128)

分类:

2011-03-26 23:29:18

原文地址:linux 串口编程 作者:yuweixian4230

在这里 附上linux 串口编程的 代码,仅供自己学习查找



  1. #include<termios>
  2. struct termios
  3. {
  4.    unsigned short c_iflag; 输入模式标志
  5.    unsigned short c_oflag; 输出模式标志
  6.    unsigned short c_cflag; 控制模式标志
  7.    unsigned short c_lflag; 本地模式标志
  8.    unsigned char c_line; 线路规程
  9.    unsigned char c_cc[NCC]; 控制特征
  10.    speed_t c_ispeed; 输入速度
  11.    speed_ c_ospeed; 输出速度
  12. }



  1. /* uart_api.h */
  2. /* 串口配置函数 */
  3. int set_com_config(int fd,int baud_rate,
  4.                     int data_bits, char parity, int stop_bits)
  5. {
  6.     struct termios new_cfg,old_cfg;
  7.     int speed;

  8.     /*保存并测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
  9.     if (tcgetattr(fd, &old_cfg) != 0)
  10.     {
  11.         perror("tcgetattr");
  12.         return -1;
  13.     }
  14.     new_cfg = old_cfg;
  15.     cfmakeraw(&new_cfg); /* 配置为原始模式 */
  16.     new_cfg.c_cflag &= ~CSIZE;
  17.     /*设置波特率*/
  18.       switch (baud_rate)
  19.       {
  20.           case 2400:
  21.         {
  22.             speed = B2400;
  23.         }
  24.         break;
  25.           case 4800:
  26.         {
  27.             speed = B4800;
  28.         }
  29.         break;
  30.           case 9600:
  31.         {
  32.             speed = B9600;
  33.         }
  34.         break;
  35.           case 19200:
  36.         {
  37.             speed = B19200;
  38.         }
  39.         break;
  40.           case 38400:
  41.         {
  42.             speed = B38400;
  43.         }
  44.         break;

  45.         default:
  46.         case 115200:
  47.         {
  48.             speed = B115200;
  49.         }
  50.         break;
  51.       }
  52.     cfsetispeed(&new_cfg, speed);
  53.     cfsetospeed(&new_cfg, speed);
  54.     
  55.     switch (data_bits) /*设置数据位*/
  56.     {
  57.         case 7:
  58.         {
  59.             new_cfg.c_cflag |= CS7;
  60.         }
  61.         break;

  62.         default:
  63.         case 8:
  64.         {
  65.             new_cfg.c_cflag |= CS8;
  66.         }
  67.         break;
  68.       }
  69.       
  70.       switch (parity) /*设置奇偶校验位*/
  71.       {
  72.         default:
  73.         case 'n':
  74.         case 'N':
  75.         {
  76.             new_cfg.c_cflag &= ~PARENB;
  77.             new_cfg.c_iflag &= ~INPCK;
  78.         }
  79.         break;

  80.         case 'o':
  81.         case 'O':
  82.         {
  83.             new_cfg.c_cflag |= (PARODD | PARENB);
  84.             new_cfg.c_iflag |= INPCK;
  85.         }
  86.         break;

  87.         case 'e':
  88.         case 'E':
  89.         {
  90.             new_cfg.c_cflag |= PARENB;
  91.             new_cfg.c_cflag &= ~PARODD;
  92.             new_cfg.c_iflag |= INPCK;
  93.         }
  94.         break;

  95.         case 's': /*as no parity*/
  96.         case 'S':
  97.         {
  98.             new_cfg.c_cflag &= ~PARENB;
  99.             new_cfg.c_cflag &= ~CSTOPB;
  100.         }
  101.         break;
  102.     }
  103.         
  104.     switch (stop_bits) /*设置停止位*/
  105.     {
  106.         default:
  107.         case 1:
  108.         {
  109.             new_cfg.c_cflag &= ~CSTOPB;
  110.         }
  111.         break;

  112.         case 2:
  113.         {
  114.             new_cfg.c_cflag |= CSTOPB;
  115.         }
  116.     }
  117.     
  118.     /*设置等待时间和最小接收字符*/
  119.     new_cfg.c_cc[VTIME] = 0;
  120.     new_cfg.c_cc[VMIN] = 1;
  121.     tcflush(fd, TCIFLUSH); /*处理未接收字符*/
  122.     if ((tcsetattr(fd, TCSANOW, &new_cfg)) != 0) /*激活新配置*/
  123.     {
  124.         perror("tcsetattr");
  125.         return -1;
  126.     }    
  127.     return 0;
  128. }

  129. /*打开串口函数*/
  130. int open_port(int com_port)
  131. {
  132.     int fd;
  133. #if (COM_TYPE == GNR_COM) /* 使用普通串口 */
  134.     char *dev[] = {"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2"};
  135. #else /* 使用USB转串口 */
  136.     char *dev[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2"};
  137. #endif
  138.     if ((com_port < 0) || (com_port > MAX_COM_NUM))
  139.     {
  140.         return -1;
  141.     }
  142.     /* 打开串口 */
  143.     fd = open(dev[com_port - 1], O_RDWR|O_NOCTTY|O_NDELAY);
  144.     if (fd < 0)
  145.     {
  146.             perror("open serial port");
  147.             return(-1);
  148.     }
  149.     
  150.     if (fcntl(fd, F_SETFL, 0) < 0) /*恢复串口为阻塞状态*/
  151.     {
  152.         perror("fcntl F_SETFL\n");
  153.     }
  154.     
  155.     if (isatty(fd) == 0) /*测试打开的文件是否为终端设备*/
  156.     {
  157.         perror("This is not a terminal device");
  158.     }    
  159.     return fd;
  160. }

  1. /* com_writer.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. #include "uart_api.h"
  9. int main(void)
  10. {
  11.     int fd;
  12.     char buff[BUFFER_SIZE];
  13.     if((fd = open_port(HOST_COM_PORT)) < 0) /* 打开串口 */
  14.     {
  15.         perror("open_port");
  16.         return 1;
  17.     }    
  18.     if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
  19.     {
  20.         perror("set_com_config");
  21.         return 1;
  22.     }    
  23.     do
  24.     {
  25.         printf("Input some words(enter 'quit' to exit):");
  26.         memset(buff, 0, BUFFER_SIZE);
  27.         if (fgets(buff, BUFFER_SIZE, stdin) == NULL)
  28.         {
  29.             perror("fgets");
  30.             break;
  31.         }
  32.         write(fd, buff, strlen(buff));
  33.     } while(strncmp(buff, "quit", 4));
  34.       close(fd);
  35.       return 0;
  36. }

  1. /* com_reader.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. #include "uart_api.h"

  9. int main(void)
  10. {
  11.     int fd;
  12.     char buff[BUFFER_SIZE];
  13.     
  14.     if((fd = open_port(TARGET_COM_PORT)) < 0) /* 打开串口 */
  15.     {
  16.         perror("open_port");
  17.         return 1;
  18.     }
  19.     if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
  20.     {
  21.         perror("set_com_config");
  22.         return 1;
  23.     }
  24.     
  25.     do
  26.     {
  27.         memset(buff, 0, BUFFER_SIZE);
  28.         if (read(fd, buff, BUFFER_SIZE) > 0)
  29.         {
  30.             printf("The received words are : %s", buff);
  31.         }
  32.     } while(strncmp(buff, "quit", 4));
  33.       close(fd);
  34.       return 0;
  35. }

 linux串口.rar  

阅读(839) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~