linux下串口编程
2014-04-10 21:39
337 查看
linux下串口编程
2014年4月10日21:40:04
步骤:
1、打开串口;2、配置串口;3、读写串口;4、关闭串口。
代码:
2014年4月10日21:40:04
步骤:
1、打开串口;2、配置串口;3、读写串口;4、关闭串口。
代码:
#include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <sys/time.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <linux/serial.h> #include <asm/ioctls.h> #include <sys/io.h> #include <time.h> 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); }
相关文章推荐
- 字符设备注册相关函数
- java运行显示“找不到或无法加载主类”的解决办法(linux)
- linux 有关磁盘
- linux msg
- Centos下主DNS的搭建
- Linux虚拟机和宿主机以及其他主机通信的办法
- 【Linux VI】 个人小结
- linux 下搭建tftp服务器
- 【Linux 入门笔记】Vi文本编辑器
- linux 重定向
- linux文件权限
- linux sharememory
- zedboard 构建嵌入式linux
- zedboard 构建嵌入式linux
- linux control
- linux signal
- linux fifo
- linux 管道
- linux时间编程
- linux文件编程- C库