Go Down

Topic: Tricky linksprite camera problem with hw serial (Read 964 times) previous topic - next topic

Wibble82

I've hooked up a linksprite webcam to an arduino (uno and mega), with moderate success, using the sample code from the sparkfun web site. With a bit of tinkering to update the code to compile on 1.0, I have images coming out of the camera and through the serial, to an app on the pc that displays the jpeg. All good so far, but the use of softwareserial is slow, so I hook it up to serial2 on the arduino mega.

Now this I'm having major issues with - it initially just seemed to stop responding, however with some digging I discovered I'm getting some data back through the serial connection multiple times. I get the command response, then rather than image data, I get the command response back again, followed by image data, and things end up out of sync.

Has anybody had any issue using the linksprite camera with hardware serial, or is aware of any hardware serial issues in general i should know?

-chris

Wibble82

In case anybody hits the same issue, turns out none of the example code waits for the 5 bytes at the end of image data. The examples all go:
- request data
- read 5 byte response
- read rest of msg and take 32 bytes

What they don't make clear, and the manual is very subtle about is that the data will actually be in the form:
<5 byte response> <32 bytes image data> <5 byte response>

You get away with it on software serial, as it's slow enough for the 5 byte response to arrive and get flushed. However on hardware serial you need to make sure you wait for the full 42 bytes before moving onto the next msg.

-Chris

contecavour

Hi Chris i avent understood.

Above my code
What's wrong?

Thanks
Ciao
Nicola


Code: [Select]

/* Linksprite */

#include <SoftwareSerial.h>
#include <SD.h>

byte incomingbyte;
SoftwareSerial mySerial(6,7);                     //Configure pin 4 and 5 as soft serial port
//NewSoftSerial mySerial(6,7);                     //Configure pin 4 and 5 as soft serial port
int a=0x0000,j=0,k=0,count=0;                    //Read Starting address       
uint8_t MH,ML;
boolean EndFlag=0;
                               
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

File imgFile, imgFile2, imgFile3, imgFile4;

void setup()
{
  Serial.begin(38400);
  mySerial.begin(38400);
 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
 
}

void loop()
{
     SendResetCmd();
     delay(4000);             //After reset, wait 2-3 second to send take picture command
     
     imgFile = SD.open("nic20.jpg", FILE_WRITE);
     imgFile2 = SD.open("nic21.jpg", FILE_WRITE);
     imgFile3 = SD.open("nic22.jpg", FILE_WRITE);
     imgFile4 = SD.open("nic23.jpg", FILE_WRITE);
     
     
      SendTakePhotoCmd();

     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();

      }   
      byte a[32];
     
      while(!EndFlag)
      { 
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();

         delay(25);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag))
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))      //Check if the picture is over
               EndFlag=1;                           
               j++;
       count++;
               }
          }
         
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
             
              imgFile.print(a[j],HEX);
              imgFile2.write(byte(a[j]));             
              //imgFile3.print(byte(a[j]));              sembra sbagliato
              //imgFile4.print(a[j]);              sembra sbagliato
             
              Serial.print(" ");
          }                                       //Send jpeg picture over the serial port
          Serial.println();
      }     
     
     imgFile.close();
     imgFile2.close();
     imgFile3.close();
     imgFile4.close();
     while(1);
}

//Send Reset command
void SendResetCmd()
{
      mySerial.write(0x56);
      mySerial.write(byte(0x00));
      mySerial.write(0x26);
      mySerial.write(byte(0x00));
}

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.write(0x56);
      mySerial.write(byte(0x00));
      mySerial.write(0x36);
      mySerial.write(0x01);
      mySerial.write(byte(0x00)); 
}

//Read data
void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.write(0x56);
      mySerial.write(byte(0x00));
      mySerial.write(0x32);
      mySerial.write(0x0c);
      mySerial.write(byte(0x00));
      mySerial.write(0x0a);
      mySerial.write(byte(0x00));
      mySerial.write(byte(0x00));
      mySerial.write(MH);
      mySerial.write(ML);   
      mySerial.write(byte(0x00));
      mySerial.write(byte(0x00));
      mySerial.write(byte(0x00));
      mySerial.write(0x20);
      mySerial.write(byte(0x00)); 
      mySerial.write(0x0a);
      a+=0x20;                            //address increases 32£¬set according to buffer size
}

void StopTakePhotoCmd()
{
      mySerial.write(0x56);
      mySerial.write(byte(0x00));
      mySerial.write(0x36);
      mySerial.write(0x01);
      mySerial.write(0x03);       
}






contecavour

Great Chris,.

I have used your post and now all works!!!!!!!!!!!

Thanks
Ciao

contecavour

Now i want to shoot a second photo.
If i launch cam.takephoto, i've obtain the some phote, not a new photo.
Why?

N

Go Up