Simple Reading of Sensor with 8N1 Serial Output

So I have searched the forum off and on for weeks now and can’t seem to find what I’m looking for.

All I want to do is read a sensor that outputs 8N1 at 1200 baud using the software serial library, and then print that result to my computer’s monitor. The stream of characters contains three numbers separated by spaced and is terminated with the carraige return character.

My code so far is this:

#include <SoftwareSerial.h>

SoftwareSerial portOne(10,10);

int sensor = 4;
void setup()
{
  Serial.begin(1200);
  portOne.begin(1200);
  pinMode(4, OUTPUT);
}

void loop()
{
  digitalWrite(sensor,HIGH);
  delay(50);
  if (portOne.available() > 8) {
    
    Serial.println(portOne.read());
  }
  digitalWrite(sensor,LOW);
  delay(1000);
}

But it just prints out random integers, one per line.

Is 50mS long enough for the device to output 9 bytes at 1200 baud? Each bit will take 1/1200 = 0.83mS start-bit, 8 data bits, stop bit = 8.3mS, x 9 bytes = 75mS

Won't that be an issue for you?

http://www.labcell.com/media/24156/5te-manual.pdf

Page 18 describes serial communication, and says it needs 120 ms of excitation.

Which is strange because the manual I have states 50 ms.

Even when I change the excitation i still get the same stream of integers…

Capture.PNG

But it just prints out random integers, one per line.

Yes that is what you wrote. If you want them on the same line you need to drop the ln bit off the Serial.println(portOne.read()) bit.
And put a Serial.println(" ") before the end delay.
You also wait until there are 8 bytes in the buffer then read only one. You must read them until there is no more left.

Can I use

while (portOne.available() > 0 )

to read all the bytes?

Yes

So I think I’m on the right track with this code

#include <SoftwareSerial.h>

SoftwareSerial portOne(10,10);
float temp;
int sensor = 4;
void setup()
{
  Serial.begin(1200);
  portOne.begin(1200);
  pinMode(4, OUTPUT);
}

void loop()
{
  digitalWrite(sensor,HIGH);
  delay(150);
  while (portOne.available() > 0) {
    int dp = (portOne.parseInt()/50);
    int ec = portOne.parseInt();
    int t = portOne.parseInt();
    if (t > 900) { 
      temp = ((900 + 5*(t - 900))-400)/10;
    }
    else { 
      temp = (t-400)/10;
    }

    if (portOne.read() == 13 ){

      Serial.print(dp);
      Serial.print(",");
      Serial.print(ec);
      Serial.print(",");
      Serial.print(temp);

    }
  }
  digitalWrite(sensor,LOW);
  Serial.println();
  delay(5000);
}

It actually gives me 24 degrees C, which is either a lucky error or somewhat accurate.

The issue now is that when I upload this program, there is a delay in printing to the serial monitor and it only prints once per upload.

So you if you have one byte available for reading you assume it is an integer in the form of a string and then go messing about with it that is not right is it? Read:- http://arduino.cc/en/Tutorial/ReadASCIIString

SoftwareSerial portOne(10,10);

SoftwareSerial(rxPin, txPin) in the reference

GrumpyMike,

Now I'm really confused, the link you posted is the tutorial I followed, and the output from the sensor should be ASCII integers.

camdenl: Now I'm really confused, the link you posted is the tutorial I followed, and the output from the sensor should be ASCII integers.

Sorry on closer examination you are right.

The issue now is that when I upload this program, there is a delay in printing to the serial monitor and it only prints once per upload.

Are you sure that your device is sending data the second time?

The data sheet you linked on page 18 it says:-

The power must be removed and reapplied for a new set of values to be transmitted.