Hi All,
I have had some great help here recently so I was hoping to pick your collective brains again if possible! Again I must stress Im very new to this so please bare with me.
I am trying to extract data from an automotive ECU, which uses RS232 comms, at a baud rate of 230400. In order to get the ECU to start reporting, you must send it a 'start report' request, and then a keep awake every ~2s. This is fine, and I have this working - the ECU begins reporting fine.
The length of one complete message back from the ECU (which contains all engine running data) is 549 bytes, and is sent over and over.
All I'm trying to do presently, is read the payload, and print specific bytes to serial monitor as a proof of concept eg byte 110 = engineRPM.
The first first three bytes of the payload are fixed at 0x4D, 0x45, 0x1C, so I'm using these to get the start of a message. This part of the code works consistently - when the start of a new message is received, I start to run through the rest of the bytes in the message.
The final byte in a message is variable in real life, but at the minute im using a fixed message for testing so I've defined it as 0x5F.
So... the problem I have is I can read the incoming message perfectly, but only up to 66 bytes. Then it seems there is nothing left to read.. if I was going to guess Id say the buffer has filled quicker than I can read through it - but if this is the case Im not sure how to work around it.
In terms of Hardware, i'm using a teensy 4.1 board, with a MAX3232 adapter to connect to the ECU. If its helpfull I can also share the fixed message Im using for testing?
My code is as follows:
char syncChars[2] = {'M', 'E',};
boolean newData = false;
const int numChars = 549;
char rdbyte[numChars];
byte req [] = { 0x4D, 0x45, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x03, 0x05, };
void setup() {
Serial1.begin(230400);
Serial.begin(230400);
Serial.println();
Serial.println("Serial debug line open");
Serial.println("Initialized serial");
delay (6000);
start_reporting_req();
}
void loop() {
if (Serial1.find(syncChars)) { // Looking for bytes 0x4D , 0x45 to signal start of message
delay(10);
recvWithStartEndMarkers() ; // If I find the start of the message, I go to start reading the full message
newData = false;
} else {
//Serial.println("timeout"); Nothing to see here!
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
int ndx = 0;
byte startMarker = 0x1C; // this is third byte in the start of a message
byte endMarker = 0x5F; // this is what im using currently to singal the end of a message during testing
byte rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
Serial.println(rc);
if (recvInProgress == true) {
if (rc != endMarker) {
rdbyte[ndx] = rc;
ndx++;
Serial.print ("Byte number recieved= "); Serial.println(ndx); // This is what Ive added in for debugging - the max ndx value I see is 65
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
Serial.print("Final NX Value = ");
Serial.println(ndx);
rdbyte[ndx] = '\0'; // terminate the string
Serial.println("End Marker recieved");
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
Serial.println("New data in now");
recvInProgress = true;
}
}
}
void start_reporting_req() {
Serial.print("Sending: 0x4D, 0x45, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x03, 0x05 ");
Serial1.write(req, sizeof(req));
Serial.println();
}