本文共 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/