以下是串行通信端口设置
1. BaudRate: 19200
2. Parity: Even
3. StopBits: 1
发送器发送几个字节的数据:0x5A 0xA5 0xAA
接收
器是在 Linux 上使用 termios 串行 API 用 C 语言编写的
每个字节的设置为 0...为什么?
以下是用于在接收器应用程序上设置串行端口设置的 C (OS: Linux) 代码摘录:
void *threadRecv(void *arg)
{
char *portName = (char *)arg;
char ch;
struct termios portSettings;
//fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
fd = open(portName, O_RDONLY | O_NOCTTY);
close(fd);
fd = open(portName, O_RDONLY | O_NOCTTY);
if(fd == -1)
{
printf("Error opening port: %s", portName);
pthread_exit("Exiting thread");
}
cfsetispeed(&portSettings, B19200);
//Parity
portSettings.c_cflag &= ~PARENB;
portSettings.c_cflag |= PARENB;
portSettings.c_cflag &= ~PARODD;
//Stop Bit
portSettings.c_cflag &= ~CSTOPB;
//Data Size: 8bits
portSettings.c_cflag &= ~CSIZE;
portSettings.c_cflag |= CS8;
portSettings.c_cflag |= CREAD;
portSettings.c_cflag |= CLOCAL;
portSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
portSettings.c_iflag |= (INPCK);
portSettings.c_oflag &= ~OPOST; //For RAW I/P
portSettings.c_cc[VMIN] = 77;
portSettings.c_cc[VTIME] = 0;
if(tcsetattr(fd, TCSANOW, &portSettings) == -1)
{
printf("Error setting port: %s", portName);
pthread_exit("Exiting thread");
}
while(1)
{
//Recv Logic
}
}