Go Down

Topic: Arduino + TTL Camera +SD (Read 2 times) previous topic - next topic

shaikhmuneer23

Thank you PeterH and Paul.

I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work  :smiley-roll-sweat:

So what if I write these HEX bytes to .txt using a SD library in Arduino 0.22>
How would this data written to the SD card be any different from what I am pasting directly from the monitor to notepad. While I write data to SD is it processing it in some form so that when i just change the extension from .txt to jpg the image should be there ??

Please bear with me...my knowledge is limited in this area.

Thanks

PaulS

Quote
I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work

Of course not. The string representation of a HEX value and the hex value itself are two completely different things. The file you attached has the string representation of some hex values in it, not some hex values.

Quote
So what if I write these HEX bytes to .txt using a SD library in Arduino 0.22

That is what you need to do.

Quote
How would this data written to the SD card be any different from what I am pasting directly from the monitor to notepad.

See above.

Code: [Select]
While I write data to SD is it processing it in some form so that when i just change the extension from .txt to jpg the image should be there ??
The SD library can open a file with any 3 letter extension, so just make it jpg when you create the file. Then there is no need to change it.


shaikhmuneer23

I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work
Quote
PaulS : Of course not. The string representation of a HEX value and the hex value itself are two completely different things. The file you attached has the string representation of some hex values in it, not some hex values.


By hex value itself do you mean the HEX representation with a prefix of 0x
e.g. 3E --> 0x3E. Is this what you mean.

Because before I save it to the SD i want to assure what i need to write to the SD and in which format so I will imitate the same using notepad and see for myself if the trick works.

PaulS

Quote
By hex value itself do you mean the HEX representation with a prefix of 0x
e.g. 3E --> 0x3E. Is this what you mean.

No I do not. You read a byte from the camera:
Code: [Select]
               incomingbyte=mySerial.read();
Write that byte, without converting it to a string, with or without the leading 0x, to the file.

Hint: use the write() method, not the print() method.

shaikhmuneer23

Code: [Select]
* Linksprite */

#include <NewSoftSerial.h>

byte incomingbyte;
NewSoftSerial mySerial(2,3);                     //Configure pin 4 and 5 as soft serial port
int long 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();

void setup()
{
  Serial.begin(9600);
  mySerial.begin(38400);
}

void loop()
{   
     SendResetCmd();
     delay(3000); //ChangeBaudRate();
     
     //SetImageSizeCmd(); 
     //SendResetCmd();
     //delay(3000);                               //After reset, wait 2-3 second to send take picture command
     
      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);
              Serial.print(" ");
          }                                       //Send jpeg picture over the serial port
          //Serial.println();
      }     
     while(1);
}

//Send Reset command
void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE); 
}

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

void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x11, BYTE);
}

void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);       
}
void ChangeBaudRate()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);       
      mySerial.print(0xAE, BYTE);   
      mySerial.print(0xC8, BYTE);             
}


This code is producing the JPEG HEX values and I managed to use a HEXEditor to construct a .JPG from it.
Find the attached .JPG file, I simply copied the HEX values from the Serial Monitor and pasted it into the HexEditor and it generated the string necessary and saved the file as .jpg and there was the IMAGE !!!

However the image has errors and this is not all.
My objective is to save this image from HEX to JPG on an SD.
I purchased a EthernetShield that has the microSD card on board and I want to save the file as JPG on SD. Please help me with this  :smiley-roll:
The CS is pin 4 and pin 53 is to be kept as an output, I have taken all the measures for the SD card to function properly and also am able to write string to a txt file and saved on SD "Hello World"  :P

But I want to write the image hex values as string to the card with .jpg extension. Please advice how I should write to SD that will guarantee that the image will be opened without the same old "Corrupted or loo large image" error that I have been facing since 2 weeks now. How can I get rid of the errors in the pic, I tried playing around with the baud rate because I assumed that may have caused a problem but that did not solve.


PaulS

Quote
This code is producing the JPEG HEX values and I managed to use a HEXEditor to construct a .JPG from it.
Find the attached .JPG file, I simply copied the HEX values from the Serial Monitor and pasted it into the HexEditor and it generated the string necessary and saved the file as .jpg and there was the IMAGE !!!

Way more work than necessary.

Quote
My objective is to save this image from HEX to JPG on an SD.

Piece of cake. Open the file, as picFile. Then change this crap:
Code: [Select]
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
              Serial.print(" ");
          }   

to:
Code: [Select]
              picFile.write(a,count);

shaikhmuneer23

Thank you PaulS
I do get the image on SD, i was printing in the for loop but now i realized tht for loop was meant for HEX values only. All is good now and the image is saved on the SD but the image is BROKEN :smiley-sad-blue:

What is the fix ?

PaulS

Quote
What is the fix ?

Compare the file created on the SD card to the one that you manually created. What are the differences?

Where is your new code?

shaikhmuneer23

Code: [Select]
/* Linksprite */

#include <NewSoftSerial.h>
#include <SD.h>

byte incomingbyte;
NewSoftSerial mySerial(2,3);                     //Configure pin 2 and 3 as soft serial port
long int a=0x0000,j=0,k=0,count=0;                    //Read Starting address       
uint8_t MH,ML;
boolean EndFlag=0;
const int chipSelect = 4;
File imgFile;

void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(9600);
  mySerial.begin(38400);
 
    pinMode(10, OUTPUT);
   
      if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
 
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i/10;
    filename[6] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }

  // Open the file for writing
  imgFile = SD.open(filename, FILE_WRITE);
   
}

void loop()
{   
     SendResetCmd();
     delay(3000); //ChangeBaudRate();
     
     //SetImageSizeCmd(); 
     //SendResetCmd();
     //delay(3000);                               //After reset, wait 2-3 second to send take picture command
     
      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++;
               }
          }
         imgFile.write(a,count);
        /*  for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
              Serial.print(" ");
          }    */                                   //Send jpeg picture over the serial port
          //Serial.println();
      }

imgFile.close();
    Serial.println("finished");
     while(1);
}

//Send Reset command
void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE); 
}

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

void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x11, BYTE);
}

void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);       
}
void ChangeBaudRate()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);       
      mySerial.print(0xAE, BYTE);   
      mySerial.print(0xC8, BYTE);             
}


I am getting same broken image on the SD and also with the HEX editor.
I am guessing the delay or any other parameter is directly affected, I looked for the manual but not much information is available there

PaulS

If you are referring to those stripes and blotches in the image, that looks like an issue with the camera and how it is capturing the image. The compressed jpg file is either correct or it isn't. There is not much way to hose up the transmission and saving of a jpg file while still leaving it semi-usable.

You might want to use the hardware serial port to connect to the camera, now that the code is working, and see if that makes a difference.

shaikhmuneer23

I guess the camera is alright because my friend used my same camera to produce a clean JPEG file but his code was based on some ready made library. However I believe in using something self made which will make it flexible for later changes.

Has anyone else faced the same problem and fixed an broken image...?? some information seems corrupt or missing probably that is causing the pixels to remain gray at some points.



manufwi

You can use this library: https://code.google.com/p/linksprite-serial-cam-arduino/
I wrote it and I get perfect pictures with software and hardware serial (software serial is reliable for me up to 19200 bds).
HTH

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview