Not Print HEX Data

Hello,

Would you please tell me why the data is showing “special characters” in stead of “HEX”. How I can fix this? Please see attached for the serial print.

and another problem I had was. The camera take only one snap shot and fails to sync next one (even I put in the loop). I have to manually re-power cycle (remove the camera and plug in again) is able to run the second snap shot. Thanks

here is the Arduino code:

#include "CameraC329SPI.h"
#include <SPI.h>
#include <SdFat.h>
SdFat SD; 

File photoFile;
int CS = 10; //set Pin 10 as the CS pin.

CameraC329SPI camera(CS); 
uint32_t writtenPictureSize;

void getPicture_callback(uint32_t pictureSize, uint16_t packetSize, uint32_t packetStartPosition, byte* packet)
{
  
  photoFile.write(packet, packetSize);
  Serial.write (packet, packetSize);
  writtenPictureSize += packetSize;

  if (writtenPictureSize >= pictureSize)
  {
    photoFile.flush();
    photoFile.close();
    Serial.println("SUCCESS");
  }
}

void setup() {
  

 if (!SD.begin(4)) 
  {
    Serial.println("SD initialization failed or Missing SD card");
    return;
  }
  else
  {
    Serial.println("SD Card initialized");
  }
  delay(2000);// wait for SD open file

  char filename[] = "C329Ph00.jpg";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      photoFile = SD.open(filename, FILE_WRITE);
      photoFile.seek(0);
      writtenPictureSize = 0;
      Serial.println("opened picture file");
      break;  // leave the loop!
    }
  }
  if (!photoFile)
  {
    Serial.println("couldnt create file");
    return;
  }
  Serial.println("card initialized.");
  Serial.print("Logging to: ");
  Serial.println(filename);
  delay (3000); //Wait for Camera Sync

  pinMode(CS, OUTPUT);

  SPI.begin();
  SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
  digitalWrite(CS, HIGH); //unselect
  SPI.endTransaction();
  delay(2000); //time for the module to load stuff from EEPROM
  if (!camera.sync())
  {
    Serial.println("OH NO - Sync failed");
    
   camera.powerOff();
    SPI.end();
    return;
  }
  else {
    Serial.println("Camera sync");
  }

  if (!camera.initialize(CameraC329SPI::BAUD921600, CameraC329SPI::CT_JPEG, CameraC329SPI::PR_160x120, CameraC329SPI::JR_320x240))
  {
    Serial.println("Initialize failed");
    return;
  }
  else {
    Serial.println("Camera initialized");
  }

  if (!camera.setQuality(CameraC329SPI::QL_BEST))
  {
    Serial.println("Set quality failed");
    return;
  }
  else {
    Serial.println("Camera quality set");
  }

  if (!camera.getPicture(CameraC329SPI::PT_JPEG_PREVIEW, &getPicture_callback))
  {
    Serial.println("Get Picture Failed");
    return;
  }
  else {
    Serial.println ("Picture taken");
  }
    if (!camera.reset(CameraC329SPI::RT_STATE))
    photoFile.close();
  Serial.println("Finished writing data to file");
}
void loop()
{
  
   delay (1000);
}

How is the camera connected? Looks like the camera is using the hardware serial of the arduino to communicate.

  Serial.write (packet, packetSize);

Why on earth are you sending binary data to an application that expects ASCII data?

What would your recommendation to change that "serial.write" to?

Yes, The camera is connected to Arduino hardware serial to communicate.

What would your recommendation to change that "serial.write" to?

Something else, obviously. The camera data is not human-readable data. Why do you assume that it is?

You're apparently writing the camera data to a file. If you want to examine it, I'd be inclined to check it out on a PC with a hex editor if you don't know what format it is and a more appropriate rendering application if you do.

If you want the binary bytes to be displayed in hexadecimal:

  for (int i=0; i<packetSize; i++)
  {
    photoFile.print(packet[i], HEX);
    photoFile.print(' ');
    Serial.print(packet[i], HEX);
    Serial.print(' ');
  }

PaulS:

  Serial.write (packet, packetSize);

Why on earth are you sending binary data to an application that expects ASCII data?

TRLE19:
What would your recommendation to change that "serial.write" to?

@OP
1. If we view A as a character of the English Language, then we use this bit pattern 01000001 (or 0x41 in hex format) for its representation. This bit representation is called/known as ASCII code/ASCII value. To see this character in human-friendly display like Serial Monitor/PC Monitor/LCD, we must send this ASCII code to the said monitor. Either one of the following two instructions can be executed to present the character A on the Serial Monitor?

Serial.print('A');
Serial.write(0x41);
not this one: Serial.print(0x41); //it will show: 65

2. If we view A as a digit of the hexadecimal set (0 - 9, A - F), then we use this bit pattern 00001010 (or 0x0A in hex representation) for its representation. This bit representation is called/known as Binary code/Binary value. To see this numeral in human-friendly display like Serial Monitor/PC Monitor/LCD, can we execute this instruction: Serial.write(0x0A);?

No! We can't execute this instruction as we have seen in Step-1 that the Serial.write(arg); instruction will show printable character on the Serial Monitor if its argument (arg) is the ASCII code of the intended character/digit. Therefore to see the numeral/digit A on the Serial Monitor, we have to convert the Binary value 0x0A into ASCII code of A by adding 0x37 (byte x = 0x0A + 0x37 = 0x41) and then execute this instruction: Serial.write(x);.

3. Now, let us come to the mechanics of this instruction: Serial.write(packet, packetSize);.
(1) Because of write() method, the packet must be an array. Let us execute these codes:

byte packet[] = {0x31, 0x32, 0x41};
Serial.write(packet, sizeof(packe));  //shows: 12A

Because the elements have coincided with the ASCII codes of the characters 1 2 A, 12A has appeared on Serial Monitor.

(2) Because of write() method, the packet must be an array. Let us execute these codes:

byte packet[] = {0x11, 0x12, 0x21};
Serial.write(packet, sizeof(packe));  //shows: artifacts

Because the elements have not coincided with the ASCII codes of any human-friendly characters, the artifact symbols have appeared on Serial Monitor. If we have a binary machine that is connected with the UART Port other than the ASCII type display unit, that machine will certainly accept these binary codes and will interpret them as per pre-negotiated rules (the protocol).

The conclusion is this: Serial.write(); function can transmit both ASCII codes and Binary codes.