简单测试串口通信
//配置串口
sudo stty -F /dev/ttyS1 115200 raw -echo -echoe -echok -crtscts
//更改权限
sudo chmod 777 /dev/ttyS1
//向串口输出数据
sudo echo "Hello,World" > /dev/ttyS1
然后在另一主机的输出端查看输出验证是否正确。
cat /dev/ttyS1
stty命令的用法请参考stty 命令说明及使用讲解 - boyemachao的专栏 - CSDN博客
代码实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
#define buffLen 1024
#define rcvTimeOut 2
int openPort(int fd, int comport){
if(comport == 4){
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY |O_NDELAY);
if (fd == -1){
perror("Can't Open Serial Port");
return(-1);
}
}
if(fcntl(fd, F_SETFL, 0) < 0)
printf("fcntl failed!\n");
else
printf("fcntl=%d/n",fcntl(fd, F_SETFL, 0));
if(isatty(STDIN_FILENO) == 0