Reading data of temperature measuring unit via RS232

Hey guys,

I hope you can help me with my first big project. I have to write a code that should read the data of a temperature measuring unit and show it on a display. Therefore I use an Arduino Mega that is connected via a RS232 interface with the unit. To get the data I have to send with the Arduino commands like #01 or #014 to the modules. Then I will get the data of all sensors like this:

+051.23+041.53+072.34-023.56+100.00-051.33+066.46+074.22<

My main problem is how can I send my commands in a proper way ( because of the # ???). And the second problem is to separate the data for each sensor. I tried some ways but I always get useless answers…

The parts I use are:

  • Arduino Mega 2560
  • 2 x EX-9018 meassuring modules Extradaq (communicate via RS485)
  • convertermodule EX-9520 from RS485 to RS232
  • MAX232
  • EVALeDIPTFT43 TFT Touch-Display
  • as RS232 Interface I tried to use the one attached

Thanks for your help!!! :wink:

max232.gif

RobinRuebe:
My main problem is how can I send my commands in a proper way ( because of the # ???). And the second problem is to separate the data for each sensor. I tried some ways but I always get useless answers…

You probably didn't mean to use the smiley icon ???) there. The forum software has mis-interpreted what you wrote.

Sending the commands is the easy part. This code is an example, since you didn't actually say what these commands do, I have invented names for them...

char CmdFetchAll[] = "#01";
char CmdDoSomething[] = "#04";

void sendFetchCommand() {
  Serial.print(CmdFetchAll);
}
void doTheSomething() {
  Serial.print(CmdFetchAll);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  sendFetchCommand(); //always fetch new data at the top of the loop
  ...
  if(SomeCondition) {
    doTheSomething()
  } else {
    //condition not met - don't do the something
  }
}

Now go and read Serial Input Basics to understand how to parse the input string into useful variables.

Hi Morgan,
thanks for help! The link helped me quite a lot. The smiley was my mistake, just wanted to write a "?".
The command #01 will send back all eight measured data of module 1. Usually I just need #01 and #02. The code #014 will fetch the data of sensor 4 connected to the module 1.
The modules are connected to Serial1 of the Arduino. At the time I try to display the results on the PC. The goal is to show them later on a small TFT display, that will be connected to another serial-port.
I'm a littlbe bit confused where the difference between your sendFetchCommand() and doTheSomething() should be.
I'm also a little bit unsure if I have to send an Serial1.read command after I sent the doTheSomething(). Could I be possible that the modules respond directly without waiting for a read command? If so, how can I get my information? Are they maybe stored in the memory ?? Sorry guys for this stupid questions. I'm a pretty arduino noob...

My main problem is how can I send my commands in a proper way ( because of the # ???). And the second problem is to separate the data for each sensor. I tried some ways but I always get useless answers…

I'm not quite clear here "how can I send my commands in a proper way". Most commands are fairly specific. How are they not 'proper' You don't give much info about the sensors but as far as the (sensor) replies are concerned then have a look at "strtok" in <string.h>

These will break up your received reply on boundaries specified by you.

Another consideration might be that 232 is becoming a rare beast on machines these days. USB is excellent for shorter runs. Without knowing what you are doing I can't really comment though.

The modules are connected to Serial1 of the Arduino. At the time I try to display the results on the PC. The goal is to show them later on a small TFT display, that will be connected to another serial-port.

Have you been able to send the commands from the serial monitor and have the returned data displayed in the serial monitor?

Hi guys,
sorry I was on a trip abroad and had no possibility to work on my project…
At the time my code looks like this:

char getAll1[] = "#01";
char getAll2[] = "#02";
String inputString = "";
boolean stringComplete = false;

void sendgetAll1(){
  Serial1.print(getAll1);  
  }

void sendgetAll2 () {
  Serial1.print(getAll2);
  }

void serialEvent() {
  while (Serial1.available() && !stringComplete ){
  char inChar = (char)Serial1.read();
  inputString += inChar;
  stringComplete = (inChar == '>');
  }
    Serial.println(inputString);
      inputString = "";
      stringComplete = false;
}

void parseString()  {
  String s = inputString;
  String ausgabe = "";
  int x = 1;                // Zahl für Sensoreingang
  int y = 1;                // Zahl für Ziffer
  boolean stringEnd = false;
  while (!stringEnd)
  {
      char c = s[y];
      switch (c)
      {
          case '+':                     // wenn das Zeichen ein + ist, neuen Wert beginnen
              Serial2.println(ausgabe);
              ausgabe = "Temp";
              ausgabe += x;
              ausgabe += ": ";
              x++;
              ausgabe += c;
              break;
          case '-':                     // wenn das Zeichen ein - ist, neuen Wert beginnen
           
             Serial2.println(ausgabe);
             ausgabe = "Temp";
             ausgabe += x;
             ausgabe += ": ";
              x++;
              ausgabe += c;
              break;
          case '>':                     // wenn das Zeichen ein > ist, ist der string beendet
              Serial2.println(ausgabe);
              stringEnd = true;
              break;
          default:
              ausgabe += c;
              break;
      }
      y++;
  }}

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  inputString.reserve(16);
  Serial.println("Arduino ready");
  }

void loop() {
  //Daten von Modul #1 anfordern
  sendgetAll1();
  Serial.println("Befehl1 gesendet");
  delay(100);
  //Daten von Modul #1 lesen
  serialEvent();				
  Serial.println("serialEvent ausgefuehrt");	
  parseString();
  delay(200);
  //Daten von Modul #2 anfordern
  sendgetAll2();
  Serial.println("Befehl2 gesendet");
  delay(100);
  //Daten von Modul #2 lesen
  serialEvent();
  Serial.println("serialEvent ausgefuehrt");
  parseString();
}

It seems to be working, (implemented some print commands to see if the Arduino is reaching every step.The parsing part runs perfect and the Arduino sends the commands to the serial port) but now I have the problem that somehow my interface is not working? Is there a possibility to test the max232 chip? I become desperate...
@hud357: Using USB won't be possible because I also have to connect the display. So I thought it would be easier to use RS232 for both since the Arduino just has one USB port.

Whoops!

serialEvent() is called by the Arduino framework. You are not supposed to call it yourself. You can, and it won't cause any errors, but it's not a good idea to disrupt the framework. It looks like perhaps you should give that function a different name, since it's really just another function in your program and you don't require the special status that serialEvent() has within the framework.

If you were expecting to use the special status of serialEvent() then you should be using serialEvent1() if you are reading data from Serial1.

Testing the MAX232:

  1. Check which version of the chip you have. Some have "enable" pins which must be held high or low for the chip's outputs to work. Some of the MAX chips are absolutely identical except the polarity of "enable" is reversed.

The following steps require a multimeter...

  1. Check the input power to the MAX232. Is it really getting 5V?

  2. Check the voltages on the storage capacitors C3 and C4. You should find a positive voltage about double your input voltage and a negative of the same voltage.

  3. Without using Serial.begin() in your sketch, just write a high or low to the output pin connected to the MAX232 input. Check that the output is a negative or positive voltage.

The following steps require an oscilloscope...

  1. Check the charge capacitors C1 and C2 are getting a square wave and the ripple voltage on C3 and C4 is not bad

  2. See if you can observe the pattern of serial data going in and out of the MAX232 chip.

I don't understand how you can claim that "The parsing works perfectly" when this function appears to clear the inputString:

void serialEvent() {
  while (Serial1.available() && !stringComplete ){
  char inChar = (char)Serial1.read();
  inputString += inChar;
  stringComplete = (inChar == '>');
  }
    Serial.println(inputString);
      inputString = "";
      stringComplete = false;
}

Is this really the code that you tested?

This is just lazy...  delay(200);
It's obviously hand-tuned to the different delays from the measurement module. It's kind of like sending a command and then waiting a year to look in the mailbox to see if the command was carried out. Why don't you let your program process the message as soon as it's complete rather than waiting longer? It can also do other useful stuff instead of being locked in a delay.

void loop() {
  //Daten von Modul #1 anfordern
  sendgetAll1();
  Serial.println("Befehl1 gesendet");
  unsigned long CommandSentTime = millis();
  while(!stringComplete) {
    //Daten von Modul #1 lesen
    serialEvent();				//as discussed above, you should rename this function

    //other useful code can go here - check all the other serial ports for messages too?

    if(millis() - CommandSentTime > TIMEOUT) break;  //set timeout to 200 milliseconds maybe?
  }
  if(stringComplete)
    Serial.println("serialEvent ausgefuehrt");	
    parseString();
  } else {
    Serial.println("serialEvent gescheitert");	
  }

RobinRuebe:
sorry I was on a trip abroad and had no possibility to work on my project…
At the time my code looks like this:

In Reply #1 @MorganS gave you a link to Serial Input Basics. The 3rd exampl in that will read your data if you just swap the start- and end-markers. And the Parse example can easily be changed to use the + as the delimiter.

...R

Short update. The RS-232 Interface works now, my MAX232 was damaged... The thing with the serialEvent() function was just my gabbiness. Thanks for this note, changed it to a different name. I tested the parsing part with another less complex sketch that just parses a given string into smaller parts.The delay was implemented so that's easier for the user to read the data on the display. But you're right, this doesn't make so much sense. It's better to write this in the display code.
My problem now is, the modules do not respond? I tested my code and the interface with a loop back. The terminal shows me that I send my reading commands #01 and #02. The manual of the modules says that they work with "Multiple Data Format". So I thought it might be possible to send the commands maybe as binary or HEX data insted of ASCII codes. Can someone tell me how to translate the commands #01 and #02 to these formats? I have no idea how to send the # .
Thanks!

RobinRuebe:
My problem now is,

The problem now is that you are expecting us to guess what your latest code looks like.

...R

It's still the same, just deleted the delays an renamed the serialEvent() to getData()

RobinRuebe:
It's still the same, just deleted the delays an renamed the serialEvent() to getData()

Then it is NOT still the same. If you want help then help us to help you.

...R