Strange Data Mutation from Software Serial

Hey all,
I am trying to create a system to start a generator based on serial data. I can use the hardware serial for the final product and do plan to, but to test it I was going to use the software to tell the Arduino what to send and receive. I ran into a strange issue with this where data I sent is changed in a way that is consistent for the same data. The code is as follows:

#define GeneratorToggle 12
#define FuelSensorReading 0
#define FuelSensorPower 7

int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 100;
float R2 = 0;
float buffer = 0;
bool currentSysStatus = 0;


//For communicating with the Rockblock+ System
#include <SoftwareSerial.h>
int rx = 8;
int tx = 7;
long BaudRate = 9600;
SoftwareSerial mySerial(rx, tx); // RX, TX

void setup(){
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
Serial.begin(BaudRate);
mySerial.begin(BaudRate); //Connection to Rockblock+ System
Serial.write("Hello, world!\r\n");
}

void loop(){
byte input = 0;
char sample = "a";
//Serial.print(sample);
//mySerial.print(sample);
if (mySerial.available() > 0) {
  delay(1);
  input = mySerial.read();
 // Serial.write(input);
  mySerial.write(input);
  delay(100);
  Serial.write(input);
}
//delay(500);
//Serial.write(mySerial.read());
delay(1000);

//if (sysStatus == 0) {
//Sleeping code would go here
//}
}

I am sending data over the software serial with putty, though when tested with Termite the same issues occurred. If I send a 'a', then the terminal session in Putty responds with 'X' and the input printed to the hardware serial in my IDE reads 'O'. I made a table of a few of my tested inputs here:
a = 0b01100001 = 97 (input to putty)
X = 0b01011000 = 88 (output from putty)
O = 0b01001111 = 79 (output to hardware)

b = Ob01100010 = 98 (input to putty)
1 = 0b00110001 = 49 (output from putty)
' = 0b00100111 = 39 (output to hardware)

c = Ob01100011 = 99 (input to putty)
, = 0b00101100 = 44 (output from putty)
N = 0b01001110 - 78 (output from hardware)

I tested with different input pins with the same issue occurring. All the hardware around the Arduino works as intended when used with different serial devices, so I am pretty confident the issue is with my code somewhere. Baud rate changes have to effect on this behavior. I have also tested using an int and char data type to capture the input, as well as having the read function output directly to the print function. I am officially at my wits end with this. Anyone have any ideas?

The best I can come up with is that there is some bizarre bitwise operation happening, but I have no idea where that would be happening.

Tldr: Input is getting changed in someway when I send it over the software serial. This change is different over the hardware serial as well. The same character always outputs the same wrong data.

#include <SoftwareSerial.h>
int rx = 8;
int tx = 7;
long BaudRate = 9600;
SoftwareSerial mySerial(rx, tx); // RX, TX

void setup() {
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  Serial.begin(BaudRate);
  mySerial.begin(BaudRate); //Connection to Rockblock+ System
  Serial.print("Hello, world!\r\n");
}

void loop() {
  char input = 0;
  if (mySerial.available() > 0) {
    delay(1);
    input = mySerial.read();
    Serial.write(input);
    mySerial.write(input);
    delay(100);
  }
  delay(1000);
}

You are making an ASCII table one char at a time.

This prints the hex value (ASCII code) of whatever char/byte you read:

Try using

char input;

...

Serial.print(input);

And remember "a" is a String, 'a' is a char.

You are making an ASCII table one char at a time.
I was trying to show the inputted and outputted values, sorry if it was unclear. When I input a, it outputs X to the same serial line and O to the hardware serial line. I included the other information for ease of access in case someone though it might be dropping bits or something.

This prints the hex value (ASCII code) of whatever char/byte you read:

It seems to output the decimal of it to me, which write takes. I tried with mySerial.print(input) and got the same results.

I also swapped the char sample = "a" to 'a' and it doesn't seem to work either. It returns a hex 0x1B when created as 'a' and O when created as "a". This confuses me further because the character a should be 0x61.

I'm not confident what was meant by this but moving those commands around doesn't change anything. Swapping which serial gets printed to still prints the same value to the same serial regardless.

if (mySerial.available() > 0) {
  delay(1);
  input = mySerial.read();
 // Serial.write(input);
  mySerial.write(input);
  delay(100);
  Serial.write(input);
}
//delay(500);
//Serial.write(mySerial.read());
delay(1000);

Start by getting rid of all the utterly useless delays.

Use delays only when they are required, and you know the required length.

1 Like

Agreed, I just wanted to make certain that it had nothing to do with data overlapping or something strange. I have removed them by now after eliminating that possibility.

It seems like I posted an unfair question, haha. For some reason that I can only assume is due to my wiring scheme, I need to use the Inverse Bits option (Which I thought was ruled out due to the characters not being a bit mirror of each other). Thanks so much for everyone that helped.