uniy3D接受并读取串口数据的c#脚本
时间: 2023-04-08 16:05:13 浏览: 66
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <termios.h>
# include <errno.h>
int main(int argc, char *argv[])
{
int fd;
struct termios options;
char buf[255];
if (argc < 2) {
printf("Usage: %s <serial port device>\n", argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open");
return -1;
}
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
options.c_oflag &= ~(OPOST);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &options);
while (1) {
memset(buf, 0, sizeof(buf));
if (read(fd, buf, sizeof(buf)) > 0) {
printf("Received: %s\n", buf);
}
}
close(fd);
return 0;
}