注意:本方法包含的<asm-generic/termbits.h>和<termios.h>这两个头文件里都定义了struct termios,需将其中一个注释。
#include <stdio.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <asm-generic/ioctls.h>
#include <asm-generic/termbits.h>
#define ERRLOG(msg) \
do \
{ \
printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
perror(msg); \
exit(-1); \
} while (0);
// 打开串口函数
int open_serial(const char *device)
{
int fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("打开串口失败");
return -1;
}
// 设置为阻塞模式
if (fcntl(fd, F_SETFL, 0) < 0)
{
perror("设置串口阻塞失败");
close(fd);
return -1;
}
return fd;
}
// 设置串口参数
int configure_serial(int fd, int baudrate, int databits, int stopbits, char parity)
{
struct termios options;
// 获取当前串口设置
if (tcgetattr(fd, &options) != 0)
{
perror("获取串口属性失败");
return -1;
}
// 设置输入输出波特率
cfsetispeed(&options, B0); // 先清除波特率
cfsetospeed(&options, B0);
// 使用自定义波特率
struct termios2 term2;
if (ioctl(fd, TCGETS2, &term2) == -1)
{
perror("获取termios2失败");
return -1;
}
term2.c_cflag &= ~CBAUD;
term2.c_cflag |= BOTHER;
term2.c_ispeed = baudrate;
term2.c_ospeed = baudrate;
if (ioctl(fd, TCSETS2, &term2) == -1)
{
perror("设置自定义波特率失败");
return -1;
}
// 设置字符大小
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "不支持的数据位: %d\n", databits);
return -1;
}
// 设置停止位
if (stopbits == 1)
{
options.c_cflag &= ~CSTOPB;
}
else if (stopbits == 2)
{
options.c_cflag |= CSTOPB;
}
else
{
fprintf(stderr, "不支持的停止位: %d\n", stopbits);
return -1;
}
// 设置校验位
switch (parity)
{
case 'N': // 无校验
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'E': // 偶校验
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'O': // 奇校验
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_iflag |= INPCK;
break;
default:
fprintf(stderr, "不支持的校验位: %c\n", parity);
return -1;
}
// 设置串口为原始模式
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
// 设置输入模式
options.c_iflag &= ~(IXON | IXOFF | IXANY);
// 设置属性生效
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("设置串口属性失败");
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
char *device = "/dev/ttyUSB0";
int fd = open_serial(device);
if (fd == -1)
{
return EXIT_FAILURE;
}
int baudrate = 614400;
int databits = 8;
int stopbits = 1;
char parity = 'E';
if (configure_serial(fd, baudrate, databits, stopbits, parity) == -1)
{
close(fd);
return EXIT_FAILURE;
}
fd_set read_fds;
unsigned char readBuffer[1024] = {0};
// 收数据
while (1)
{
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
// 使用 select 等待数据
int activity = select(fd + 1, &read_fds, NULL, NULL, NULL);
if (activity < 0)
{
perror("select error");
break;
}
if (FD_ISSET(fd, &read_fds))
{
int bytes_read = read(fd, readBuffer, sizeof(readBuffer));
if (bytes_read > 0)
{
for (size_t i = 0; i < bytes_read; i++)
{
printf("%02X ", readBuffer[i]);
}
putchar('\n');
// printf("%s\n",readBuffer);
memset(readBuffer, 0, sizeof(readBuffer));
}
// 每次读到数据存在这里readBuffer
}
}
return 0;
}