乐鑫技术交流
直播中

符筹荣

7年用户 942经验值
私信 关注
[问答]

jpeg相机连接esp8266,读取缓冲区时文件以正确的魔术字 (0xFF 0xD8) 开头单不结尾是为啥?

我买了一个 jpeg 相机 (C429-L36) 来连接我的 esp8266。
通过在互联网上找到的一些示例,我首先在我的计算机上完成了这个程序来测试协议。
代码:全选    int main()
    {

    int USB = open( "/dev/ttyUSB1",  O_RDWR | O_NOCTTY | O_SYNC );
    struct termios tty;
    /* Set Baud Rate */
    cfsetospeed (&tty, (speed_t)B115200);
    cfsetispeed (&tty, (speed_t)B115200);

    /* Setting other Port Stuff */
    tty.c_cflag     &=  ~PARENB;            // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
   
    tty.c_cflag     &=  ~CRTSCTS;           // no flow control
    tty.c_cc[VMIN]   =  1;                  // read doesn't block
    tty.c_cc[VTIME]  =  15;                  // 0.5 seconds read timeout
    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl     lines

    /* Make raw */
    cfmakeraw(&tty);
   
    tcsetattr ( USB, TCSANOW, &tty ) ;
   
   
    char response[261];
    unsigned char cmd[20];
   
    cmd[0] = 0x56;
    cmd[1] = 0x00;
    cmd[2] = 0x11;
   
    cmd[3] = 0x00;
   
    write(USB, cmd, 4);
    sleep(1);
   
    read(USB, response, 16);
   
    for(int i = 0; i< 16;++i)
    {
       printf("%c", response);
       fflush(stdout);
    }
   
    cmd[0] = 0x56;
    cmd[1] = 0x00;
    cmd[2] = 0x3;
    cmd[3] = 0x01;
    cmd[4] = 0x00;
   
    write(USB, cmd, 5);
    sleep(1);
    read(USB, response, 5);
   
    cmd[0] = 0x56;
    cmd[1] = 0x00;
    cmd[2] = 0x34;
    cmd[3] = 0x01;
    cmd[4] = 0x00;
   
    tcflush(USB,TCIOFLUSH);

    write(USB, cmd, 5);
   
   
    sleep(1);
   
   
    int n = 0;
   
    n = read( USB, response, 9 );
   
    int length = 0;
    unsigned int res;
    if(n == 9)
    {
       if(response[3] == 0x00 && response[4] == 0x04)
       {
       res = (response[8] & 0xFF);
       res += (response[7] & 0xFF) << 8;
       res += (response[6] & 0xFF) << 16;
       res += (response[5] & 0xFF) << 24;   
       }
    }
   
   
    int addr = 0;
   
    FILE* f =  fopen("photo.jpg", "wb");
   
    int bytes = 255;
    while(addr != res)
       {
   
       if(res - addr < bytes)
          bytes = res-addr;
       else
          bytes = bytes;
   
       printf("%d\n", addr);
       fflush(stdout);
   cmd[0] = 0x56;
   cmd[1] = 0x00;
   cmd[2] = 0x32;
   cmd[3] = 0x0C;
   cmd[4] = 0x00;
   cmd[5] = 0x0A;
   cmd[6] = (addr >> 24) & 0xFF;
   cmd[7] = (addr >> 16) & 0xFF;
   cmd[8] = (addr >>  8) & 0xFF;
   cmd[9] = (addr & 0xFF);
   cmd[10] = 0;
   cmd[11] = 0;
   cmd[12] = 0;
   cmd[13] = bytes;
   cmd[14] = 1;
   cmd[15] = 0;


   write(USB, cmd, 16);

   sleep(1);

   read(USB, response, 5);

   read(USB, response, bytes);

   fwrite(response, bytes, 1, f);

   read(USB, response, 5);

   addr += bytes;
   }

    fclose(f);
   
    return 0;
    }

它运行良好,控制器版本良好,FBuffer 长度良好,当我读取缓冲区时,文件以正确的魔术字 (0xFF 0xD8) 开头,但没有以正确的魔术字 (0xFF 0xD9) 结尾,这很奇怪,因为文件大小与宣布的 FBuffer 大小一致。
有人可以帮助我吗?

回帖(1)

低调de炫耀爱

2023-10-9 16:25:31
设置其他串口参数 */    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8 bits per byte    tty.c_iflag = IGNPAR; // ignore parity errors    tty.c_oflag = 0; // no processing of output    tty.c_lflag = 0; // no processing of input    /*    打开读取图片的命令并发送   */    write(USB,"?CMD=STILL",16);    /*  等待直到缓冲区准备好  */    sleep(1);    /*  读取缓冲区的图像数据  */    unsigned char read_buffer[BUFFER_SIZE];    int num_bytes = read(USB, &read_buffer, BUFFER_SIZE);    printf("Received %d bytes from cameran", num_bytes);    /*  检查文件头  */    if (read_buffer[0] == 0xFF && read_buffer[1] == 0xD8) {    printf("Found JPEG magic headern");    } else {    printf("No JPEG magic header foundn");    }    /*  关闭串口  */    close(USB);    return 0;  } 运行后,我发现缓冲区的第一字节和第二字节是正确的魔术字节 (0xFFD8),但是当我检查缓冲区的最后几个字节时,我发现它们并不是正确的 JPEG 文件结尾字节。这是为什么?有什么解决方法吗?
举报

更多回帖

×
20
完善资料,
赚取积分