为什么不能从“读取”中获取数据到 uint8_t 数组?

Why can not get data from " read" into uint8_t array?

请指教。 我想让代码从串行端口获取数据,例如 tty/USB0。 所以,需要让这段代码从串行协议读取数据,如下所示。

uint8_t recBlk; // receive blk from chunk
ret = read(serial_fd, &recBlk, sizeof(recBlk));
printf("Block Num is %d\n", recBlk);
fflush(stdout);
if (ret != sizeof(recBlk)) {
    perror("read");
    return -errno;
}

但是我不明白为什么下面显示的这些行不起作用,而是出现错误 "read: invalid argument"

如何从读取函数中获取数组数据?

uint8_t recData[1024]; // receive data from chunk
ret = read(serial_fd, (void *)&recData, sizeof(recData));
    printf("Data buffer is %c\n", &recData);
    fflush(stdout);
if (ret != sizeof(recData)) {
     printf("Can't fetch the Data length!");
     perror("read");
     return -errno;
}

下面显示的这段代码是我的实现函数。

static int xmodem_receive(int serial_fd, char* filename, int _crc, int use_crc){
    int skip = 0;
    uint8_t sdCRC = 'C'; // Request-To-Send
    uint8_t sdACK = X_ACK; //
    uint8_t eof = X_EOF;
    uint8_t sdNAK = X_NAK;
    uint8_t recSTX; // receive SOH from chunk
    uint8_t recBlk; // receive blk from chunk
    uint8_t recNegBlk; // receive blk negative from chunk
    uint8_t recData[1024]; // receive data from chunk
    uint16_t recChksum;
 
    uint8_t expected_blkno;
 
    FILE *fp;
    int ret, fd;
    struct stat stat;
    // If we want to receive, We have to send NAK to Sendor.
 
    if (use_crc)
        use_crc = MAX_RETRY + 1;
 
        /* Writing as binary */
    fp = fopen(filename, "wb");
    //Send NAK still read SOH that header of one data chunks
    /*
    CRC 16
    Sending C
    Sending NAK
 
    */
    while(use_crc){
        char status;
        printf("Waiting for sender ping ...");
        fflush(stdout);
        //
        if(_crc){
            printf("Send C ping....\n");
            ret = write(serial_fd, &sdCRC, sizeof(sdCRC));
        }else{
            // send NAK before read SOH
            printf("Send NAK ping....\n");
            ret = write(serial_fd, &sdNAK, sizeof(sdNAK));
        }    // after sending NAK,receiving SOH from chunk
        fflush(stdout);
        ret = read(serial_fd, &recSTX, sizeof(recSTX));
 
 
        if(recSTX == X_STX){
            printf("STX is %c\n", &recSTX);
            break;
        }else{
            printf("Garabage payload %c\n", &recSTX);
        }
        fflush(stdout);
        if (ret != sizeof(recSTX)) {
                printf("Not working");
                fflush(stdout);
                perror("read");
                return -errno;
        }
        use_crc--;
    }
 
    expected_blkno = 1;
 
    while(ret != EOF){
        //So next, receiving blk
        ret = read(serial_fd, &recBlk, sizeof(recBlk));
        printf("Block Num is %d\n", recBlk);
        fflush(stdout);
        if (ret != sizeof(recBlk)) {
            perror("read");
            return -errno;
        }
        ret = read(serial_fd, &recNegBlk, sizeof(recNegBlk));
        printf("Negative Block Num is %d\n", recNegBlk);
        fflush(stdout);
        if (ret != sizeof(recNegBlk)) {
            perror("read");
            return -errno;
        }
        ret = read(serial_fd, (void *)&recData, sizeof(recData));
        printf("Data buffer is %c\n", &recData);
        fflush(stdout);
        if (ret != sizeof(recData)) {
            printf("Can't fetch the Data length!");
            perror("read");
            return -errno;
        }
        ret = read(serial_fd, &recChksum, sizeof(recChksum));
        printf("Check sum is %c", &recChksum);
        fflush(stdout);
        if (ret != sizeof(recChksum)) {
            printf("Can't fetch the Check sum");
            perror("read");
            return -errno;
        }
        // data block number check
        if(recBlk == 0xff - recNegBlk){
            printf("Can't fetch the Block !");
            perror("read");
 
            return -errno;
        }
        // data integrity check
        if(recChksum == swap16(crc16(recData, sizeof(recData)))){
            perror("read");
            return -errno;
        }
        // check of appended "0xff" from end of data to  end of chunk in chunk
        unsigned char *ptr = recData;
        ptr += sizeof(recData);
        while(*ptr == 0xff){
            ptr--;
        }
        fwrite(recData, (ptr - recData),1,fp); // write Datas bytes from our buffer
        // set skip flag or end connect
        ret = write(serial_fd, &eof, sizeof(uint8_t));
        if (ret != sizeof(eof) || ret != sizeof(X_STX)){
            return -errno;
        }else{
            if(ret == X_STX){
                skip = 1;
                printf("next chunk");
                fflush(stdout);
                expected_blkno++;
            }else if(ret == EOF){
                printf("EOF ...");
                ret = write(serial_fd, &sdACK, sizeof(sdACK));
                break;
            }else{
                return -errno;
            }
        }
 
    }
    printf("done.\n");
    fclose(fp);
    return 0;
 
}

termous 设置在这里。

static int open_serial(const char *path, int baud)
{
        int fd;
        struct termios tty;

        fd = open(path, O_RDWR | O_SYNC);
        if (fd < 0) {
                perror("open");
                return -errno;
        }

        memset(&tty, 0, sizeof(tty));
        if (tcgetattr(fd, &tty) != 0) {
                perror("tcgetattr");
                return -errno;
        }

        cfsetospeed(&tty, baud);
        cfsetispeed(&tty, baud);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        tty.c_iflag &= ~IGNBRK;                         // disable break processing
        tty.c_lflag = 0;                                // no signaling chars, no echo,
                                                        // no canonical processing
        tty.c_oflag = 0;                                // no remapping, no delays
        tty.c_cc[VMIN]  = 1;                            // read doesn't block
        tty.c_cc[VTIME] = 5;                            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY);         // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);                // ignore modem controls,
                                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);              // shut off parity
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr(fd, TCSANOW, &tty) != 0) {
                perror("tcsetattr");
                return -errno;
        }

        return fd;
}

产生“错误”的代码是:

    uint8_t recData[1024]; // receive data from chunk
  ...
        ret = read(serial_fd, (void *)&recData, sizeof(recData));
        printf("Data buffer is %c\n", &recData);
        fflush(stdout);
        if (ret != sizeof(recData)) {
            printf("Can't fetch the Data length!");
            perror("read");
            return -errno;
        }

1.) 第一个问题是C语法。
recData 是数组的地址,在 read()printf() 调用中,该地址的地址作为第二个参数传递。
对数组地址的address-of操作不正确。

2.) 第二个问题是return值的评估。
手册页描述了 read() 系统调用的可能 return 值。

2a.) errno变量仅在return值为-1时有效。
但是,只要 return 值不等于数组的字节数,代码中的 perror() 就会被调用。
因此 "read: Invalid argument" 消息很可能是伪造的。

2b.) read() 系统调用不需要满足请求的长度参数。
您的 termios 配置指定最少读取 1 个字节。
因此 read() 系统调用将 return 至少有一个字节,并且只有串行端口驱动程序当前接收到的数据,最多为请求的长度。
因此,每当无法完成 1024 字节的完整读取(这很可能总是如此)时,您的程序将中止并出现虚假错误。