@cyysu
2018-07-30T06:12:05.000000Z
字数 2840
阅读 907
- 时间:2017年10月30日10:30:34
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
- 版本:4.0
- 描述:测试ARM板串口通信小程序
Linux应用系列教程
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<termios.h>#include<string.h>#include <stdint.h>// 设置一些串口信息int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) {struct termios newtio, oldtio;if (tcgetattr(fd, &oldtio) != 0) {perror("SetupSerial 1");return -1;}bzero(&newtio, sizeof(newtio));newtio.c_cflag |= CLOCAL | CREAD; //CLOCAL:忽略modem控制线 CREAD:打开接受者newtio.c_cflag &= ~CSIZE; //字符长度掩码。取值为:CS5,CS6,CS7或CS8// 数据位switch (nBits) {case 7:newtio.c_cflag |= CS7;break;case 8:newtio.c_cflag |= CS8;break;}// 校验信息switch (nEvent) {case 'O':newtio.c_cflag |= PARENB; //允许输出产生奇偶信息以及输入到奇偶校验newtio.c_cflag |= PARODD; //输入和输出是奇及校验newtio.c_iflag |= (INPCK | ISTRIP); // INPACK:启用输入奇偶检测;ISTRIP:去掉第八位break;case 'E':newtio.c_iflag |= (INPCK | ISTRIP);newtio.c_cflag |= PARENB;newtio.c_cflag &= ~PARODD;break;case 'N':newtio.c_cflag &= ~PARENB;break;}// 串口波特率switch (nSpeed) {case 2400:cfsetispeed(&newtio, B2400);cfsetospeed(&newtio, B2400);break;case 4800:cfsetispeed(&newtio, B4800);cfsetospeed(&newtio, B4800);break;case 9600:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;case 115200:cfsetispeed(&newtio, B115200);cfsetospeed(&newtio, B115200);break;case 460800:cfsetispeed(&newtio, B460800);cfsetospeed(&newtio, B460800);break;default:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;}// 停止位if (nStop == 1)newtio.c_cflag &= ~CSTOPB; //CSTOPB:设置两个停止位,而不是一个else if (nStop == 2)newtio.c_cflag |= CSTOPB;//1、 MIN = 0 , TIME =0 有READ立即回传否则传回 0 ,不读取任何字元//2、 MIN = 0 , TIME >0 READ 传回读到的字元,或在十分之一秒后传回TIME若来不及读到任何字元,则传回0//3、 MIN > 0 , TIME =0 READ 会等待,直到MIN字元可读//4、 MIN > 0 , TIME > 0 每一格字元之间计时器即会被启动READ 会在读到MIN字元,传回值或TIME的字元计时(1/10秒)超过时将值 传回newtio.c_cc[VTIME] = 0; //VTIME:非cannoical模式读时的延时,以十分之一秒位单位newtio.c_cc[VMIN] = 0; //VMIN:非canonical模式读到最小字符数tcflush(fd, TCIFLUSH); // 改变在所有写入 fd 引用的对象的输出都被传输后生效,所有已接受但未读入的输入都在改变发生前丢弃。if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) //TCSANOW:改变立即发生{perror("com set error");return -1;}printf("set done!\n\r");return 0;}//小端返回0 大端返回1uint16_t checkCPUendian(){union{uint16_t a;uint8_t b;}c;c.a = 1;return !(c.b == 1);}int main(void) {int fd1, nset, nread, ret;char buf[100] = { "serial data\n" };char buf1[100];// 测试程序大小端printf("this cpu is big/little endian:%d(1:big0:little)\n",checkCPUendian());// 打开串口设备fd1 = open("/dev/ttyS0", O_RDWR);if (fd1 == -1)exit(1);printf("open SAC0 success!!\n");// 设置波特率的信息,这里测试时可以不用设置nset = set_opt(fd1, 9600, 8, 'N', 1);if (nset == -1)exit(1);printf("SET SAC0 success!!\n");while (1){// 清空串口接受缓存区memset(buf1, 0, sizeof(buf1));// 向设备写入信息ret = write(fd1, buf, 100);if (ret > 0) {printf("write success! wait data receive\n");}// 读取数据nread = read(fd1, buf1, 100);if (nread > 0) {printf("redatad: nread = %s\n\n\r", buf1);}sleep(1);}close(fd1);return 0;}
关于termios这个结构体的信息可以查看下面这个链接:
http://blog.csdn.net/mr_raptor/article/details/8349323
支付宝 微信