Reading serial data

Hi,

I've got some issues that might be an easy task to solve.

I want to read a serial data and check if there is a special character there or not.
If that special character is not present, it should send the data further on to another serial port.

The data I receive is in this format: ka 00 01

The code I'm just briefly testing with is this:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

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

// set the data rate for the SoftwareSerial port
mySerial.begin(19200);

}

void loop() //
{
if (mySerial.available())
Serial.write(mySerial.read());

if (Serial.available())
mySerial.write(Serial.read());
}

This only gives me back the first byte "ka" and not the rest.

I guess I need to store these bytes into a variable and send them out again.

Any inputs?

If those are bytes, the serial monitor doesn't display character 0 or character 1. They're likely being received, just not printed.

Hi,

thanks.. I will try a different terminal and get back.

Maybe try:

  if (mySerial.available()) {
    char c = mySerial.read();
    Serial.write(c);
    }

I think that will suffer from the same issue that the serial monitor won't render those characters. You could print them as ints though to confirm what was received:

if (mySerial.available()) {
    int c = mySerial.read();
    Serial.println(c);
    }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Or Serial.println(c,HEX);

How is this:
1. Screen shot of receiver UNO
sm26.png

2. NANO TX Codes

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // SRX, STX

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

void loop()
{
  mySerial.println("ka 00 01");
  delay(1000);
}

3. UNO RX Codes

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // SRX, STX

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

void loop() 
{
  byte n = mySerial.available();
  if( n != 0)
  {
    char x = mySerial.read();
    Serial.print(x);
  }
}

sm26.png