Serial Comm Tx/Rx coding

Hello Arduino lovers,
I am trying to get digital inputs from an oxygen monitor using Rx and Tx. I am using Arduino mega2560. How do i write the code for it. I have attached datasheet of the sensor. Please help. Should i use the below code as reference?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(19, 18); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(38400);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

OCS-3F 2.1data sheet.pdf (598 KB)

I2C is a serial protocol.
It's not an asynchronous serial protocol.
If the device is I2C then software serial is completely inappropriate

"I am using Arduino mega2560."
SoftwareSerial mySerial(19, 18); // RX, TX

That is a poor example as 18/19 are hardware port Serial1 on the Mega, why would one run software serial over the top of it?

"Digital Output 9600bps UART
5V TTL / 3.3V CMOS compatible"

It certainly looks like all you need is +12, Gnd, and Rx/Tx connected to J2 to receive messages that are automatically sent.

I don't see I2C mentioned anywhere.

Yes, +12v, GND and RX/Tx all are connected to J2. I connected RX/TX to D19 D18. How do i get the reading in arduino?

similar to your code of post #1 but using Mega Serial1

// Arduino Mega serial1 test

// mega pin 18 is Tx
// pin 19 is Rx
// for loopback test connect pin 18 to pin 19

unsigned long time;

void setup() {
  Serial.begin(115200);   // initialise serial monitor port
  Serial1.begin(115200);  // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
}

void loop() {
  if (Serial1.available())        // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  if (Serial.available()) {       // read from Serial outut to Serial1
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

you can do a simple loopback test by connecting Mega pin18 to pin 19

a loopback run gives

Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19
this is a test
1234567890

The test is giving garbage value like ????@@##$%^???

Then check your baud rate in the program ( Serial.begin(); ) and the drop down box in the serial monitor. Both should be the same...

I understood that it is not I2C communication. But how do i get the data on TX and RX in the serial monitor? I did all the connections. i.e. 12v,gnd,tx,rx

The baud rate is the same. I just have to short/connect pin 18 and 19, right? and run the code.