SoftwareSerial vs HardwareSerial

Hello, its my first time to submit a forum, anyways I am facing a problem in serial communication which is kind weird. I have a device that sends data all what i have to do is to listen to it and then I will use this data but first stage I read these data correctly.
first I used SoftwareSerial library the code is working as a charm.
please note i am using Arduino mega so i decided to remove the SoftwareSerial and use the TX2 RX2.

here is the SoftwareSerial code,

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10,"SERIAL_8N1"); // 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 port only
  }
  // set the data rate for the SoftwareSerial port
  mySerial.begin(19200);
}
void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

and here is the output from this code which is 100% correct:

T1,0.0,0.0,0.0,0.0,0,0,0,0,0.0,0,0.0,0,7,0.0,0.0,0.0,0.0,0.0,0,0.0,0,3,0,0,0,0
T2,0.000,0.0,9223372036854775808.8,9223372036854775808.8,3,1,476,429496729,81,6152,4061,6150,1983,0,132,179,37350939,245,0.0,21474836.0,9223372036854775808.8,4381
T6,255,3,3,31,31,31,14,3,0,0,0,15,31
T1,0.0,0.0,0.0,0.0,0,0,0,0,0.0,0,0.0,0,7,0.0,0.0,0.0,0.0,0.0,0,0.0,0,3,0,0,0,0
T2,0.000,0.0,9223372036854775808.8,9223372036854775808.8,3,1,476,429496729,81,6152,4061,6150,1983,0,132,179,37350939,245,0.0,21474836.0,9223372036854775808.8,4381
T6,255,3,3,31,31,31,14,3,0,0,0,15,31

Please note: incase I didnt add this "SERIAL_8N1" in SoftwareSerial mySerial(11, 10,"SERIAL_8N1"); // RX, TX the code wont give me the right answers.

the output of SoftwareSerial code without "SERIAL_8N1" is :

Uv⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

Now I am using the TX2 RX2 which is Serial2 in arduino mega:

void setup() {
  // initialize both serial ports:
  Serial.begin(19200);
  Serial2.begin(19200,SERIAL_8N1); // RX, TX

}

void loop() {
  // read from port 1, send to port 0:

  while (1) {
    if (Serial2.available()) {
      int inByte = Serial2.read();
      Serial.write(inByte);
    }

    // read from port 0, send to port 1:
    if (Serial.available()) {
      int inByte = Serial.read();
      Serial2.write(inByte);
    }
  }
}

and here is the output :

⸮⸮v⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

i believe the issue is in Serial2.begin(19200,SERIAL_8N1); // RX, TX although
The default is 8 data bits, no parity, one stop bit. which is SERIAL_8N1
so please if someone can help thanks in advanced.

How are you connecting to Serial2 to read it? Is that device still set to the correct baudrate?

Any difference if you omit the SERIAL_8N1 parameter?

yes same configuration for the device and its doing this issue.
while for the connection the Tx of the device is connected to the Rx2 of the arduino
while the Rx of the device is connected to the Tx2 of the arduino
of course ground to ground.

it gives me same output

⸮⸮v⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

You could always try Serial1 or Serial3.

same output

SoftwareSerial does not have a constructor that takes a text as the 3rd argument. See https://docs.arduino.cc/learn/built-in-libraries/software-serial.

The only option is a bool to invert the signal. Your text is a pointer to some text in memory and is not NULL (0) so it's treated as true indicating that inverse logic is used.

HardwareSerial does not have a way to use inverse logic.

I suggest that you check the spec of the device. Does it use TTL or RS232 signal levels?

Ok, what's the device that is sending the data.

its RS232 signal I connected it to rs232 to ttl and its works
but on SoftwareSerial i wasnt using RS232 to ttl

thanks for mentioning about ttl and rs232

thanks for everyone

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