Unwanted characters sent on Serial

Hi everybody,

i'm currently working on a project that implements a communication between a Arduino mega 2560 and a sensor named a photomultiplicator through a rs232 adaptator. This sensor is wired on the Serial2 of the board and it has a strict list of commands that we can send to him. Once the command send, it responds by sending 2 characters that represent a report of the good reception of the command. For instance i send : D carriage_return and it replies VA for saying the command is valid.

Anyway before even plug the sensor to the board, i link the board to computer with its RS232 connector so i check the data i plan to send. I'm on Linux so i open a cutecom (a terminal to chat with Arduino through RS232) and when i plug the board, so when the program starts to run i notice that i receive 2 null characters -> 0x00 twice.
This is a big problem to me because the sensor will read them and the first command i'll try to give him it will reject it.
Is there a way to flush the Serial2 before or somehow solve this? i check the flush method on the serial library but it doesn't match with what i want.

Sorry if the english ain't good and thanks in advance

Is it the sensor that sends 0x00 twice ?
And you don't want those values in the Arduino ?
You can read them and throw them away.

The RS-232 signals are -15..-3 to +3..+15V, you can not connect those to the Arduino board, you need a converter.

Can you give a link to the photomultiplicator ?

If I understand correctly you want the Arduino Mega to send commands to the sensor device using Serial2. You say you connect Serial2 to your PC (using cutecom on the PC) and it appears to send 0x00 to the PC when the Mega is started up.

How is the Mega powered?

Can you post a copy of your code or (if it is long) a short sketch that illustrates the problem.

...R

No it's the contrary actually.
Through the command cutecom on my computer in a terminal, i can see that 2 0x00 are sent to the sensor right after downloading the program into the board and each time the program reload (for instance each time i open a monitor in the arduino ide).

Then remove the code that is sending this data from your program!

Mark

That's precisely the issue, i'm not sending those datas...

I only go with :
Serial2.begin(9600);

then a little further :

Serial2.print("D\r");

and when i watch on a terminal what datas are being transmitted i got :
0x00 0x00 D \r

So where is the diagram of your hardware and where is the code. No one else has problems with extra chars being sent so the problem is at your end. With the diagram and the code no one can help you.

Mark

You normally need physical pull-up resistors on the TXn pins to prevent then
floating at powerup. The RX/TX pins itself already have them I believe, but the rest default
to INPUT.

I was avoiding posting my code since i don't have only the main sketch on arduino but other external cpp's.
I'm only pasting here the code where lies all the Serial2 calls. We got here the main sketch and the Photomultiplicateur.cpp which is the class for my sensor (sorry my variable names are in french :s )

SKETCH :

 #include <Regexp.h>
#include <Photomultiplicateur.h>

MatchState monMatchState;
char isMatched;
Photomultiplicateur* PM;

String requeteSerial2 = "";
boolean requeteCompleteSerial2 = false;
String requeteSerial1 = "";
boolean requeteCompleteSerial1 = false;
int compteurData = 0;
long valeurRequete = 0;


void setup()
{
  Serial.begin(9600);
  PM = new Photomultiplicateur; 
}

void loop()
{
  if (requeteCompleteSerial1)
  {
    Serial.print("La commande depuis le PC : ");
    Serial.println(requeteSerial1);
    char buffer[100];
    requeteSerial1.toCharArray(buffer, 100);
    buffer[requeteSerial1.length()] = '\0';
    monMatchState.Target(buffer);  
    if((isMatched = monMatchState.Match("#C")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
    switch (requeteSerial1.charAt(2))
    {
      case 'I':
      {
        if((isMatched = monMatchState.Match("^#CI$")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
        else
          PM->initialiserPhotomultiplicateur();
        break;
      }
      case 'S':
      {
        if((isMatched = monMatchState.Match("^#CS$")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
        else
          PM->arreterPhotomultiplicateur();
        break;
      }
      case 'D':
      {
        if((isMatched = monMatchState.Match("^#CD$")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
        else
          PM->demarrerLecture();
        break;
      }
      case 'L':
      {
        if((isMatched = monMatchState.Match("^#CL[0-2][0-5][0-5]$")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
        else
          PM->setNombreLectures(requeteSerial1.substring(3,6).toInt());
        break;
      }
      case 'P':
      {
        if((isMatched = monMatchState.Match("^#CP[0-1][0-9][0-9]$")) == REGEXP_NOMATCH)
		Serial.println("Commande Incorrecte.");
        else
          PM->setPeriodeEchantillonnage(requeteSerial1.substring(3,6).toInt());
        break;
      }
      default : break;
    }
    requeteSerial1 = "";
    requeteCompleteSerial1 = false;
  }
  if(requeteCompleteSerial2)
  {
    Serial.print("La reponse depuis le PM : ");
    Serial.println(requeteSerial2);
    int compte_rendu = PM->interpreterReponse(requeteSerial2);
    switch(compte_rendu)
    {
      case 0: break;
      case MAUVAISE_COMMANDE :
      {
        Serial.println("Photomultiplicateur: mauvaise commande recue.");
        break;
      }
      case MAUVAIS_ARGUMENT:
      {
        Serial.println("Photomultiplicateur : mauvais argument dans la commande recue.");
        break;
      }
      default:
      {   
        Serial.print("valeur ok");
        break;
      }
    }
    requeteSerial2 = "";
    requeteCompleteSerial2 = false;
    compteurData = 0;
  }
}

void serialEvent()
{
  while (Serial.available())
  {
    char caractereEntrant = (char)Serial.read(); 
    if (caractereEntrant == '\n')
      requeteCompleteSerial1 = true;
    else
      requeteSerial1 += caractereEntrant;
  }
}

void serialEvent2()
{
  while (Serial2.available())
  {
    if(PM->flagLecture == false)
    {
      char caractereEntrant = (char)Serial2.read();
      requeteSerial2 += caractereEntrant; 
      if (requeteSerial2.length() == 2)
        requeteCompleteSerial2 = true;
    } 
    else
    {
      long valeurEntrante = Serial2.read();
      Serial.print("Valeur entrante :");
      Serial.println(valeurEntrante);
      long valeurPonderee = valeurEntrante << (8*(3-compteurData));
      valeurRequete += valeurPonderee;
      if(++compteurData == 4)
      {
        requeteCompleteSerial2 = true;
        requeteSerial2  = String(valeurRequete);
        valeurRequete = 0;
      }
    }
  }   
}

PHOTOMULTIPLICATEUR.CPP

#include "Photomultiplicateur.h"

Photomultiplicateur::Photomultiplicateur()
{
	Serial2.begin(9600);
	flagLecture = false;
        compteurLectures = 0;
        nbLectures = 0;
}
void Photomultiplicateur::initialiserPhotomultiplicateur()
{
	Serial2.print("D\r");
}

void Photomultiplicateur::arreterPhotomultiplicateur()
{
        flagLecture = false;
        nbLectures = 0;
        compteurLectures = 0;
	Serial2.print("V");
        Serial2.write(0);
        Serial2.write(0);
        Serial2.print("\r");
	
}

void Photomultiplicateur::setNombreLectures(int _nbLectures)
{
        nbLectures = _nbLectures;
        Serial2.print("R");
        Serial2.write(nbLectures);
        Serial2.print("\r");
}

void Photomultiplicateur::setPeriodeEchantillonnage(int _periode) // nombre de 10ms
{
        Serial2.print("P");
        Serial2.write(_periode);
        Serial2.print("\r");
}
   

void Photomultiplicateur::demarrerLecture()
{
	Serial2.print("S\r");
	flagLecture = true;
}

int Photomultiplicateur::interpreterReponse(String reponse) // faire l'overflow
{
	if (reponse == "VA")
		return 0;
	else if(reponse == "BC")
		return MAUVAISE_COMMANDE;
	else if(reponse == "BA")
		return MAUVAIS_ARGUMENT;
	else
	{ 
           if (flagLecture == true)
           {
               // faire le traitement
               compteurLectures++;
               if (compteurLectures == nbLectures)
               {
                  flagLecture = false;
                  compteurLectures = 0;
                  nbLectures = 0;
                  Serial.println("Lecture finie");
               }
               return 0;
           }
           else
           {
              Serial.println("Reponse inconnue");
              return 0; // faire une autre erreur
           }
 	}
}