打印
[ARM入门]

Linux下的串口调用

[复制链接]
373|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Cheetah使用说明之Linux下的串口调用

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

int main(int argc, char* argv[])
{
  time_t now; //实例化time_t结构
  struct tm *timenow; //实例化tm结构指针
    int fd;
    int fd_log;
    int iRet;
    char *bufTemp;
    struct termios options_old, options;
    char buf[1024];
    if((fd_log=open("log.txt",O_RDWR|O_CREAT,0644))==-1){
      printf("open log error\n");
      goto err1;
    }
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY );
    if (fd < 0) { /** error **/
        printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
        goto err1;
    }
    fcntl(fd, F_SETFL, FNDELAY);
    /*********************************************************/
    /** * Get the current options for the port... **/
    tcgetattr(fd, &options);
    options_old = options;
    /*** Set the baud rates to 115200... **/
  cfsetispeed(&options, B115200);
  cfsetospeed(&options, B115200);

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

    /*** Set the new options for the port... **/
    tcsetattr(fd, TCSANOW, &options);
    /*********************************************************/
    while(1){
    /*iRet = write(fd, "{\"type\":\"test\",\"FlowNumber\":\"20200615\"}", 42);
    if (iRet < 0) {
      printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
    }*/
        memset(buf, 0, 1024);
        bufTemp = buf;
        while(1){
          iRet = read(fd, bufTemp, 1);
          if(iRet <= 0){
            break;
          }
          bufTemp += iRet;
          usleep(1000);
        }
      if(bufTemp == buf){
        continue;
      }
    time(&now);//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
    timenow = localtime(&now);//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
    strncat(buf, asctime(timenow), strlen(asctime(timenow)));
    iRet += strlen(asctime(timenow));
    if(write(fd_log, buf, strlen(buf))!=strlen(buf)){
      printf("write error!\n");
      goto err1;
    }
    }
err1:
  tcsetattr(fd, TCSANOW, &options_old);
  close(fd);
  close(fd_log);
    return 0;
}

上述代码,建立了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

254905ee771ad0b6c2.png (1000 Bytes )

254905ee771ad0b6c2.png

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

8

帖子

0

粉丝