在我的程序中,我從串口設備(Linux,8N1)讀取沒有任何問題。但是在我想寫出一個字節的情況下,我沒有在界面上看到任何東西。我假設我的串行輸出設置是錯誤的。但有沒有那麼多的方式如何設置c_oflag定義...串行設備:讀取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/9600就是ttyS1 8N1
來驗證/ dev/ttyS0上出現的內容。我什麼也看不見。如果我使用Docklight 1.8進行嗅探,那也是一樣。
有什麼建議嗎?謝謝
同意。 RTSCTS表示硬件握手,通常不用於9600n81。這隻有在線路也由櫃檯部分觸發時纔有效。在硬件中連接RTS和CTS也應該使相同的程序工作,或者用適當的(空調制解調器)電纜將它連接到另一臺PC,並記得在那裏配置RTS/CTS握手。 – Adriaan 2009-10-13 14:08:22