RS232 Serial Communication with a digital multimeter

Hi everybody,

I have a digit programmable multimeter HM8012 (Hameg Instruments) that has a RS232 serial communication.
I want to plug it in the TX1 RX1 (pins 18/19) of my Arduino Mega2560 and then read the multimeter from my computer (which is plug to Arduino through usb).

I've read the manual so i know the commands, like the one to get the current value of the multimeter (S? = carriage return).
I wrote something in arduino but that doesn't work at all, i'm receiving random values.

The manual indicates the settings (4800 baud, 8N1) : "Each control must have two ascii code characters followed by a character 13 (). On reception of terminator the equipment sends the character 19 () to indicate that the dialogue is suspended. As soon as it is possible to resume the dialogue, the instruments sends 17 ()".
I tried to wrote a little something to test it but it doesn't work

int octet = 0;
char caractere = 0; 
String chaine = "";
int octet1 = 0;
char caractere1 = 0; 
String chaine1 = "";
boolean flag = false;
boolean flag1 = false;
boolean flag_dialog = true;


void setup()
{
  Serial.begin(9600);
  Serial1.begin(4800);
  Serial.flush();
  Serial1.flush();
}

void loop()
{
  if (flag && flag_dialog)
  {
    Serial.println(chaine);
    int nb = 0;
    nb = Serial1.print(chaine);
    Serial.println(nb);
    flag = false;
    chaine = "";
  }
  if (flag1)
  {
    Serial.println(chaine1);
    flag1 = false;
    chaine1 = "";
  }
    
}

void serialEvent()
{
  while (Serial.available()>0) 
   {
      octet = Serial.read();
      caractere = char(octet);
      chaine += caractere;
      if (octet == 13)
      {
        flag = true;
        break;
      }
   }
}

void serialEvent1()
{
  while (Serial1.available() > 0)
  {
    octet1 = Serial1.read();
    if(octet1 == 19 && flag_dialog == true)
    {
      flag_dialog = false;
      Serial.println("No Dialog");
    }
    else if(octet1 == 17 && flag_dialog == false)
    {
      flag_dialog = true;
      flag1 = true;
      Serial.print("Dialog resumed");
      break;
    }
    else
    {
      caractere = char(octet);
      chaine += caractere;
    }
  }
}

Is the code wrong (i know it's not perfect but just to test something it should be enough right?)
Or is it a physical issue? I've read somewhere that the RS232 serial comm needed 12V, and the arduino provides only 5.

Any thoughts?

Thanks for reading this

The serial ports on your Mega are TTL level (0-5 volts) and standard RS232 for connection to the PC can be -12V to +12V. Some serial uses lower voltages like -5 to +5 but the point is you will damage the Mega by feeding <0 or >5V to the pins. You need a RS232 to TTL converter or bypass the Mega all together and plug it into the PC.

Where are you calling serialEvent()/serialEvent1()?

To Riva : Ok, i've read it indeed somewhere else on the internet so it is true !
I've tried to check the serial communication and send some data through the serial monitor, do i damage my arduino hard? :s

To fungus : i believe those functions are called automatically as soon as data are available for reading, right?

Thanks for answering so quick

siva_dashq:
To fungus : i believe those functions are called automatically as soon as data are available for reading, right?

Oh, that must be a new feature. Apparently it calls them in between each call to loop().

The first thing I'd try is lighting up an LED when data arrives. That way you can tell if data is coming through or not.

Well nothing happens ^^
I'm not getting anything, i think Riva is right there is a communication problem here.
I'm gonna look into a rs232 to ttl converter

Thanks for your patience fungus ^^

http://www.robotshop.com/adaptateur-rs232-ttl-dfrobot.html Will this do the trick ? (sorry it's in french !)

siva_dashq:
http://www.robotshop.com/adaptateur-rs232-ttl-dfrobot.html Will this do the trick ? (sorry it's in french !)

Yes that will allow you to safely connect RS232 to the Arduino serial ports but why use something like that when with something like this you can connect it directly to the PC (assuming your PC does NOT have standard serial port)
Hopefully your Mega is not damaged.

Because i want it to go through the Arduino for my project.
See i'm on my work experience here and i use that multimeter to check the reception of data through serial rs232 wtih arduino.

In fact, it will be with a photo-sensor that has a rs232 serial comm, but i wanted to do some tests first so i wont damage it.

You've been a great help, thanks again Riva.