Serial Timing Question on TeesyLC

Status
Not open for further replies.

Brion Sohn

Well-known member
OK so i have been going back and forth on things and doing a bunch of reading and everything and cannot seem to find the answer to what I am trying to figure out:

So I have the following Code for serialEvent, I developed this off of the non blocking serial code read code that is in a few places on the internet, however the major difference is that I do not have a completely defined Beginning and End byte and this is because the program I am interfacing with does NOT send those (and I think that is where I am having some issue)..

Anyway here is the code: (This CODE DOES WORK)..

Code:
void serialEvent() {
  if (Serial.available() > 0) {
    delayMicroseconds(1500);
    incomingByte = (byte)Serial.read();                          // get a single byte    
    if ((incomingByte == 0xFF) && (serialStep != 1)) { 
      serialStep = 0;
    } 
    if (serialStep != endOfPkg ) {
      serialBuffer [serialStep] = incomingByte;                  // place read Byte in buffer array.
      serialStep = serialStep + 1;                               // step for next processor loop
    } else if (serialStep == endOfPkg) {                         // check for end of package defined by serialDataPkg
      serialBuffer [serialStep] = incomingByte;                  // place read Byte in buffer array.
      serialStep = 0;                                            // reset counter
      if (serialBuffer[endOfPkg] == errorCheckEnd) {             // data checksum byte check (error check)
        for (byte c = 0; c < serialDataPkg; c++) {
          serialData[c] = serialBuffer[c];                       // copy serialBuffer to serialData
        }
      }                                                          //
      for (byte r = 0; r < serialBufferSize; r++) {
        serialBuffer[r] = 0;                                     // reset serialBuffer
      }
    }
  }
}

The issue that I want to try to remove is the " delayMicroseconds(1500);" in the third line if possible as over time as the Whole scope of the project increases that 1.5ms delay will add up.. though yea it is only effecting one loop of the Teensy at a time since it is not in a blocking situation (or at least I don't see it if it is).

The Program connects at 115200 baud.. So a bit about the serial stream.. At the moment the Serial stream comes in 8 byte packets, these packets always start with FF and end with 00 BUT there are possibly multiple FF's and 00's in that stream.. and they can be sent in a continuous stream (currently for a limited duration) but eventually may be completely continuous.. So a typical limited duration stream would be --

Code:
FF01010232000000FF01020564000000FF20020564000000FF20010232000000FFFFA26000000000FFFFA00000000000

NOTE :
This is the standard data Packet type with FF being the beginning = FF01010232000000
This is the standard command Packet type with FF being the beginning and the Second FF indicating commas = FFFFA00000000000

So now the issue I have is that IF i remove the delay in line 3, I only pick up parts of the stream.. I can usually pick up the first 8 bytes fine and then maybe pick up a Packet or 2 in the middle and can usually pick up the last.. But because of this things of course don't work right..

Fixes that I have seen:

#1 - Is the delay I have found that 1500 microseconds is reliable, I have tried 1250 and the results are spotty sometimes it will pick up everything and other times it will start to mix things up.

#2 - Transmission Delay (per byte) - if I delay at the sending side (at least through a terminal program - I didn't write the main windows program) it will also solve the issues

NOTE: as far as I see I don't have anything seriously wrong in my code and it works in that it reads a Byte per loop filling a software serialBuffer array and when that array hits 8 Bytes it transfers that data into the serialData Array which is then sent to a parser to determine what to do with the data received..

One possibility that I have considered is that the delay might be needed to KEEP from overwriting the serialData array BEFORE I have a chance to extract all of the data from it but that would seem strange as the parser runs in the loop constantly to allow for changes coming from the serial through serialData[] to be implemented immediately.

Anyway any help on this or ideas would be appreciated.
 
Status
Not open for further replies.
Back
Top