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.