野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 37986|回复: 4

[求助] 在debian 系统下,使用串口2,串口3都试过,使用 gettimeofday ...

[复制链接]
发表于 2021-3-22 17:57:08 | 显示全部楼层 |阅读模式
48火花
本帖最后由 tao_oYMYe 于 2021-3-23 09:17 编辑

在debian 系统下,使用串口2,串口3都试过,使用 gettimeofday 函数,过1000ms 发送一次数据。

问题描述:开机之后,程序发送的数据无法到达串口终端,很多条数据可能只到达一条,没有规律。经过一段时间(时间不确定,一般是半个小时左右)以后,程序发送数据正常,终端正常显示。
接收数据本来有问题,后面配置了串口的配置参数,使其接收原始数据,才不会丢失特殊字符。

程序如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <termios.h>
  8. #include <string.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/time.h>
  11. #include <termios.h>

  12. int usart_init(char * path);

  13. //根据具体的设备修改
  14. const char default_screen_path[] = "/dev/ttymxc2";

  15. void main(void)
  16. {
  17.     unsigned char buf[250] = "Embedfire tty send test.01234567899876543210 abcdefghijklmnopqrstuvwxyz.\n";
  18.     struct timeval start, end;
  19.     int interval;

  20.     //串口初始化
  21.     int usart_fd = usart_init((char *)default_screen_path);
  22.     if (usart_fd == 0)
  23.     {
  24.         printf("usart '%s' init failed", default_screen_path);
  25.     }

  26.     //tv_sec 单位秒  tv_usec 单位微秒,最大1000000
  27.     gettimeofday(&start, NULL);

  28.     while (1)
  29.     {
  30.         //系统计时 200ms 发送一条数据
  31.         gettimeofday(&end, NULL);
  32.         interval = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000;
  33.         if (interval > 1000)
  34.         {
  35.             // send message
  36.             write(usart_fd, buf, sizeof(buf));
  37.             printf("interval = %d\n", interval);
  38.             interval = 0;
  39.             gettimeofday(&start, NULL);
  40.         }
  41.     }

  42.     close(usart_fd);
  43.     return NULL;
  44. }

  45. //串口初始化
  46. int usart_init(char *path)
  47. {
  48.     int fd;
  49.     int res;
  50.     struct termios opt;

  51.     //获取串口设备描述符
  52.     fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK, 0777);
  53.     if (fd < 0)
  54.     {
  55.         printf("Fail to Open %s device\n", path);
  56.         return 0;
  57.     }
  58.     printf("open %s is successed\n", path);

  59.     //清空串口接收缓冲区
  60.     tcflush(fd, TCIOFLUSH);
  61.     // 获取串口参数opt
  62.     tcgetattr(fd, &opt);

  63.     //设置串口输出波特率
  64.     cfsetospeed(&opt, B115200);
  65.     //设置串口输入波特率
  66.     cfsetispeed(&opt, B115200);
  67.     //设置数据位数
  68.     opt.c_cflag &= ~CSIZE;
  69.     opt.c_cflag |= CS8;
  70.     //校验位
  71.     opt.c_cflag &= ~PARENB;
  72.     opt.c_iflag &= ~INPCK;
  73.     //设置停止位
  74.     opt.c_cflag &= ~CSTOPB;

  75.     // &= 关闭   |= 设置
  76.     //如果设置,使能规范输入,否者使用原始数据
  77.     //opt.c_lflag &= ~ICANON;
  78.     opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  79.     //如果设置,将NL转换成CR-NL后输出
  80.     opt.c_oflag &= ~ONLCR;
  81.     //如果设置,将接收到的NL(换行)转换成CR(回车)。
  82.     opt.c_iflag &= ~INLCR;
  83.     //最少可读数据
  84.     opt.c_cc[VMIN] = 0;
  85.     //等待数据时间(10秒的倍数)
  86.     opt.c_cc[VTIME] = 0;
  87.     //如果设置,则忽略接收到的break信号
  88.     opt.c_iflag |= IGNBRK;
  89.     //如果设置,则启用实现自定义的输入处理
  90.     opt.c_lflag &= ~IEXTEN;
  91.     //如果设置,则禁止产生SIGINT,SIGQUIT和SIGSUSP信号时刷新输入和输出队列
  92.     opt.c_lflag |= NOFLSH;

  93.     opt.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL);

  94.     //使用原始数据输出
  95.     opt.c_oflag &= ~OPOST;

  96.     //更新配置
  97.     if(tcsetattr(fd, TCSANOW, &opt) != 0)
  98.     {
  99.         printf("init error...........\n");
  100.         return -1;
  101.     }

  102.     return fd;
  103. }
复制代码




正常的截图如下:
11.png



不正常的就是debian 的终端打印了很多条, 串口终端才打印很少的数据。

22.png



回复

使用道具 举报

 楼主| 发表于 2021-3-23 10:17:36 | 显示全部楼层
最新结果:在同时插上 串口2 串口3的时候,串口3 可以正常的工作了
回复

使用道具 举报

发表于 2021-3-23 16:24:09 | 显示全部楼层
是不是没有共地之类的
回复

使用道具 举报

发表于 2021-3-27 17:37:41 | 显示全部楼层
示波器抓下波形
回复

使用道具 举报

 楼主| 发表于 2021-3-29 08:57:35 | 显示全部楼层
经过测试,串口2和串口3同时相连,串口3的发送才正常,对应火哥的原理图,看了下引脚都正常连接,不知道其他的 pro 板是不是有问题。

应该不是代码的问题了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-2 20:59 , Processed in 0.030332 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表