免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1546 | 回复: 1
打印 上一主题 下一主题

[C] serial 导向问题 [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-03-15 21:36 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>
  9. #include <sys/time.h>
  10. #include <string.h>

  11. #define TRUE 1
  12. #define FALSE -1

  13. int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
  14.                    B38400, B19200, B9600, B4800, B2400, B1200, B300,
  15.                   };

  16. int name_arr[] = {115200, 38400,  19200,  9600,  4800,  2400,  1200,  300,
  17.                   38400,  19200,  9600, 4800, 2400, 1200,  300,
  18.                  };

  19. void set_speed(int fd, int speed)
  20. {
  21.     int i;
  22.     int status;

  23.     struct termios Opt;
  24.     tcgetattr(fd, &Opt);

  25.     for (i = 0; i < sizeof(speed_arr) / sizeof(int); i++) {
  26.         if (speed == name_arr[i]) {
  27.             tcflush(fd, TCIOFLUSH);
  28.             cfsetispeed(&Opt, speed_arr[i]);
  29.             cfsetospeed(&Opt, speed_arr[i]);
  30.             status = tcsetattr(fd, TCSANOW, &Opt);

  31.             if (status != 0) {
  32.                 perror("tcsetattr fd1");
  33.             }

  34.             return;
  35.         }

  36.         tcflush(fd, TCIOFLUSH);
  37.     }
  38. }

  39. int set_Parity(int fd, int databits, int stopbits, int parity)
  40. {
  41.     struct termios options;

  42.     if (tcgetattr(fd, &options) != 0) {
  43.         perror("SetupSerial 1");
  44.         return (FALSE);
  45.     }

  46.     options.c_cflag &= ~CSIZE;

  47.     switch (databits) {
  48.     case 7:
  49.         options.c_cflag |= CS7;
  50.         break;

  51.     case 8:
  52.         options.c_cflag |= CS8;
  53.         break;

  54.     default:
  55.         fprintf(stderr, "Unsupported data size\n");
  56.         return (FALSE);
  57.     }

  58.     switch (parity) {
  59.     case 'n':
  60.     case 'N':
  61.         options.c_cflag &= ~PARENB;    /* Clear parity enable */
  62.         options.c_iflag &= ~INPCK;    /* Enable parity checking */
  63.         options.c_iflag &= ~(ICRNL | IGNCR);
  64.         options.c_lflag &= ~(ICANON);
  65.         break;

  66.     case 'o':
  67.     case 'O':
  68.         options.c_cflag |= (PARODD | PARENB);
  69.         options.c_iflag |= INPCK;    /* Disnable parity checking */
  70.         break;

  71.     case 'e':
  72.     case 'E':
  73.         options.c_cflag |= PARENB;    /* Enable parity */
  74.         options.c_cflag &= ~PARODD;
  75.         options.c_iflag |= INPCK;    /* Disnable parity checking */
  76.         break;

  77.     case 'S':
  78.     case 's':  /*as no parity*/
  79.         options.c_cflag &= ~PARENB;
  80.         options.c_cflag &= ~CSTOPB;
  81.         break;

  82.     default:
  83.         fprintf(stderr, "Unsupported parity\n");
  84.         return (FALSE);
  85.     }

  86.     switch (stopbits) {
  87.     case 1:
  88.         options.c_cflag &= ~CSTOPB;
  89.         break;

  90.     case 2:
  91.         options.c_cflag |= CSTOPB;
  92.         break;

  93.     default:
  94.         fprintf(stderr, "Unsupported stop bits\n");
  95.         return (FALSE);
  96.     }

  97.     /* Set input parity option */

  98.     if (parity != 'n') {
  99.         options.c_iflag |= INPCK;
  100.     }

  101.     options.c_cc[VTIME] = 150; // 15 seconds
  102.     options.c_cc[VMIN] = 0;

  103.     tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */

  104.     if (tcsetattr(fd, TCSANOW, &options) != 0) {
  105.         perror("SetupSerial 3");
  106.         return (FALSE);
  107.     }

  108.     return (TRUE);
  109. }

  110. int main(int argc, char** argv)
  111. {
  112.     int fd;
  113.     int nread;
  114.     int nwrite;
  115.     int n = 0;
  116.     char buffer[15];
  117.     char devname_head[10] = "/dev/";
  118.     char dev_name[20];

  119.     if (argc < 2) {
  120.         printf("Please input './test_uart ttySx'\n");
  121.         exit(1);
  122.     } else {
  123.         strcpy(dev_name, devname_head);
  124.         strcat(dev_name, argv[1]);
  125.     }

  126.     fd = open(dev_name, O_RDWR);

  127.     if (fd < 0) {
  128.         perror("error to open /dev/ttySx\n");
  129.         exit(1);
  130.     }

  131.     if (fd > 0) {
  132.         set_speed(fd, 115200);
  133.     } else {
  134.         printf("Can't Open Serial Port!\n");

  135.         exit(0);
  136.     }

  137.     if (set_Parity(fd, 8, 1, 'N') == FALSE) {
  138.         printf("Set Parity Error\n");
  139.         exit(1);
  140.     }

  141.     printf("\nWelcome to uart_test\n\n");
  142.     memset(buffer, 0, sizeof(buffer));
  143.     char test[15] = "hello world";
  144.     nwrite = write(fd, test, strlen(test));

  145.     if (nwrite < 0) {
  146.         printf("write error\n");
  147.     }

  148.     printf("Send test data------>%s\n", test);

  149.     while (1) {
  150.         nread = read(fd, &buffer[n], 1);

  151.         if (nread < 0) {
  152.             printf("read error\n");
  153.         }

  154.         printf("read char is -> %c \n", buffer[n]);

  155.         if (strlen(buffer) == strlen(test)) {
  156.             printf("Read Test Data finished,Read Test Data is------->%s\n", buffer);
  157.             memset(buffer, 0, sizeof(buffer));
  158.             printf("Send test data again------>%s\n", test);

  159.             write(fd, test, strlen(test));
  160.             n = 0;
  161.             sleep(1);
  162.             continue;
  163.         }

  164.         n++;
  165.     }
  166. }
复制代码


上面代碼 hello world 字浮串導向 /dev/ttyS0

sudo ./test_uart ttyS0

我開cutecom 跟 minicom 想收hello world 字浮串但都收不到

baud rate 兩邊都是115200

可是我導向某個終端機

sudo ./test_uart pts/22 可以收到 hello world 字浮串
不明白為什麼 ttyS0 收不到字浮串

謝謝


论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
2 [报告]
发表于 2017-04-05 15:39 |只看该作者
     cfsetispeed(&Opt, speed_arr[i]);

35.       cfsetospeed(&Opt, speed_arr[i]);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP