我已经购买了jpeg相机(C429-L36)。
结合互联网上的一些示例,我已经完成了该程序:
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);
addr += bytes;
}
fclose(f);
return 0;
}
它工作正常,控制器版本良好,FBuffer Length好,当我读取缓冲区时,文件以正确的魔术字(0xFF 0xD8)开始,但未以正确的魔术字(0xFF 0xD9)结尾奇怪,因为文件大小与已声明的FBuffer大小一致。
有人可以帮助我吗? 谢谢。 浮动。