Hey all, I've got another weird one for you! This one is driving me absolutely nuts, but I think I've boiled it down to the core issue, so hopefully someone can point me in the right direction. Buckle up!
So, in short, I'm trying to write a little function that'll interpret some EI-Bisynch commands sent over serial to the Teensy. Nothing was working, so I made this example code which illustrates what things aren't working. Note, that I have gotten identical results out of 2 Teensy 4.1s.
As you can see above, I'm simply reading in the data sent from my PC's serial terminal, printing out the data and some comparison strings(in binary so I can see what's wrong) to my VSCode terminal, and then sending the received data back to my PC serial port.
But you can clearly see here in my VSCode terminal, that my 2s and EOTs aren't being received correctly:
This is manifesting in this example by having a failure to match strings with indexOf(). Whoops: Ignore "Prefix" in these last 2 lines of this screenshot. That was just something leftover from before. You can see the actual code is just comparing with "3322".
The real crazy thing is that when I send it back, everything goes out correctly (screenshot of my PC's serial terminal app connected to Serial3 on the Teensy through a MAX3232):
And here's some proof from my logic analyzer that everything is going in/out correctly on the RX/TX of the Teensy's port:
Any idea why I'm getting junk data out of some of my received characters? How is it possible that I can send that same junk back and it looks good on the other end?
I haven't tried much more than the alphanumeric characters in this example, but I know '1' also doesn't work. And I expect others to behave this way too... Perhaps I'll try that next to see how far this issue reaches.
So, in short, I'm trying to write a little function that'll interpret some EI-Bisynch commands sent over serial to the Teensy. Nothing was working, so I made this example code which illustrates what things aren't working. Note, that I have gotten identical results out of 2 Teensy 4.1s.
Code:
#include <Arduino.h>
void getEIBisynch();
void parseEIBisynch(String input);
String inputString="";
void setup() {
Serial3.begin(9600, SERIAL_7E1);
Serial.begin(9600);
}
void loop() {
}
void serialEvent3() {
getEIBisynch();
}
void getEIBisynch() { // Read all bytes into inputString until an ETX or ENQ is received
inputString=""; // start with empty string
while (Serial3.available()>0) {
char inChar = Serial3.read();
inputString += inChar; // add it to the string
delay(2);
if (inChar == '\x03' || inChar == '\x05') { // \x03 is End of Transmission, \x05 is Enquiry.
parseEIBisynch(inputString); // presumably a complete command. Send it to be parsed
break;
}
}
}
void parseEIBisynch(String input) {
// assemble expected string to compare with input
// String testA = '\x04'+"3322V"+'\x05'; // I don't know why this doesn't work, but whatever.
String testA = '\x04';
testA += "3322PV";
testA += '\x05';
// Send data to Serial Monitor:
Serial.print("input: ");
Serial.println(input);
Serial.println("input Binary:");
for (int i=0; i<input.length(); i++) {
Serial.println(input.charAt(i), BIN);
}
delay(250);
Serial.print("testA: ");
Serial.println(testA);
delay(250);
Serial.println("testA Binary:");
for (int i=0; i<testA.length(); i++) {
Serial.println(testA.charAt(i), BIN);
}
Serial.print("input.indexOf(Prefix): ");
Serial.println(input.indexOf("3322"));
delay(250);
Serial.print("testA.indexOf(Prefix): ");
Serial.println(testA.indexOf("3322"));
// Echo the received data back to the device
Serial3.print(input);
}
As you can see above, I'm simply reading in the data sent from my PC's serial terminal, printing out the data and some comparison strings(in binary so I can see what's wrong) to my VSCode terminal, and then sending the received data back to my PC serial port.
But you can clearly see here in my VSCode terminal, that my 2s and EOTs aren't being received correctly:
This is manifesting in this example by having a failure to match strings with indexOf(). Whoops: Ignore "Prefix" in these last 2 lines of this screenshot. That was just something leftover from before. You can see the actual code is just comparing with "3322".
The real crazy thing is that when I send it back, everything goes out correctly (screenshot of my PC's serial terminal app connected to Serial3 on the Teensy through a MAX3232):
And here's some proof from my logic analyzer that everything is going in/out correctly on the RX/TX of the Teensy's port:
Any idea why I'm getting junk data out of some of my received characters? How is it possible that I can send that same junk back and it looks good on the other end?
I haven't tried much more than the alphanumeric characters in this example, but I know '1' also doesn't work. And I expect others to behave this way too... Perhaps I'll try that next to see how far this issue reaches.
Last edited: