Write Reply to serial port

Hi.

I try to use a LinkSprite JPEG Color Camera Serial UART Interface With Infrared with my Arduino.

So I connected the wires according to this :

Name descriptions
RED RXD (IN) → Pin 5
Brown TXD( OUT) → Pin 4
Purple GND
Gray 5V DC

The Reset Command is documented with

Reset
Command (HEX)
56 00 26 00
Return (HEX)
76 00 26 00

So my Code is

#include <SoftwareSerial.h>

SoftwareSerial cameraSerial(4,5); 
void setup()
{ 
  Serial.begin(9600);
  cameraSerial.begin(38400);
}

void loop() 
{
  if( Serial.available() )
  { 
    char btdata;
    btdata = Serial.read();     
    if (btdata == 'r'){
      resetCommand();
    } 
  }
}

void resetCommand()
{
  cameraSerial.print(0x56, HEX);
  cameraSerial.print(0x00, HEX);
  cameraSerial.print(0x26, HEX);
  cameraSerial.print(0x00, HEX);
  Serial.println("Reset Answer START");
  while(cameraSerial.available()>0)
  {
    byte answerbyte = cameraSerial.read();
    Serial.println(answerbyte, HEX); 
  }
  Serial.println("Reset Answer END");
}

So if i type in “r” in the serial monitor, i would expect “76 00 26 00” as the answer. Unfortunatly I do not get an reply besides “Reset answer” Start and end.

cameraSerial.readBytes(buffer, 5);
for (int i = 0 ;i < 5;i++)
{
      Serial.println(buffer[i], HEX); 
}

doesn´t work either.

cameraSerial.available()

is 0 so there isn´t anything to read.

Am I doing anything wrong, or do I have a logical error somewhere ?

So if i type in “r” in the serial monitor, i would expect “76 00 26 00” as the answer.

I can’t see why you might expect that - can you explain your reasoning, please?

Well.

If I send rvia Serial to the arduino it executes the resetCommand() function. I know that this part works, because it sends "Reset Answer START" and "Reset Answer END" back to the monitor via serial.

The resetCommand() also sends the reset command 56 00 26 00 to the camera and since it should return "76 00 26 00" I would expect that the arduino recieves that reply and forwards it to m machine.

while(cameraSerial.available()>0)
  {
    byte answerbyte = cameraSerial.read();
    Serial.println(answerbyte, HEX);
  }
  Serial.println("Reset Answer END");

So this is what i expect, but it does not work, so there must be an error somewhere (code or logic).

  cameraSerial.print(0x00, HEX);
  Serial.println("Reset Answer START");
  while(cameraSerial.available()>0)

How long do you think it takes the Arduino to execute that code? How long do you think it takes your camera to receive the command, act on it and respond?

Hi.

I´m not quite sure, what a good delay would be (I would think 2-3 seconds most) , but i tried lots of them between 0 and 7500ms and added them after the cameraSerial.print and before the Serial.println.

AWOL:   cameraSerial.print(0x00, HEX);   Serial.println("Reset Answer START");   while(cameraSerial.available()>0)

Unfortunatly that doesn't help.

In that case, have you checked that the camera is receiving any commands, and responding after any length of time? Have you checked the connections to the camera?

Problem solved.

Indeed I did not get a response from the camera, because

  mySerial.print(0x56, HEX);
  mySerial.print(0x00, HEX);
  mySerial.print(0x26, HEX);
  mySerial.print(0x00, HEX);

did not work the way I expected. It actually did not send the hex value for 0x56 to the cam, but the hex value of the chars 5 and 6 for example. So the cam did not recieve a proper command.

 byte bytes_to_send[4] =  { 0x56, 0x00, 0x26, 0x00 };
  mySerial.write(bytes_to_send,4);

did the job and i did recieve the response. Of course i also do need some delays.