linux 串口通信

xiaoxiao2021-02-28  236

串口的操作一般都通过四个步骤来完成: 1、打开串口

2、配置串口:对串口的波特率、数据位、停止位、校验码、等进行设置。

3、读写串口

4、关闭串口

1打开串口:

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> 函数原型: int open(const char *pathname, int flags);        int open(const char *pathname, int flags, mode_t mode);

int open_port(void) { int fd; fd=open("/dev/ttyS2",O_RDWR | O_NOCTTY | O_NONBLOCK);//O_NONBLOCK设置为非阻塞模式,在read时不会阻塞住,在读的时候将read放在while循环中 if(fd==-1) { perror("Can't Open SerialPort"); } return fd; }

可以利用fcntl函数来恢复串口为阻塞状态。

#include <unistd.h> #include <fcntl.h> fcntl(fd,F_SETFL,0); //恢复串口阻塞状态,等待串口数据的读入,返回-1代表出错 if(isatty(STDIN_FILENO)==0) printf("isatty success"); //测试文件描述符是否引用一个终端设备,进一步确认串口是否正确打开。 else printf("standard input is not a terminal device\n");

2、配置串口:对串口的波特率、数据位、停止位、校验码、等进行设置。

函数声明:

nt32_t serial_port_open_full(const char *device, uint32_t baud_rate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flow_ctrl); kbinfo->tty_fd = serial_port_open_full(tty_dev, 4800, 8, 1, 2, 0); //函数调用 if (kbinfo->tty_fd < 0) { log_error("failed to open %s, %s", tty_dev, strerror(errno)); exit (1); }

int32_t serial_port_open_full(const char *device, uint32_t baud_rate, uint32_t databits, uint32_t stopbits, uint32_t parity, uint32_t flow_ctrl) { int32_t comfd; struct termios setting; // if (NULL == device) { log_error("tty device is null"); return -1; } if ((comfd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { log_warn("Can not open serial port %s", device); return -1; } tcgetattr(comfd, &setting); //初始化一个终端对应的termios结构 setting.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | PARENB); //control flag setting.c_iflag = 0; //input flag setting.c_oflag = 0; //output flag setting.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //local flag switch (baud_rate) //波特率 { case 300: baud_rate = B300; break; case 600: baud_rate = B600; break; case 1200: baud_rate = B1200; break; case 2400: baud_rate = B2400; break; case 4800: baud_rate = B4800; break; case 9600: baud_rate = B9600; break; case 19200: baud_rate = B19200; break; case 38400: baud_rate = B38400; break; case 57600: baud_rate = B57600; break; case 115200: baud_rate = B115200; break; case 230400: baud_rate = B230400; break; default: log_warn("Invalid baud rate %d.\n", baud_rate); goto ERR; } switch (databits) //数据位 { case 5: databits = CS5; break; case 6: databits = CS6; break; case 7: databits = CS7; break; case 8: databits = CS8; break; default: log_warn("Invalid data bits %d.\n", databits); goto ERR; } switch (stopbits) //停止位 { case 1: stopbits = 0; break; case 2: stopbits = CSTOPB; break; default: log_warn("Invalid stop bits %d.\n", stopbits); goto ERR; } switch (parity) //校验码 { case 0: parity = 0; break; case 1: //odd parity = PARENB|PARODD; break; case 2: //even parity = PARENB; break; default: log_warn("Invalid parity.\n"); goto ERR; } switch (flow_ctrl) //流量控制 { case 0: flow_ctrl = 0; break; case 1: //hardware flow_ctrl = CRTSCTS; break; case 2: //software flow_ctrl = 0; setting.c_iflag |= (IXON | IXOFF | IXANY); break; default: log_warn("Invalid flow ctrl.\n"); goto ERR; } setting.c_cflag |= (baud_rate| databits | stopbits | parity | flow_ctrl | CREAD | CLOCAL); cfsetspeed(&setting, baud_rate); if (tcsetattr(comfd, TCSAFLUSH, &setting)) { goto ERR; } return comfd; ERR: close(comfd); return -1; }

最小的termios结构的经典定义如下:

#inlcude<termios.h> struct termios { unsigned char c_iflag; //输入模式 unsigned char c_oflag; //输出模式 unsigned char c_cflag; //控制模式 unsigned char c_lflag; //本地模式 unsigned char c_cc[NCC]; //特殊控制模式 };

3.读写串口

读串口

#include <unistd.h> ssize_t read(int fd, void *buf, size_t count);

nread=read(fd,buff,8);//读串口 printf("nread=%d,%s\n",nread,buff); 写串口

#include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);

fd:文件描述符

buf:读数据缓存

count:请求读取字节数

4.关闭串口

#include<unistd.h> int close(int fd);

转载请注明原文地址: https://www.6miu.com/read-22604.html

最新回复(0)