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.