Parsing numbers from RS232

Hi, I'm trying to read data from a vacuum sensor for a vacuum coating machine. I have a Teledyne Hastings DV6M gauge tube and a DAVC which provides an RS232 output of Voltage then Pressure or an Analog signal 5vdc or a non linear signal 1vdc.
I found trying to read the pressure on the analog voltage too jumpy, particularly at the lower scale, so I got an RS232 shield and am now Serial.reading to an LCD. That is the pressure from the RS232 output. This output is both voltage and pressure delineated by a character on the LCD of 4 horizontal lines. As I don't need the voltage I'm overwriting that with blank spaces so I just see the pressure. Both the voltage and the pressure are E numbers so a typical reading might look like 2.65666E-2 3.458666E-2. So far so good.
Now what I'd like to do is to monitor and record the vacuum pressure over time, by parsing just the pressure as an integer. Then writing it to the serial monitor or plotter. Firstly how do I parse I'm the second number and convert it from an E number. Then record it over time. Everything I've tried seems to screw up the LCD readback.
Many thanks if you can help.. jon

Here's me code

include <Wire.h>
#include <LiquidCrystal_I2C.h>

byte lcdNumCols = 20; // -- number of columns in the LCD

// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd1(0x27, 20, 4);

void setup(){

Serial.begin(100); //init serial
lcd1.init();
lcd1.backlight();
lcd1.clear();
lcd1.setCursor (0,0); //
lcd1.print("Vac Test");
lcd1.setCursor (0,1); //
lcd1.print("Please Wait - 1");
lcd1.setCursor (0,1);
delay(1000);
lcd1.print("Please Wait - 2");
delay(1000);
lcd1.setCursor (0,1); //
lcd1.print("Please Wait - 1");
delay(1000);
lcd1.clear();// clearn previous values from screen
delay(3000);
Serial.println("P1");

}

void loop() {

lcd1.setCursor(0,0);
lcd1.print("Chamber Pressure");
if(Serial.available()){
lcd1.setCursor(0,1);
while(Serial.available() >0){
lcd1.write(Serial.read());
}
lcd1.setCursor(0,1);
lcd1.print(" ");
lcd1.setCursor(15,1);
lcd1.print(" ");
lcd1.setCursor(0,1);
lcd1.print(" ");
lcd1.setCursor(0,3);
lcd1.print(" ");
}
}

Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

What is the whole message from the sensor? Can you post a sample of the data from the sensor? Can you post a link the the data sheet or manual for the sensor?

I would use the methods in the serial input basics tutorial to read the whole message from the sensor then I could save the values as a strings or numbers for displaying on the LCD, transmitting or saving the data.

Also using the main (usb) Serial port for the sensor and serial monitor at the same time can be problematic. I suggest a software or different hardware serial port for the sensor, depending on the Arduino board that you are using. Save the Serial port for program upload, program output and debugging.

You're relying on the speed of serial to pick up numbers. Better to read all the data and then parse out the bits you want. The atof function can deal with floats in E notation.

I can give you an example:

const byte maxMsgLen = 16;

const byte cbLF = 10;
const byte cbCR = 13;

const char exText[] = "2.65666E-2 3.458666E-2";

void setup() {
  Serial.begin(115200);
  Serial.print(F("parse two floats\nexample "));
  Serial.println(exText);
  oneLineReceived(exText);
}

void loop() {
  handleSerial();
}

void oneLineReceived(const char * buffer) {
  float voltage = atof(buffer);
  char* delim = strchr(buffer, ' ');
  float pressure = atof(delim+1);
  Serial.print(F("v = "));
  Serial.print(voltage, 6);
  Serial.print(F(", p = "));
  Serial.println(pressure, 6);
}

void handleSerial() {
  static uint8_t bIndex;
  static uint8_t buffer[maxMsgLen + 1];
  bool lineReady = false;
  if (Serial.available()) {
    uint8_t inChar = Serial.read();
    if (inChar != cbLF) {
      if (inChar == cbCR) {
        lineReady = true;
      } else {
        buffer[bIndex++] = inChar;
        lineReady = (bIndex == maxMsgLen);
      }
      if (lineReady) {
        buffer[bIndex] = 0;
        oneLineReceived((const char*)buffer);
        bIndex = 0;
      }
    }
  }
}
parse two floats
example 2.65666E-2 3.458666E-2
v = 0.026567, p = 0.034587
v = 1234000.000000, p = 0.000346

Firstly thank you all very much for your responses. I greatly appreciate you help and consideration. Secondly please accept my sincerest apologies for posting code incorrectly I will ake sure I will llearn the forum requirements.
So the vacuum sensor is a Tekedyne Hastings DV6M gauge tube, I've not found a manual for this so far on the Web, which might be deliberate. The box which interprets it and plugs into it is a DAVC by the same company. The manual for this can be found here,

https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.teledyne-hi.com/resourcecenter/Documents/Manuals/174-DigitalAVC_Manual.pdf&ved=2ahUKEwil4tPWnazxAhVQ3IUKHWoXArkQFjAAegQIBBAC&usg=AOvVaw36fC_SWw1nNjL7GJjKDDnU

Now this device is programmable over RS232 so the Linear Analog pressure output could be 10vdc, 5vdc or 1vdc or 4-20mA. Then what's nice a pressure and voltage output can be streamed over serial. Then even nicer features exist such as calling for a Zero output and full-scale for calibration purposes. I think it would be rude not to take advantage of some of its great features. However my requirement Is to
1 display real-time pressure on an LCD
2 chart and datalog this pressure over a time period.
From my uneducated messing around with the Arduinos code I got the impression reading from serial to LCD then printing to sane serial for datalogging might not be super stable.
I will post a rs23 terminal showing the data independently of the Arduino tomorrow.
Once again thanks I'm very grateful :pray:

Please provide a link to the RS232 shield and specify the type of Arduino you are using now.

The data sheet only mentions 9600 or 19200 baud, what is that strange baud rate for?

Six seconds delays to display some "Please wait"s is just mean, I'm really quite :face_vomiting:.

Here is the RS232 shield.

The Arduino is a Uni Rev 3

The Serial Baud Rate should be 9600. I don't know why its pasted 100. I'll check tomorrow. It should be 9600

The 6 second delay, was put there to try to issue the RS232 serial write "P1", which causes the DAVC to start data streaming the pressure after power up.
I agree i don't need this. However from a user perspective, it's nice to think the device is going through some kind of self check or Init, when really it's just delay. I agree its unnecessary. But irrelevant for the data.

The data is the real gold here.

Thanks
Jon :smiley:

I disagree strongly.

Devices that deliberately waste the users time should be removed.
Devices that fake some kind of cleverness (like self test) should be removed.
Programmers that build such devices should seek different areas of employment.

The RS232 shield sits on Serial, which is a PITA for debugging and uploading, isn't it?
Choosing an Arduino variant with more than one hardware serial would make more sense,
small RS232 modules are available, that can attach to any hardware serial pins.

If you want to log and graph the data on a connected PC, it should have its own hardware serial.
An interrupt driven software serial for the RS232 would probably work also,
but a hardware serial puts less load on the processor, so I would prefer it.

Arduino Software Solutions has a variety of methods for reading from Serial with their pros and cons

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.