-2

我正在尝试通过 C 中的串行线路发送数据。我怀疑我的问题在于我设置的选项,但我无法弄清楚。

在下面的程序中,我发送字节 0x79、0x80、0x81,但在另一端,我收到 0x86、0x06、0x00

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

/*
 * 'open_port()' - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error.
 */
char SERIAL_DEVICE[] = {"/dev/ttyUSB0"};
struct termios options;

void open_port(int* fd)
{
    *fd = open(SERIAL_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);

    if (*fd == -1){
        /* Could not open the port. */
        perror("open_port: Unable to open serial port - ");

    } else 
        fcntl(*fd, F_SETFL, 0);

}

void send(int message[], size_t bytes_to_send, int* fd){
    int n,i;

    for(i=0;i<bytes_to_send;i++){
        printf("%x", message[i]);
    }
    n = write(*fd, message, bytes_to_send);
    if (n < 0)
      fputs("write() failed!\n", stderr);
}

int main(){

    int fd; /* File descriptor for the port */

    int message[] = {0x79, 0x80, 0x81};

    // load the options structure with the cuurent port options
    tcgetattr(fd, &options);        
    cfsetispeed(&options, B19200); // set in baudrate
    cfsetospeed(&options, B19200); // set out baudrate
    options.c_cflag |= (CLOCAL | CREAD);

    // No Parity 8N1
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_iflag &= ~(IXON | IXOFF | IXANY);
    options.c_iflag |= (IGNPAR | ISTRIP);

    //raw input and output
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag &= ~OPOST;

    // Flush buffers and set new options
    tcsetattr(fd, TCSAFLUSH, &options);

    open_port(&fd);

    int array_length = sizeof(message) / sizeof(message[0]);    
    int i;
    for(i=0;i<array_length;i++){
        printf("%x", message[i]);
    }

    send(message, array_length, &fd);

    close(fd);
    return 0;
}
4

1 回答 1

2

你所有的 termios 东西都是无效的。您正在更改随机文件描述符(fd未初始化)的终端特征,因为您在 AFTER 之后打开了串行端口tcsetattr。如果您检查tcgetattrandtcsetattr调用上的错误,您就会发现这一点。此外,如果您注意编译器发出的警告,您就会发现它。

然后,您的功能有两个问题send

  • send它与系统调用同名。这在技术上是可以的,因为您send覆盖了sendfrom libc 并且一切正常,但它令人困惑(当我第一次阅读代码时,我认为您正在使用send系统调用)并且很危险(如果您send在程序的其他地方使用系统调用)。
  • 它将一个ints 数组传递给write,但write需要一个字节数组。结果,当您尝试写入 0x79 时,最终会写入 4 个字节 0x00 0x00 0x00 0x79(对于 big-endian 32 位平台)或 0x79 0x00 0x00 0x00(对于 little-endian 32 位平台)。
于 2012-12-18T19:04:16.230 回答