C 串行设备:读取8N1工作,但写入单个字节失败

C 串行设备:读取8N1工作,但写入单个字节失败,c,linux,serial-port,C,Linux,Serial Port,在我的程序中,我从串行设备Linux 8N1中读取数据,没有任何问题。但是如果我想写出一个字节,我在接口上什么也得不到。我假设我的串行输出设置是错误的。但并没有太多的方法来设置LAG的c_ 我的代码: #define TTYDEVICE "/dev/ttyS0" #define BAUDRATE B9600 int openSerialDevice(const char* devTTY, struct termios oldTio) { //----< Open serial devic

在我的程序中,我从串行设备Linux 8N1中读取数据,没有任何问题。但是如果我想写出一个字节,我在接口上什么也得不到。我假设我的串行输出设置是错误的。但并没有太多的方法来设置LAG的c_

我的代码:

#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进行嗅探也是一样的


有什么建议吗?谢谢

你如何确认没有结果

您可以尝试删除RTSCT,然后重试。事实上,如果您希望将来自tty层的干扰降至最低,则应使用以下方法将终端设置为raw:

cfmakeraw(&newtio);

你如何确认什么都没有出来

您可以尝试删除RTSCT,然后重试。事实上,如果您希望将来自tty层的干扰降至最低,则应使用以下方法将终端设置为raw:

cfmakeraw(&newtio);
你给write一个ACK的数据参数,它是指向int的指针。这可能不是你的意思。这意味着write将看到一个缓冲区,其中包含字符{6,0,0,0}小端或{0,0,0,6}大端。这假设sizeof int==4为真,根据需要调整其他大小,问题仍然存在

您很可能应该将缓冲区改为无符号字符。另外,如果您是这样打电话的:

int wrote = write(fd, ACK, sizeof ACK);
printf("Wrote %d bytes\n", wrote);
你会得到直接的反馈。您应该测试类似的内容,以查看写入是否确实成功。

您给write指定的数据参数是ACK,它是指向int的指针。这可能不是您的意思。这意味着write将看到一个缓冲区,其中包含字符{6,0,0,0}小端或{0,0,0,6}大端。这假设sizeof int==4为真,根据需要调整其他大小,问题仍然存在

您很可能应该将缓冲区改为无符号字符。另外,如果您是这样打电话的:

int wrote = write(fd, ACK, sizeof ACK);
printf("Wrote %d bytes\n", wrote);

你会得到直接的反馈。您应该进行类似的测试,以查看写入是否确实成功。

激活的硬件流控制CRTSCTS是写入被阻止的原因,最终串行输出上没有显示任何内容

谢谢

代码快照,它可以工作:

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;

    fcntl(fileDescriptor, F_SETFL, 0);
    // set everything to 0
    bzero(&newtio, sizeof(newtio));

    // again set everything to 0
    bzero(&newtio, sizeof(newtio));

    newtio.c_cflag |= BAUDRATE; // Set Baudrate first time
    newtio.c_cflag |= CLOCAL; // Local line - do not change "owner" of port
    newtio.c_cflag |= CREAD; // Enable receiver

    newtio.c_cflag &= ~ECHO; // Disable echoing of input characters
    newtio.c_cflag &= ~ECHOE;

    // set to 8N1
    newtio.c_cflag &= ~PARENB; // no parentybyte
    newtio.c_cflag &= ~CSTOPB; // 1 stop bit
    newtio.c_cflag &= ~CSIZE; // Mask the character size bits
    newtio.c_cflag |= CS8; // 8 data bits

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


    // Set teh baudrate for sure
    cfsetispeed(&newtio, BAUDRATE);
    cfsetospeed(&newtio, BAUDRATE);

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

    tcflush(fileDescriptor, TCIFLUSH); // flush pending data

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

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

激活的硬件流控制CRTSCTS是写入被阻止的原因,最终串行输出上没有显示任何内容

谢谢

代码快照,它可以工作:

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;

    fcntl(fileDescriptor, F_SETFL, 0);
    // set everything to 0
    bzero(&newtio, sizeof(newtio));

    // again set everything to 0
    bzero(&newtio, sizeof(newtio));

    newtio.c_cflag |= BAUDRATE; // Set Baudrate first time
    newtio.c_cflag |= CLOCAL; // Local line - do not change "owner" of port
    newtio.c_cflag |= CREAD; // Enable receiver

    newtio.c_cflag &= ~ECHO; // Disable echoing of input characters
    newtio.c_cflag &= ~ECHOE;

    // set to 8N1
    newtio.c_cflag &= ~PARENB; // no parentybyte
    newtio.c_cflag &= ~CSTOPB; // 1 stop bit
    newtio.c_cflag &= ~CSIZE; // Mask the character size bits
    newtio.c_cflag |= CS8; // 8 data bits

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


    // Set teh baudrate for sure
    cfsetispeed(&newtio, BAUDRATE);
    cfsetospeed(&newtio, BAUDRATE);

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

    tcflush(fileDescriptor, TCIFLUSH); // flush pending data

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

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

同意。RTSCTS表示硬件握手,通常不用于9600n81。这仅在计数器部件也触发行时有效。在硬件中连接RTS和CTS也应该使相同的程序工作,或者用适当的零调制解调器电缆将其连接到另一台pc,并记住在那里配置RTS/CTS握手。同意。RTSCTS表示硬件握手,通常不用于9600n81。这仅在计数器部件也触发行时有效。在硬件中连接RTS和CTS也可以使相同的程序工作,或者使用适当的零调制解调器电缆将其连接到另一台pc,并记住在那里配置RTS/CTS握手。