Serial communication (RX and TX) with camera using Arduino

Hello,

First, I wish to apologize for three reasons:
1 - My English is not very fluent. So you may find horrible English mistakes.
2 - I am not familiar with electronics and programming at all. So you may find horrible electronics or programming mistakes.
3 - Serial communication with RX and TX pin have been discussed several time here, however I did not find the solution to my problem.

Here is my problem:
I wish to synchronize a Polaris Vicra Camera system with a force sensor. The idea is to connect the force sensor to the analog pin of an Arduino Uno board. The camera system goes with a usb connector, so I use a serial/usb convertor which RX and TX pins are connected (respectively) to TX and RX pins of the arduino.

According to the camera user manual, you can send instructions to the camera and receive responses. For example, if your send "INIT \r", it responds "OKAY896" if it succeeds or something else if it fails.

My problem is that I do not receive anything. Here is my code:

char incomingBytes[8] ;
int availableBytes ;

void setup()
{
  Serial.begin(9600) ;
  
  // Send instruction
  Serial.write("INIT \n") ;
  
  // Count number of Bytes receives
  availableBytes = Serial.available();
  Serial.print("Number of available bytes: ") ;
  Serial.println(availableBytes,DEC) ;
  
  if (availableBytes < 0)  // If any Bytes is received...
  {
    for(int n=0; n<8; n++)
    {
      incomingBytes[n] = Serial.read() ;  // ... collect them...
      Serial.println(incomingBytes[n]) ;  // ... and show them.
    }
  }
}

void loop()  //  This is the loop to repeat...
{
  // ... but there is nothing inside.
}

As you can see, I tell my program to show me the respond only if there is any. When I try to get it anyway, I have a "ÿ" (no idea what it is) in each incomingBytes case. This means there is no response.

My questions are :
I have to send characters (like "A", "B", "C", etc.) and not number. Is "Serial.write("INIT \n") ;" the appropriate way to write a string instruction in the arduino TX pin?
Normally, should have characters ("A", "B", "C", etc.). How can I collect string using "Serial.read()" function?

Thank you in advance for any suggestion or remark.

I have modified my program. Now, I am using the SoftwareSerial library. This allow me to configure two digital pins (5 and 6 in my case) to act respectively like RX and TX. however, this is not still not working.

Here is my code:

#include <SoftwareSerial.h>

int availableBytes ;
byte outcomingBytes[7] ;
byte incomingBytes[8] ;

SoftwareSerial mySerial(5, 6); // RX, TX

void setup()
{
  Serial.begin(9600) ;
  mySerial.begin(9600) ;
    
  // Send instruction
  mySerial.write("INIT \n");
  Serial.println("") ;
  
  availableBytes = mySerial.available();  // Count number of Bytes receives
  Serial.print("Number of available bytes: ") ;
  Serial.println(availableBytes,DEC) ;
  
  if (mySerial.available())  // If any Bytes is received...
  {
    while(mySerial.available() > 0)
    {
      int i = 0 ;
      incomingBytes[i] = mySerial.read() ;  // ... collect Bytes
      Serial.println(incomingBytes[i]) ;  // ... and show them
      i++ ;
      delay(2000) ;
    }
  }
}

Does anyone have any idea of where the problem come from and how to solve it?

Thank you.

You try something, that is not possible. The camera is a USB slave as is the Arduino. You can connect both to the PC and talk from the PC to the camera and to the Arduino. But you cannot connect the two directly because none of them can act as a USB host (the Arduino theoretically can with the help of a USB host shield but you would have to program the driver yourself).

Pylon, thank you for this important information. I am very surprised, I have found many tutorials where they explain how to control different sort of things with arduino like gyroscopes, cameras (for example: Linksprite JPEG Camera | Robotic Controls) or GPS (for example: Adafruit Ultimate GPS Breakout | Robotic Controls) which are also slave, right? So I though it would be possible to control a camera. I do not see major the difference. In this configuration, does the problem come from the presence of a serial/usb converter between them? (because this is the only difference my modest knowledge allows me to figure out)

So if the only solution would be to plug them directly to the computer, then I should use the same program to communicate with them because I want them to be synchronized. Is it a problem to talk with two usb ports at the same time? Do you thing it is possible to do this between 20 to 50ms?

Thank you in advance for any suggestion or comment.

I am very surprised, I have found many tutorial where they explain how to control different sort of things with arduino (like gyroscopes, cameras), so I though it would be possible to control a camera. I do not clearly see the difference.

An Arduino is able to talk to myriad of devices, you might even get it to talk to your camera if costs and effort are no problem. I just wanted you to notice that it won't work the way you explained it. The reason for that is in the design of the USB. It has one master (usually a PC) and one to several slaves (usually peripheral devices like printers, mice, keyboards and the like). The USB interface on the Arduino was made to connect it easily to a PC for programming and not to act as a PC. The same holds true for the camera. It's USB interface let's a PC control it, so it's a slave device as the Arduino is.
You cannot directly connect two devices of the same kind. So you cannot connect two PCs directly by a USB cable, the standard even dictates different connectors for master (A-connector) and slaves (B-connector, which is available in 3 standardized sizes) to make that difference obvious to the user.
As I wrote in theory you can connect your camera to the Arduino by using a USB host shield. But this only gives you the electrical connection. You write that the camera uses a serial emulation to communicate. There are several different chips on the market that do such a conversion from a serial to a USB interface, each needs a separate driver on the PC. While on the PC most of them have drivers, most if not all don't have one on the Arduino. So you might have a very hard time to develop such a driver.

Is it a problem to talk with two usb port at the same time?

You cannot do that at the same time but almost, at least you won't notice, so fast is it.

Do you thing it is possible to do this between 20 to 50ms?

That depends on the speed your camera is able run on it's serial interface (or the emulation of it on the USB). Also you have to measure how much delay you have from sending a command on that interface until the reaction from the camera comes (as taking a picture for example). For most cameras this delay time is longer than 50ms. The Arduino is able to react in such a time frame, but if it will do that depends largely on the calculations it has to do before showing a reaction.

Pylon, thank you again.

Just to make sure I have described my problem accurately. Here is a picture of my experience (I admit I should have done this at the beginning). Can you please look at it? So this is not possible unless I program a camera driver for the arduino which is very difficult, yet impossible for me :(.

In this case, your suggestion to plug the camera directly is better. I will work on this direction. I am thinking about Matlab because someone just gave me a code to control the camera with it (and I am afraid of Visual C). I will just have to program a way to communication with the sensor via arduino on Matlab.

Thank you very much for your help :).

NB: The SD is to record data, but not necessary anymore because Matlab can do it direction on the computer. Interrupter is to start and and recording data.

The serial to USB converter is a slave USB device as is the camera. You should not be able to connect the two together that is why they have the same end. You have used a lead extender to do this this is a very wrong thing to do.
As mentioned you need a USB host shield on your arduino which is actually another computer board to even get the hardware right.