博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下串口编程
阅读量:4045 次
发布时间:2019-05-24

本文共 3097 字,大约阅读时间需要 10 分钟。

linux下串口编程

2014年4月10日21:40:04

步骤:

1、打开串口;2、配置串口;3、读写串口;4、关闭串口。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int speed_sel[] ={B115200, B57600, B38400, B19200, B9600};static int name_sel[] = {115200, 57600, 38400, 19200, 9600};int open_port(char *SerialPort){ int fd; fd = open(SerialPort, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { printf("open_port:Unable to open %s \n", SerialPort); } else fcntl(fd, F_SETFL, 0); return (fd);}int close_port(int fd){ int rtn; rtn = close(fd); return (rtn);}void speed_set(int fd, int speed){ int i, status, tempspeed; struct termios opt; struct serial_struct p; speed_t lngspeed; if(fd <= 0) return ; tcgetattr(fd, &opt); tempspeed = speed; if(speed == 28800) tempspeed = 38400; for(i = 0; i < sizeof(speed_sel) / sizeof(int); i++) { if(tempspeed == name_sel[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&opt, speed_sel[i]); cfsetospeed(&opt, speed_sel[i]); status = tcsetattr(fd, TCSANOW, &opt); if(status != 0) { return ; } tcflush(fd, TCIOFLUSH); } } lngspeed = cfgetispeed(&opt); if(speed == 28800) { ioctl(fd, TIOCGSERIAL, &p); p.flags = ASYNC_SPD_CUST; p.custom_divisor = 4; ioctl(fd, TIOCSSERIAL, &p); } lngspeed = cfgetispeed(&opt);}int parity_set(int fd, int databits, int stopbits, int parity){ struct termios opt; if(fd <= 0) return 1; tcgetattr(fd, &opt); opt.c_cflag &= ~CSIZE; switch(databits) { case 7: opt.c_cflag |= CS7; break; case 8: opt.c_cflag |= CS8; break; } switch(parity) { case 'n': case 'N': opt.c_cflag &= ~PARENB; opt.c_iflag &= ~INPCK; break; case 'o': case 'O': opt.c_cflag |= (PARODD | PARENB); opt.c_iflag |= INPCK; break; case 'e': case 'E': opt.c_cflag |= PARENB; opt.c_cflag &= ~PARODD; opt.c_iflag |= INPCK; break; case 'S': case 's': opt.c_cflag &= ~PARENB; opt.c_cflag &= ~CSTOPB; break; } switch(stopbits) { case 1: opt.c_cflag &= ~CSTOPB; break; case 2: opt.c_cflag |= CSTOPB; } if(parity != 'n') opt.c_iflag |= INPCK; opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opt.c_oflag &= ~OPOST; opt.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL); tcflush(fd, TCIFLUSH); opt.c_cc[VTIME] = 5; opt.c_cc[VMIN] = 0; tcsetattr(fd, TCSANOW, &opt); return(1);}unsigned char writecom(int fd, unsigned char *pdata, long lnglen){ long lngwritelen; FILE *intFile; char chinfo[300]; long i; if(fd <= 0) return 0; tcflush(fd, TCIOFLUSH); lngwritelen = write(fd, pdata, lnglen); if(lngwritelen == -1) return 0; if(lngwritelen != lnglen) return 0; return 0xff;}unsigned char readcom(int fd, unsigned char *pdata, long lnglen){ long lngwritelen; if(fd <= 0) return 0; lngwritelen = read(fd, pdata, lnglen); if(lngwritelen == -1) return 0; if(lngwritelen != lnglen) return 0; return 0xff;}int main(){ char* SerialPort = "/dev/ttymxc2"; int csc_comm = open_port(SerialPort); speed_set(csc_comm, 115200); parity_set(csc_comm, 8, 1, 'n'); writecom(csc_comm,"seulww", 6); getchar(); char str[20] = {0}; readcom(csc_comm,str,15); printf("%s\n",str); close_port(csc_comm); return (csc_comm);}

转载地址:http://cnwci.baihongyu.com/

你可能感兴趣的文章
AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
查看>>
Android中AsyncTask的简单用法
查看>>
解决跨网场景下,CAS重定向无法登录的问题(无需修改现有代码)
查看>>
java反编译命令
查看>>
activemq依赖包获取
查看>>
概念区别
查看>>
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>