嵌入式Linux系列第7篇:操做UART

1linux

引言ios

串口是咱们实际工做中常用的一个接口,好比咱们在Linux下使用的debug串口,它用来登陆Linux系统,输出log。另外咱们也会使用串口和外部的一些模块通讯,好比GPS模块、RS485等。这里对Linux下串口使用作个总结,但愿对你们有所帮助。

2git

环境介绍github

2.1.硬件
1) 网上的一个第三方作的NUC972开发板:
有兴趣购买的朋友,能够去他们的淘宝店购买:
https://s.click.taobao.com/X8mza8w
此次要控制的是板子底板上DB9串口:
对应NUC972的PE3和PE2引脚。
2) 2根USB转RS232线,一个用来链接板子的debug串口UART0,另一个用来链接板子上的串口UART1.
2.2.软件
1) 咱们在上一篇《Linux学习系列六:操做GPIO》的基础上改动下Linux内核配置,生成新的970uimage并烧写到板子里。
2) uboot、rootfs使用板子里默认的,为了增长micorcom命令,须要使用busybox生成,而后经过U盘导入到板子里。Busybox具体使用参考《Linux学习系列五:Nand Flash根文件系统制做》
3)交叉工具链arm_linux_4.8.tar.gz

3web

Busybox生成microcom命令编程

microcom命令相似于windows下的串口调试助手,在调试串口时很是有用,默认状况下板子里不支持这个命令,须要用busybox去生成。
1)busybox的使用若是你们有遗忘,能够参考《Linux 学习系列五:Nand Flash 根文件系统制做》中详细介绍,首先咱们把原来的~/nuc972/rootfs目录里的内容给删掉
2)进入到busybox目录,make menuconfig,输入/, 搜索microcom,找到配置它的位置
而后进入到对应的位置,把microcom选中。
3) 编译make,安装make install,而后压缩一下生成rootfs.tar
4) 经过U盘导入到板子里,放到根目录下解压,这样板子就支持microcom命令了。

4windows

内核配置缓存

1)为了使用UART1,须要在内核里作以下配置:
Device Drivers --->
Character devices --->
 Serial drivers
[*] NUC970/N9H30 UART1 support
保存生成新的.config 文件。
2)make uImage,生成新的970uimage文件,将其单独下载到板子里便可。

5微信

UART操做app

5.1.命令行操做
咱们将板子上的两个串口同时和PC机链接,经过debug串口登陆Linux系统操做UART1,PC端打开串口调试助手,选择UART1对应的串口,这样板子经过UART1就能够和PC之间进行数据的收发了。
登陆板子后,输入下面指令:
microcom -s 115200 /dev/ttyS1
  /dev下的ttyS1对应的就是UART1设备。
  microcom 命令后的-s 115200,表示设置波特率为115200bps。
  若是你想了解microcom的详细实现机制,能够到busybox的目录miscutils查看microcom.c源代码便可。
  输入上述命令后,当此串口收到数据后,就会自动在窗口中显示出来,若是键盘输入字符,就会自动经过此串口发送出去。咱们能够双向收发测试。
注意:
1) micrcom指令退出的方式是Ctrl+x,不是Ctrl+c,若是输入Ctrl+c,它实际上是发送了0x03字符。
2) 有些工程师喜欢用cat 指令去查看串口就没有收到数,其实这是不对的,咱们作下面这个测试,为了方便起见,咱们让PC端1s一次定时发送
  使用micrcom的话,
microcom -s 115200 /dev/ttyS1
会看到在不断的接收数据
咱们Ctrl+x先关掉microcom,直接输入
cat /dev/ttyS1
会有什么结果呢?
什么都没有收到。
因此千万不要直接用cat去判断串口是否有数据接收,为何有时能收到呢,那是由于串口设备在某个地方被打开(调用了open函数)了。
好比你让microcom指令在后台执行
microcom -s 115200 /dev/ttyS1 &
这时再使用cat指令就能够显示数据了。
5.2.C语言串口编程
咱们看下在C代码里如何操做串口,下面是一个例子:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <asm/termios.h>#include <memory.h>
#define DEV_NAME "/dev/ttyS1"int main (int argc, char *argv[]){ int fd; int len, i,ret;  char buf[] = "Hello TopSemic! \n"; fd = open(DEV_NAME, O_RDWR | O_NOCTTY);  if(fd < 0) { perror(DEV_NAME); return -1;   } len = write(fd, buf, sizeof(buf)); if (len < 0) { printf("write data error \n"); } memset(buf,0x00,sizeof(buf)); len = read(fd, buf, sizeof(buf));  if (len < 0 { printf("read error \n"); return -1;  }  printf("%s", buf);  return(0);}
将它编译后放到板子里,注意上述代码没有设置串口波特率,默认值是9600,须要在串口调试助手中正确配置,运行一下咱们先看看效果:
交叉验证下,咱们把UART1的波特率设置为115200后,结果以下,能够看到是没法正确接收到数据的。
上述程序工做过程是串口先发送一串数据,而后一直停在read函数处不动,直到接收到数据后返回退出。此时串口工做在阻塞模式下。所谓阻塞和非阻塞的含义以下:
阻塞:
对于read,指当串口输入缓存区没有数据的时候,read函数将会阻塞在这里,直到串口输入缓存区中有数据可读取,read读到了须要的字节数以后,返回值为读到的字节数;
对于write,指当串口输出缓冲区满,或剩下的空间小于将要写入的字节数,则write将阻塞,一直到串口输出缓冲区中剩下的空间大于等于将要写入的字节数,执行写入操做,返回写入的字节数。
非阻塞:
对于read,指当串口输入缓冲区没有数据的时候,read函数当即返回,返回值为-1。
对于write,指当串口输出缓冲区满,或剩下的空间小于将要写入的字节数,则write将进行写操做,写入当前串口输出缓冲区剩下空间容许的字节数,而后返回写入的字节数。
在打开串口文件时,打开模式加上O_NDELAY能够以非阻塞方式打开串口;反之,不加上O_NDEAY,默认以阻塞方式打开串口。上述第一例子中没有加O_NDEAY标志,因此工做在阻塞模式下,下面再看个例子,咱们加上O_NDEAY
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <asm/termios.h>#include <memory.h>
#define DEV_NAME "/dev/ttyS1" int main (int argc, char *argv[]){ int fd; int len, i,ret; char buf[] = "Hello TopSemic! \n";
fd = open(DEV_NAME, O_RDWR | O_NOCTTY|O_NDELAY); if(fd < 0) { perror(DEV_NAME); return -1; }
len = write(fd, buf, sizeof(buf)); if (len < 0) { printf("write data error \n"); } while(1) { memset(buf,0x00,sizeof(buf)); len = read(fd, buf, sizeof(buf));
printf("len:%d \n",len); if(len>0) printf("%s", buf); usleep(100000); }}
这时程序运行结果以下,在串口接收不到数据时,read函数当即返回,返回值是-1,当接收到数据后,返回值是接收到数据值长度。
你们可能注意到,上述代码没有关于串口的参数配置,好比波特率、校验位、数据位、中止位的设置,实际应用中极可能是要修改这些参数的,最多见的就是修改波特率,下面例子在上面的基础上修改以下:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <asm/termios.h>#include <memory.h>#include <signal.h>
#define DEV_NAME "/dev/ttyS1"static struct termios newtios,oldtios; /*termianal settings */
static int saved_portfd=-1; /*serial port fd */
static void reset_tty_atexit(void){ if(saved_portfd != -1) { tcsetattr(saved_portfd,TCSANOW,&oldtios); } }
/*cheanup signal handler */static void reset_tty_handler(int signal){ if(saved_portfd != -1) { tcsetattr(saved_portfd,TCSANOW,&oldtios); } _exit(EXIT_FAILURE);}
static set_port_attr (int portfd,int baudrate){ struct sigaction sa; /*get serial port parnms,save away */ tcgetattr(portfd,&newtios); memcpy(&oldtios,&newtios,sizeof newtios); /* configure new values */ cfmakeraw(&newtios); /*see man page */ newtios.c_iflag |=IGNPAR; /*ignore parity on input */ newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL); newtios.c_cc[VMIN]=1; /* block until 1 char received */ newtios.c_cc[VTIME]=0; /*no inter-character timer */ switch(baudrate) { case 9600: cfsetispeed(&newtios,B9600); cfsetospeed(&newtios,B9600); break; case 19200: cfsetispeed(&newtios,B19200); cfsetospeed(&newtios,B19200); break; case 38400: cfsetispeed(&newtios,B38400); cfsetospeed(&newtios,B38400); break; case 115200: cfsetispeed(&newtios,B115200); cfsetospeed(&newtios,B115200); break; } /* register cleanup stuff */ atexit(reset_tty_atexit); memset(&sa,0,sizeof sa); sa.sa_handler = reset_tty_handler; sigaction(SIGHUP,&sa,NULL); sigaction(SIGINT,&sa,NULL); sigaction(SIGPIPE,&sa,NULL); sigaction(SIGTERM,&sa,NULL); /*apply modified termios */ saved_portfd=portfd; tcflush(portfd,TCIFLUSH); tcsetattr(portfd,TCSADRAIN,&newtios); return portfd;
}
int main (int argc, char *argv[]){ int fd; int len, i,ret; char buf[] = "Hello TopSemic! \n"; fd = open(DEV_NAME, O_RDWR | O_NOCTTY|O_NDELAY); if(fd < 0) { perror(DEV_NAME); return -1; } set_port_attr (fd,115200);
len = write(fd, buf, sizeof(buf)); if (len < 0) { printf("write data error \n"); } while(1) { memset(buf,0x00,sizeof(buf)); len = read(fd, buf, sizeof(buf));
printf("len:%d \n",len); if(len>0) printf("%s", buf); usleep(100000); }
return 0;}
这时咱们把波特率修改成115200了,你们能够验证下,只有把uart1对应串口波特率设置为115200时才能够正确收发。

6

结束语

本期相关的资料在连接:  https://github.com/TopSemic/NUC972_Linux    07 Lesson7 操做UART 中。

嵌入式Linux系列第1篇:开发环境搭建

嵌入式Linux系列第2篇:运行Hello World

嵌入式Linux系列第3篇:uboot编译下载

嵌入式Linux系列第4篇:Kernel编译下载

嵌入式Linux系列第5篇:Nand Flash根文件系统制做

嵌入式Linux系列第6篇:操做GPIO

长按下方二维码,欢迎关注


本文分享自微信公众号 - TopSemic嵌入式(TopSemic)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索