View Single Post
Old 09-15-2013, 09:10 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,031

Well, one thing I said incorrectly is that you would read the Message ID to determine if the message had a checksum, but in looking at the document again (and it was implied in the comment in my hand decoded packet), is that the Originating ID of 10 (0xE0 + 10 = 0xEA) indicates that the packets will use a checksum byte. If the Originating ID was 9 (0xE0 + 9 = E9) then the packets would not have a checksum byte.

I wouldn't do nearly as much string conversions as you do. Generally the only time to convert numbers to strings is when you have to display them, not to compare them, or collect them or operate on them.

Why not create an array of bytes to act as a temporary buffer. You can use the Read method of the Serial port to read data into that array at a given offset, so you can real all the available bytes into the array in one swoop, and accumulate the bytes by keeping track of the next position in the array to be written to, and reading future bytes into that array at an offset that "appends" the new data to the previous data.
Now that you have an array of bytes, you can start looking for your message, and you don't have to convert the numbers to strings.
I'm tired and heading to bed, so some example of using the byte array would be like the code below, but I'm not going to exhaustively proof it, so there could be an error.
Not shown is getting the data into the buffer and updating "used" (which would be done by the serial data reader). Also didn't add a calculate checksum function to generate a checksum to compare to the checksum byte in the packet.
  Dim Buffer(1000) As Byte   'Array of bytes to use as a buffer
  Dim Used as Integer
  Dim Ptr as Integer

' Assume you've read and accumulated a number of bytes in Buffer, and set Used to the last index filled
  Private Function extractPacketFromBuffer(ByRef pkt() As Byte) As Boolean  'note pkt() passed by reference so we can redim
    Dim success As Boolean = True
    Dim looking As Boolean = True
    Dim payloadLen As Integer

    Do While (looking)
      If used > 4 Then                  'can't have have a packet with less than six bytes
        If Buffer(Ptr) = &HAA Then         '  If we've found an 0xAA then
          payloadLen = Buffer(Ptr + 4)            '    Get the packet payload length
          If used > 4 + payloadLen Then '    if we have enough bytes in the buffer for a full message then
            If Buffer(ptr + 5 + payloadLen) = &HAB Then

              ReDim pkt(payloadLen + 5)
              Array.Copy(Buffer, ptr, pkt, 0, payloadLen + 6)
              looking = False 'no longer looking
              success = True  'we have a packet
              Dim unProcessedIdx = ptr + 6 + payloadLen        'Determine the first buffer byte index after the packet extracted
              If used > unProcessedIdx Then                    'If there are extra unprocessed bytes in the buffer
                Array.Copy(Buffer, unProcessedIdx, Buffer, 0, used - unProcessedIdx)  'move all the unprocessed bytes to the front
                Ptr = 0 'point to the front of the buffer for next time
              End If
              used -= unProcessedIdx  'adjust the used count down to account for all the processed bytes removed
            End If
          Else  'not enough bytes in the buffer for a full message, so leave for now
            looking = False  'no longer looking for a packet
            success = False  'no packet found this time
          End If
          ptr += 1
        End If
        looking = False
        success = False
      End If

    Return success

  End Function

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim pkt() As Byte 'will be redimensioned by the function if a packet is retrieved
    If extractPacketFromBuffer(pkt) Then  'if we extracted a packet
      Debug.Print(pkt.Length)      'Parse the packet to extract data
    End If
  End Sub
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 09-16-2013 at 12:42 PM.
Reply With Quote