在我的程序中,我从串行设备(linux,8n1)读取没有任何问题。但是如果我想写一个字节,我在接口上什么也写不出来。我认为我的串行输出设置是错误的。但是没有那么多的方法来设置标签…
我的代码:

#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:
}

现在,如果我用
屏幕/dev/ttys1 9600 8n1
以验证/dev/ttys0上的内容。我什么也看不见。如果我在1.8号码头嗅到同样的气味。
有什么建议吗?谢谢

最佳答案

你怎么确认什么都没有出来?
您可以尝试删除RTSCT,然后重试。事实上,如果您希望TTY层的干扰最小,您应该使用以下命令将终端设置为RAW:

cfmakeraw(&newtio);

10-08 08:07