C++ 写入/dev/ttyS0后读取0字节
我一直在尝试通过/dev/ttyS设备在linux上执行串行通信,但是当我在写入之后尝试读取它们时,我没有读取任何数据 我有以下代码C++ 写入/dev/ttyS0后读取0字节,c++,c,linux,serial-port,tty,C++,C,Linux,Serial Port,Tty,我一直在尝试通过/dev/ttyS设备在linux上执行串行通信,但是当我在写入之后尝试读取它们时,我没有读取任何数据 我有以下代码 #include <stdio.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <unistd.h> int main() { printf("hel
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
int main() {
printf("hello world\n");
int n;
int fd;
char c;
int bytes;
char buffer[10];
char *bufptr;
int nbytes;
int tries;
int x;
struct termios options;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if(fd == -1) {
perror("open_port: Unable to open:");
} else
tcgetattr(fd, &options);
// Set the baudrate, same speed for both I/O
cfsetispeed(&options, B150);
cfsetospeed(&options, B150);
// Enable reading
options.c_cflag |= (CLOCAL | CREAD);
// Set 'RAW' mode
cfmakeraw(&options);
// Set byte size
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set parity
// options.c_cflag &= ~PARENB;
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_cflag &= ~CSTOPB;
// Set StopBits, @Linux no OneHalf is supproted, so OneHalf and Two are the same
options.c_cflag &= ~CSTOPB;
// Set Handshake options
// options.c_iflag |= CRTSCTS;
// options.c_iflag &= ~CRTSCTS;
// options.c_cflag &= ~( IXON | IXOFF | IXANY );
options.c_cflag |= IXON | IXOFF | IXANY;
// Set Timeouts
options.c_cc[VMIN] = 0; // read() will return after receiving character
options.c_cc[VTIME] = 10; // == 0 - infinite timeout, != 0 - sets timeout in deciseconds
tcsetattr(fd, TCSANOW, &options);
tcflush(fd, TCIOFLUSH);
bytes = write(fd, "ATZ\r",4);
printf(" wrote %d bytes\n", bytes);
bufptr = buffer;
bytes = read(fd, bufptr, sizeof(buffer));
printf("number of bytes read is %d\n", bytes);
perror ("read error:");
for (x = 0; x < 10 ; x++) {
c = buffer[x];
printf("%d ",c);
}
tcflush(fd, TCIOFLUSH);
close(fd);
printf("\n");
return (0);
}
虽然我希望它能读我刚才写的4个字符,但读起来似乎是0字节。如果我将VTIME设置为0,则永远读取块。我曾尝试执行
echo/dev/ttyS0
,但没有输出。你知道这可能是什么原因,以及如何修复吗?你的代码显然是正常的,除了以下事实:
- 您调用
perror(“读取错误”)在调用
之后,而不是在printf(3)
之后,因此调用read(2)
会掩盖调用printf(3)
时可能出现的错误(如果发生)。如果要打印读取的字符数,请在调用read(2)
之前保存printf(3)
的值和errno
返回的值,如果返回的错误为负,则调用read(2)
peror(3)
- 无论如何。
施加一秒钟的超时,这对于重置调制解调器来说太过苛刻了。您的线路设置为:c_cc[VTIME]=10
通常情况下,调制解调器在重置后响应CS8, Parity ODD, one STOP bit, and 150 baudrate
命令,这需要一些时间(通常超过一秒)和默认调制解调器速度(因为您已重置它),而不是您发送ATZ\r
命令的速度 因此,重置调制解调器通常是盲目的,然后您发送一个简单的at
命令,以请求AT\r
响应。对\r\nOK\r\n
命令的响应通常是立即的,而不是对重置命令的响应,并允许调制解调器根据接收到的字符调整其通信设置 当接收到AT\r
和A
序列时,调制解调器总是通过对以高采样频率接收的方脉冲进行采样来调整其速度,然后将速度切换到检测到的且通常进行的速度转换(使其能够以与协商的远程速度不同的速度与调制解调器通话)T
- 您调用
perror(“读取错误”)在调用
之后,而不是在printf(3)
之后,因此调用read(2)
会掩盖调用printf(3)
时可能出现的错误(如果发生)。如果要打印读取的字符数,请在调用read(2)
之前保存printf(3)
的值和errno
返回的值,如果返回的错误为负,则调用read(2)
peror(3)
- 无论如何。
施加一秒钟的超时,这对于重置调制解调器来说太过苛刻了。您的线路设置为:c_cc[VTIME]=10
通常情况下,调制解调器在重置后响应CS8, Parity ODD, one STOP bit, and 150 baudrate
命令,这需要一些时间(通常超过一秒)和默认调制解调器速度(因为您已重置它),而不是您发送ATZ\r
命令的速度 因此,重置调制解调器通常是盲目的,然后您发送一个简单的at
命令,以请求AT\r
响应。对\r\nOK\r\n
命令的响应通常是立即的,而不是对重置命令的响应,并允许调制解调器根据接收到的字符调整其通信设置 当接收到AT\r
和A
序列时,调制解调器总是通过对以高采样频率接收的方脉冲进行采样来调整其速度,然后将速度切换到检测到的且通常进行的速度转换(使其能够以与协商的远程速度不同的速度与调制解调器通话)T
- 除了以下事实外,您的代码显然是正常的:
/dev/ttyS0
?如果您不这样做,并且假设串行端口是RS232端口,那么很容易创建一个:如果您希望读取所写内容,则串行端口的另一端应该将这些字符复制(“回显”)给您。一般来说,您只接收对方发送给您的内容。您是否将物理环回加密狗插入/dev/ttyS0
?如果您不这样做,并且假设串行端口是RS232端口,那么很容易创建一个:如果您希望读取所写内容,则串行端口的另一端应该将这些字符复制(“回显”)给您。更一般地说,你只收到对方发给你的东西。