Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';linux中的t读串行端口_C_Linux_Serial Port - Fatal编程技术网

Can';linux中的t读串行端口

Can';linux中的t读串行端口,c,linux,serial-port,C,Linux,Serial Port,我需要学习如何读写串口。出于测试目的,我将传输线连接到接收线,并用示波器检查信号行为。我可以看到写入是正常的:1个数据字节从Tx传输到Rx。但是函数read(fd,&rcv_val,1)返回-1 使用了不同的端口:“/dev/ttyUSB0”、“/dev/ttyS5” 我在这个网站上看到了类似的问题,但这并不能帮助我解决问题 代码: int res=0; int fd;/*端口的文件描述符*/ 国际snd_val,rcv_val; fd=开放(“/dev/ttyUSB0”,O|RDWR | O|

我需要学习如何读写串口。出于测试目的,我将传输线连接到接收线,并用示波器检查信号行为。我可以看到写入是正常的:1个数据字节从Tx传输到Rx。但是函数
read(fd,&rcv_val,1)
返回-1

使用了不同的端口:“/dev/ttyUSB0”、“/dev/ttyS5”

我在这个网站上看到了类似的问题,但这并不能帮助我解决问题

代码:

int res=0;
int fd;/*端口的文件描述符*/
国际snd_val,rcv_val;
fd=开放(“/dev/ttyUSB0”,O|RDWR | O|NOCTTY | O|NDELAY);
如果(fd==-1){//无法打开端口。
fprintf(stderr,“打开端口:无法打开/dev/ttyUSB0-%s\n”,strerror(errno));
返回fd;
}
速度波特=B38400;
结构术语设置;
tcgetattr(fd和设置);
cfsetispeed(设置和波特率);//波特率
cfsetospeed(设置和波特率);//波特率
settings.c_cflag&=~PARENB;//不对等
settings.c_cflag&=~CSTOPB;//1停止位
settings.c_cflag&=~CSIZE;
设置.c_cc[VMIN]=1;
设置.c_cc[VTIME]=0;
settings.c|u cflag |=(CS8 | CLOCAL | CREAD);
tcflush(fd,tcflush | TCIFLUSH);
tSetTTR(fd、tCanow和设置);//应用设置
snd_val=0xA5;
写入(fd和snd_val,1);
如果(res<0){
printf(“Err wr\n”);
返回res;
}
else printf(“发送确定\n”);
res=读取(fd和rcv_val,1);
如果(res<0){
printf(“Err rd\n”);
返回res;
}else printf(“rcv_val=0x%02X\n”,rcv_val);
返回res;

读取(2)
“On error,-1被返回,errno被正确设置。”
这意味着您应该检查
errno
或调用
peror
。我建议使用
minicom
或类似工具进行检查,以确保安全性(即端口实际上可以从中读取)。还请注意,您正在非阻塞模式下打开fd
O\u NDELAY
。返回的错误可能是
EAGAIN
(因为调用
read
时可能没有1字节就绪)。Errno对应于“资源暂时不可用”。现在我看看它的意思。首先,您可以测试您的环回“电缆”:[我删除了设置O_NDELAY。现在进程冻结在read()上,没有任何消息。
int res = 0;
int fd; /* File descriptor for the port */
int snd_val, rcv_val;

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1){//Could not open the port.
    fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n", strerror(errno));
    return fd;
}

speed_t baud = B38400;
struct termios settings;
tcgetattr(fd, &settings);
cfsetispeed(&settings, baud); // baud rate
cfsetospeed(&settings, baud); // baud rate
settings.c_cflag &= ~PARENB; // no parity
settings.c_cflag &= ~CSTOPB; // 1 stop bit
settings.c_cflag &= ~CSIZE;
settings.c_cc[VMIN] = 1;
settings.c_cc[VTIME] = 0;
settings.c_cflag |= (CS8 | CLOCAL | CREAD);
tcflush(fd, TCOFLUSH | TCIFLUSH);
tcsetattr(fd, TCSANOW, &settings);// apply the settings

snd_val = 0xA5;
write(fd, &snd_val, 1);
if (res < 0){
    printf("Err wr\n");
    return res;
}
else printf("send ok\n");

res = read(fd, &rcv_val, 1);
if (res < 0){
    printf("Err rd\n");
    return res;
}else printf("rcv_val = 0x%02X\n", rcv_val);

return res;