[ARM入门] Linux下的串口调用

[复制链接]
 楼主| dominic318 发表于 2020-6-15 21:04 | 显示全部楼层 |阅读模式
Cheetah使用说明之Linux下的串口调用

admin  畅维通达  今天
串口作为通讯、日志、配置、AT指令集控制等手段,在各种电子设备中被广泛使用,本文以cheetah板卡为例,介绍Linux下如何调用硬件串口。
准备:
预装ubuntu16.04的cheetah主板,预备好开发环境(关注公众号,留言区输入“环境”,可以看到教程《Cheetah使用说明之Linux编程环境》);
USB转串口工具;
在PC机上,你已经准备好了Linux系统,本文使用虚拟机,ubuntu12.02。
目标:
    在cheetah上实现监控外来的串口TTL信息,自动记录收到的信息到cheetah的eMMC上的指定文件里,并且为每一条信息打上时间戳。
步骤:
1.打开PC机Eclispe,编辑代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <termios.h>
  7. #include <time.h>

  8. int main(int argc, char* argv[])
  9. {
  10.   time_t now; //实例化time_t结构
  11.   struct tm *timenow; //实例化tm结构指针
  12.     int fd;
  13.     int fd_log;
  14.     int iRet;
  15.     char *bufTemp;
  16.     struct termios options_old, options;
  17.     char buf[1024];
  18.     if((fd_log=open("log.txt",O_RDWR|O_CREAT,0644))==-1){
  19.       printf("open log error\n");
  20.       goto err1;
  21.     }
  22.     fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY );
  23.     if (fd < 0) { /** error **/
  24.         printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
  25.         goto err1;
  26.     }
  27.     fcntl(fd, F_SETFL, FNDELAY);
  28.     /*********************************************************/
  29.     /** * Get the current options for the port... **/
  30.     tcgetattr(fd, &options);
  31.     options_old = options;
  32.     /*** Set the baud rates to 115200... **/
  33.   cfsetispeed(&options, B115200);
  34.   cfsetospeed(&options, B115200);

  35.     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
  36.     options.c_oflag &= ~OPOST; /** 选择原始输出 **/

  37.     /*** Set the new options for the port... **/
  38.     tcsetattr(fd, TCSANOW, &options);
  39.     /*********************************************************/
  40.     while(1){
  41.     /*iRet = write(fd, "{"type":"test","FlowNumber":"20200615"}", 42);
  42.     if (iRet < 0) {
  43.       printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
  44.     }*/
  45.         memset(buf, 0, 1024);
  46.         bufTemp = buf;
  47.         while(1){
  48.           iRet = read(fd, bufTemp, 1);
  49.           if(iRet <= 0){
  50.             break;
  51.           }
  52.           bufTemp += iRet;
  53.           usleep(1000);
  54.         }
  55.       if(bufTemp == buf){
  56.         continue;
  57.       }
  58.     time(&now);//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
  59.     timenow = localtime(&now);//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
  60.     strncat(buf, asctime(timenow), strlen(asctime(timenow)));
  61.     iRet += strlen(asctime(timenow));
  62.     if(write(fd_log, buf, strlen(buf))!=strlen(buf)){
  63.       printf("write error!\n");
  64.       goto err1;
  65.     }
  66.     }
  67. err1:
  68.   tcsetattr(fd, TCSANOW, &options_old);
  69.   close(fd);
  70.   close(fd_log);
  71.     return 0;
  72. }

上述代码,建立了log.txt作为本地日志文档,初始化了插在USB0接口上的USB转串口工具,设置了115200波特率等,主循环中不断查询串口有无发来信息,如果有收到信息就每隔一秒钟再次读取缓存区直到本帧信息结束,然后加上时间戳存储到log.txt中。
2.在cheetah的USB0接口上插上USB转串口工具。

3.登陆SSH,访问cheetah,在终端输入ls /dev应能检测到如下图红框内的应答。

4.在Eclispe编译代码,并下载到cheetah上运行,具体参考《Cheetah使用说明之Linux编程环境》。
5.在cheetah运行二进制文件。
小结:
上述是对USB0接口的描述,同样的,可以使用cheetah8口插座上预留的TTL串口ttyS2。
关注“畅维通达”公众号,获得更多产品咨询。
https://mp.weixin.qq.com/s/Oy8sofQThuHA9_QuQh2rGw

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

4

主题

8

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

4

主题

8

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部