我正在尝试使用 write() Unix 系统调用写入字节,但是我发送的任何以 1 开头的 8 位值都会变成 32 位值。我试图查看是否有任何 termios 标志来解决这个问题,但我似乎找不到任何标志。
struct termios config;
if(!isatty(fd)) { std::cout << "Not a TTY" << std::endl; }
if(tcgetattr(fd, &config) < 0) {std::cout << "Termios structure is broken" << std::endl;}
config.c_iflag = 0;//&= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
config.c_oflag = 0; //&= ~(OCRNL | ONLCR | ONLRET | ONOCR | CSTOPB | ONOEOT| OFILL | OPOST);
config.c_cflag &= ~(CSIZE | PARENB | CSTOPB); // 8bits, no parity, 1 stop
config.c_cflag |= CS8 | CREAD | HUPCL;
config.c_cc[VMIN] = 0;
config.c_cc[VTIME] = 1;
// Communication speed (simple version, using the predefined constants)
if(cfsetispeed(&config, B9600) < 0 || cfsetospeed(&config, B9600) < 0) {
std::cout << "Speed is messed up" << std::endl;
}
// Finally, apply the configuration
if(tcsetattr(fd, TCSAFLUSH, &config) < 0) { std::cout << "Applied Configuration is broken" << std::endl;}
// uint8_t c[2] = {'D','A'};
// write(fd,&c,2);
write(fd,&command[6],2);
输入样本:
BIN6: 10111110 -------- HEX6:be -------- DEC6: 190 -------- ASCII6: ?
BIN7: 11101100 -------- HEX7: ec -------- DEC7: 236 -------- ASCII7: ?
输出样本:
BIN6:000000011111111111111111111111110111110 -------- HEX:FFFFFFBE -------- DEC:-66 -------- ASCII:¾
BIN7:00000001111111111111111111111111101100 -------- HEX:FFFFFFEC -------- DEC:-20
知道如何解决这个问题吗?谢谢!