VC0706 camera and a corrupted jpeg image

Flow_75
Posts: 2
Joined: Thu Aug 09, 2018 6:40 pm

VC0706 camera and a corrupted jpeg image

Postby Flow_75 » Wed Oct 10, 2018 8:30 am

I've bought a jpeg camera (C429-L36) to connect it to my esp32

With some examples found on the internet, I've done this program first on my computer :

Code: Select all

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[i]);
	    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;
    }
it works good, good version of the controller, good FBuffer Length, When i read the buffer, the file begin by the correct magic word (0xFF 0xD8) but doesn't end with the correct one (0xFF 0xD9) which is strange because the file size it coherent with the annouced FBuffer size.

Does anybody could help me ?
Thanks.
Flo.

Who is online

Users browsing this forum: No registered users and 109 guests