Receiving Standard and Extended CAN frames using Visual Basic and the Gryphon DLL

The following code snippet works under the assumption that a Gryphon session has been activated¹, and that the channel handle has been obtained² for the desired CAN channel.


Dim gframe As Long
Dim hdrbytes As Byte
Dim hdrbits As Byte
Dim datalen As Integer
Dim payload(12) As Byte

gframe = dgCreateFrameHandle(FT_DATA)

dgRecvFrame(session, channel, gframe, 0)

dgGetHeaderLength(gframe, hdrbytes)
dgGetHeaderBits(gframe, hdrbits)

' If hdrbytes = 2 and hdrbits = 11,
'   then we have received a Standard CAN frame
' If hdrbytes = 4 and hdrbits = 29,
'   then we have received an Extended CAN frame

dgGetDataLength(gframe, datalen)

dgGetPayload(gframe, payload(0), hdrbytes + datalen)

' the received CAN frame contents are now in array "payload"



Note that in the above code snippet, function call return value checking is omitted in order to more clearly demonstrate the required logic. In a real program, however, it is strongly recommended that every return value be checked for error conditions, and acted upon accordingly.

Footnotes

1  See dgBeginSession(), Gryphon C Library User's Manual, Dearborn Group Inc.
2  See dgGetChannel(), Gryphon C Library User's Manual, Dearborn Group Inc.