读取33个字节后,UART读取块

时间:2013-08-30 10:22:16

标签: c

我通过uart为微控制器制作了一个C程序flash图像。在这段代码中,我遵循一个协议,一切都进展顺利。但是当我通过uart将图像加载到微控制器时,写入工作正常但是读取33个字节后读取被阻塞。我正在逐字节地写入和读取...声明&加载图像的函数定义如下:

功能声明:

load_RAM_image(file_size,file_buff);

这里file_size是unsigned int,它的值是40,980KB,file_buff是指向图像缓冲区的unsigned char指针。

功能声明:

#define BYTE_WRITE 1

#define DELAY 10000

int load_RAM_image(unsigned int buff_size, unsigned char *buff)
{
    unsigned int count, num_Wbytes, num_Rbytes, byte;
    for (count = 0; count < buff_size;)  /* increase count value to every time */
    { 
        num_Wbytes = write( serial_fd, buff + count, BYTE_WRITE );
        if (num_Wbytes < 0)
        {
            fputs("write failed!\n", stderr);
            return -1;
        }

        tcdrain(serial_fd);
        usleep(DELAY);
        num_Rbytes = read( serial_fd, rbuff + count, BYTE_WRITE );
        if (num_Rbytes < 0)
        {
            if (errno == EAGAIN)
            {
                printf("SERIAL EAGAIN ERROR\n");
                return -EAGAIN;
            }
            else
            {
                printf("SERIAL read error %d %s\n", errno, strerror(errno));
            }
        }

        printf("wbuf[ %d ] = %02x   rbuff = %02x\n\n", 
               count /*+ byte*/, *(buff + count/* + byte*/), rbuff[count]);

        /* Compare the received byte from BAM */
        if (strncmp(buff + count, rbuff + count, BYTE_WRITE ) < 0)
        {
            printf("Error : RAM loading error(W != R)\n");
            return -1;                      
        }

        count += BYTE_WRITE  ;
        printf("count = %d\n", count);
    }

    return 0;
}

0 个答案:

没有答案