gpt4 book ai didi

c - 串行设备 : Reading 8N1 works, 但写入单个字节失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:59 26 4
gpt4 key购买 nike

在我的程序中,我从串行设备(Linux,8N1)读取没有任何问题。但是在我想写出一个字节的情况下,我在界面上什么也得不到。我假设我的串行输出设置是错误的。但是设置c_oflag的方法并不多...

我的代码:

#define TTYDEVICE "/dev/ttyS0"
#define BAUDRATE B9600

int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}

// set new parameters to the serial device
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

// set to 8N1
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;

newtio.c_iflag = IGNPAR;

// output mode to
//newtio.c_oflag = 0;
newtio.c_oflag |= OPOST;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */
newtio.c_cc[VMIN] = 0; /* blocking read disabled */

tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}

//----< / Open serial device >----------------------------------
return fileDescriptor;
}

int ACK[1] = { 6 };

int main() {
// old termios to restablish
struct termios oldTIO;
// filedescriptor
int fd;

fd = openSerialDevice(TTYDEVICE, oldTIO);

if ((fd == -1) | (fd == -99)) {
perror("Could not open TTY-Device. Exit on failure!");
return EXIT_FAILURE;
}

write(fd, ACK, 1); // Problem !!


return 0:
}

现在,如果我使用

screen /dev/ttyS1 9600 8n1

验证/dev/ttyS0 上的内容。我什么也看不见。如果我用 Docklight 1.8 嗅探,也是一样。

有什么建议吗?谢谢

最佳答案

你如何验证什么都没有出来?

您可以尝试删除 RTSCTS,然后重试。事实上,如果你希望来自 tty 层的干扰最小,你应该将你的终端设置为原始的,使用这个:

cfmakeraw(&newtio);

关于c - 串行设备 : Reading 8N1 works, 但写入单个字节失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com