Problem with SoftWareSerial

Hello,

I can't use SoftWareSerial on may Arduino Uno :-(. It's a problem because i would like to use a ttl-camera on pin 10 and 11.
When i try this code from "Arduino Reference" :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

the only thing i see when i open a terminal is : "Goodnight moon" !
Some one understand where is the problem ? mySerial.available is always 0 ???
Thank you very much for your help.

Some one understand where is the problem ? mySerial.available is always 0 ???

What is connected to pins 10 and 11 that is supposed to be sending serial data in/receiving serial data?

These loopback examples don't make it obvious, and the reference documentation is about as useful as a chocolate teapot, but SoftwareSerial can't send and receive at the same time so you can't just loop a SoftwareSerial port back to itself by crossing its Tx and Rx pins over. If you want to test it, you need to send to/receive from some other device.

Hello,

Thank youvery much for your answers.
I would like to test this fonction of the arduino Uno because, previously i have connected a ttl camera on pin 10,11 and i could'nt synchronize.
Thirst, i thought that my camera was out of order, but with an other one this was the same problem. Impossible to connect.
Here is the code

/* material conditions :
  - arduino on usb port (digital I/O D0 and D1 on Rx and Tx),
  - Vcc cam on PINCAM,      
  - CamOv528 Tx (white) -> D10 (Rx) and Rx (yellow) -> D11 (Tx). Possible with SofwareSerial library */
#include <SoftwareSerial.h>
#define PINCAM 3
SoftwareSerial CamSerial(10,11); 
void envoiCommande(char * cmd,unsigned char longueur){
        for(unsigned char i=0;i<longueur;i++)CamSerial.print(cmd[i]);
}

void initialiseCam()
{
        unsigned char nbessais=0;
        //SYNC
        char sync[] = {0xaa,0x0d,0x00,0x00,0x00,0x00} ;
        char ack1[]={0x00,0x00,0x00,0x00,0x00,0x00},ack2[]={0x00,0x00,0x00,0x00,0x00,0x00};
        CamSerial.setTimeout(500);
        while (1)
        {
                nbessais++;
                Serial.println(nbessais);
                //UNO send ACK
                envoiCommande(sync,6);
                if (CamSerial.readBytes(ack1, 6) != 6)
                {
                        continue;
                }
                //first cam ACK OK ?
                if (ack1[0] == 0xaa && ack1[1] == 0x0e  && ack1[2] == 0x0d && ack1[4] == 0x00 && ack1[5] == 0x00)
                {
                        //wait for a the second ACK
                        if (CamSerial.readBytes(ack2, 6) != 6) continue;
                        //second ACK OK ?
                        if (ack2[0] == 0xaa && ack2[1] == 0x0d && ack2[2] == 0x00 && ack2[3] == 0x00 && ack2[4] == 0x00 && ack2[5] == 0x00) break;
                }
        }
        //create the answer
        sync[1] = 0x0e;
        sync[2] = 0x0d;
        envoiCommande(sync, 6);
        Serial.println("Réponse de la camera :");
        Serial.print("Nb of try : ");
        Serial.println(nbessais);
        Serial.println("Thirst ACK :");
        for (unsigned char i;i<6;i++)Serial.print(ack1[i]);
        Serial.println("Second ACK :");
        for (unsigned char i;i<6;i++)Serial.print(ack2[i]);
}
void setup(){
        pinMode(PINCAM,OUTPUT);
        digitalWrite(PINCAM,HIGH);
        Serial.begin(9600);//initialisation serail port PC
        CamSerial.begin(115200);//initialisation serial port cam
        initialiseCam();
}
void loop(){
}

I need a second serial port because i would like to keep the original serial (D0, D1) to control with a terminal.
Finally, i had some doubts with my UNO and later i thought that SoftwareSerial did'nt work :frowning:
I am worried about my project :frowning:
Thank you very much.

And what output do you get in the serial monitor?

What's the cams default baud rate , is it 115200?

Mark

Thank you Mark.

The cams default rate is 115200 bds (it is written in the data-sheet).

I get only the values of the counter "nbessais" in the serial monitor : 1, 2, 3 .... 100,... :frowning:

You're writing binary data including nulls - I think you want to use CamSerial.write(), not CamSerial.print().

The serial port speed may also be part of your problems. Although the documentation suggests that it supports speeds up to 115200 bps, in practice the higher the speed the more sensitive comms are to timing glitches, and you might find it won't actually work reliably at speeds that high in your application.

The readBytes(char *, int) you're using to read back the response isn't documented and isn't implemented in the copy of SoftwareSerial in my 1.0.4 installation. What does it do? In particular, what does it do if the number of bytes available to be read is less than the second argument?

Thank you very much Peter for your message.

I have simplified and and change my code :

/* material conditions :
   - arduino on usb port (digital I/O D0 and D1 on Rx and Tx),
   - Vcc cam on PINCAM,      
   - CamOv528 Tx (white) -> D10 (Rx) and Rx (yellow) -> D11 (Tx). Possible with SofwareSerial library */
#include <SoftwareSerial.h>
#define PINCAM 3
SoftwareSerial CamSerial(10,11); 
void envoiCommande(char * cmd,unsigned char longueur){
        for(unsigned char i=0;i<longueur;i++)CamSerial.write(cmd[i]);
}

void initialiseCam()
{
        unsigned char nbessais=0;
        char sync[] = {0xaa,0x0d,0x00,0x00,0x00,0x00} ;
        CamSerial.setTimeout(500);
        while (1)
        {
                Serial.println(CamSerial.available());
                //UNO send SYNCK
                envoiCommande(sync,6);
                if (CamSerial.available()==0)
                {
                        continue;
                }
                break;
        }
}
void setup(){
        pinMode(PINCAM,OUTPUT);
        digitalWrite(PINCAM,HIGH);
        Serial.begin(9600);//initialisation serial port PC
        CamSerial.begin(115200);//initialisation serial port cam
        initialiseCam();
}
void loop(){
}

and i get only a list of 0 in the terminal...so no character in SerialCam's buffer !

Maybe the problem is that the bauds rate is too hight but that is the default rate of the cam and i can't change it before synchronization....
Thank you for your help.

swingzazou:
and i get only a list of 0 in the terminal...so no character in SerialCam's buffer !

Maybe the problem is that the bauds rate is too high but that is the default rate of the cam and i can't change it before synchronization....
Thank you for your help.

if 115200 bauds is really the default speed, you could temporarily change your code, connect the camera on the hardware serial pins,
make the init, synchro, and change the bauds rate to 9600 bauds. Then, back to your test with softwareserial .

With the latest code posted, if Serial.available() returns any value except zero at line 22 the code will break out of the while loop without doing anything with the response. It will also keep sending commands continually as fast as the serial port will allow.

While you're trying to get it working, I suggest you send your sync command once in setup() and then in loop read any byte that is available on the CamSerial port and just print it to the hardware Serial port so you can see it. In other words, something like this:

includes and declarations omitted ...
void setup()
{
         pinMode(PINCAM,OUTPUT);
         digitalWrite(PINCAM,HIGH);
         Serial.begin(9600);//initialisation serial port PC
         CamSerial.begin(115200);//initialisation serial port cam
         char sync[] = {0xaa,0x0d,0x00,0x00,0x00,0x00} ;
         envoiCommande(sync, sizeof(sync));
}

void loop()
{
    if(CamSerial.available() > 0)
    {
        int input = CamSerial.read();
        Serial.print("Received [");
        Serial.print(input, DEC);
        Serial.println("]");
    }
}

Thank you for your time. Without your help, i'd be lost....
I will try tomorrow your suggestions and i will tell you.
Thanks.

Hello,

Alnath, i have tested your suggestion but i get nothing in the terminal :frowning:
Peter, i have connected pins on the hardware serials and change the code and made tests with leds.

/*materials conditions :
  - Vcc cam in PINCAM,      
  - Camera Ov528  : Tx-camera(white) in D1 et Rx-cam(YELLOW) in D0.*/
#define PINCAM 5
#define GREEN 6
#define RED 7
#define YELLOW 8
//si on ne veut pas aborder les pointeurs, on se passe de cette fonction qui prend en paramètre l"adresse du tableau de char
void envoiCommande(char * cmd,unsigned char longueur){
        for(unsigned char i=0;i<longueur;i++)Serial.write(cmd[i]);
}
void clearRx()
{
        while (Serial.available())
        {
                Serial.read();
        }
}
void initialiseCam()
{
        char i;
        //synchronization  (page 9 data-sheet)
        char sync[] = {0xaa,0x0d,0x00,0x00,0x00,0x00};
        //cam's answers
        char ack1[]={0x00,0x00,0x00,0x00,0x00,0x00},ack2[]={0x00,0x00,0x00,0x00,0x00,0x00};
        Serial.setTimeout(500);
        while (1)
        {
                //I don't know if it is necessary...        
                clearRx();
                //to see the yellow led twinckle
                delay(500);
                i=0;
                if (digitalRead(YELLOW)==HIGH){
                        digitalWrite(YELLOW,LOW);
                }
                else
                {
                        digitalWrite(YELLOW,HIGH);
                }

                envoiCommande(sync,6);
                if (Serial.available()>0)
                {
                        digitalWrite(RED,HIGH);
                        if (Serial.available()<7)
                        {
                                digitalWrite(GREEN,HIGH);
                                digitalWrite(YELLOW,LOW);

                                break;
                        }
                }
        }
}
void setup(){
        int rate[]={9600,14400,19200,28800,38400,57600,115200};
        pinMode(PINCAM,OUTPUT);
        pinMode(GREEN,OUTPUT);
        pinMode(RED,OUTPUT);
        pinMode(YELLOW,OUTPUT);
        digitalWrite(PINCAM,HIGH);
        //test led
        digitalWrite(YELLOW,HIGH);
        digitalWrite(RED,HIGH);
        digitalWrite(GREEN,HIGH);
        delay(2000);
        //initial state
        digitalWrite(YELLOW,LOW);
        digitalWrite(RED,LOW);
        digitalWrite(GREEN,LOW);
        Serial.begin(9600);
        delay(100);
        initialiseCam();
}
void loop(){
}

I have tried all rates. The results :
115200, 57600, 38400, 19200, 14400, 9600 : Serial.available() greater than 6 ! (Red : On Green : Off).
28800 : Serial.available()=0 (Red : Off, Green : Off).
But in the data-sheet, it's said 6 continous single byte !!! (8-bit contents, start bit=0, stop-bit=1, the default config of Serial.begin() no ? ).
I don't understand :frowning:
Thank and good evening.