打开串口:
int fd;
fd = open("/dev/ttySAC0", O_RDWR);
if(fd < 0){
//打开失败
}
备份串口配置信息:
用于使用完串口后,将配置信息复原,以便其他使用此串口。
struct termios uart_cfg;
int ret;
ret = tcgetattr(fd, &uart_cfg);
if(ret < 0){
//获得配置信息失败
}
复原串口配置信息:
恢复使用前的串口配置信息。
int ret;
ret = tcsetattr(fd, TCSANOW, &uart_cfg);
if(ret < 0){
//恢复串口配置信息失败
}
配置串口:
struct termios uart_cfg;
int ret;
//先获得配置信息,在获得的配置信息上修改成想要的配置信息
ret = tcgetattr(fd, &uart_cfg);
if(ret < 0){
//获得配置信息失败
}
//设置波特率
tcflush(fd, TCIOFLUSH);
cfsetispeed(&uart_cfg, B9600); //设置为9600
cfsetospeed(&uar