
GETCAN enables a program to read CAN (Controller Area Network) frames from the CAN interface into byte arrays. The CAN interface must be enabled for this to work. The array must have at least 28 bytes because CAN frames handled by the module have an extended header that contains e.g. time stamps. GETCAN needs a single argument which is the name of the target array. If this argument is not an array or an array which is smaller than 28 bytes, GETCAN rejects the call and sets LASTERR to ERR_ARGUMENT(4).
A CAN frame read by GETCAN consists of:
4 bytes arbitrary headerThis field is not affected by the GETCAN command but must be there for compatibility reasons.
4 bytes frame information
This field contains information that describes the CAN frame e.g. its type and if it has the RTR bit set.4 bytes message ID
This field contains the message ID of the frame8 bytes payload
This field contains the data bytes of the CAN frame4 bytes Timestamp #1
This field contains the second timestamp based on the RTC of the module.4 bytes Timestamp #2
This field contains the value of the free running missecond counter of the module.
This example repeatedly tries to read CAN frames. The first time it catches one, it prints a message an exits.
DIM a(28)
DO
GETCAN a
IF LASTERR = 0 THEN
PRINT "frame received"
END
END IF
LOOP
LASTERR becomes ERR_OK(0) if a frame has been successfully fetched from FIFO buffer. If there's no frame LASTERR becomes ERR_NO_DATA(8). If LASTERR is 0, that is when GETCAN has fetched a frame, BYTESREAD is set to the number of payload bytes in that frame. Regarding to this, if both, LASTERR and BYTESREAD are zero, GETCAN has fetched a frame with no payload. There are convenient functions like CANINFO and SETCAN to dissect and construct CAN frames. Please read also their manual pages.
CAN messages are mapped into a 28 byte long array:
|
Byte
|
Bits
|
Description
|
|
1
|
7..0
|
User definable header bytes. Usually all '0'.
|
|
2
|
15...8
|
|
|
3
|
23..16
|
|
|
4
|
31..23
|
|
|
5
|
7..0
|
Frame descriptor: reserved bits, all '0'
|
|
6
|
7..0
|
Frame descriptor: reserved bits, all '0'
|
|
7
|
7..4
|
Frame descriptor: reserved bits
|
|
|
3..0
|
Frame descriptor: CAN data length
|
|
8
|
7
|
Frame descriptor: Frame type (0 = standard, 1 = extended)
|
|
|
6
|
Frame descriptor: RTR bit ('1' = RTR bit is set)
|
|
|
5..0
|
Frame descriptor: reserved bits
|
|
9
|
7…0
|
Message ID
Standard message: valid bits 10..0 Extended message: valid bits 28..0 |
|
10
|
15...8
|
|
|
11
|
23..16
|
|
|
12
|
31..23
|
|
|
13
|
7..0
|
CAN data bytes in the order 1 …8
Valid number of bytes defined by CAN data length |
|
14
|
7..0
|
|
|
15
|
7..0
|
|
|
16
|
7..0
|
|
|
17
|
7..0
|
|
|
18
|
7..0
|
|
|
19
|
7..0
|
|
|
20
|
7..0
|
|
|
21
|
7…0
|
Real time clock time stamp in seconds since 01.01.2007
|
|
22
|
15...8
|
|
|
23
|
23..16
|
|
|
24
|
31..23
|
|
|
25
|
7…0
|
Millisecond time stamp. Value in ms since module power on.
|
|
26
|
15...8
|
|
|
27
|
23..16
|
|
|
28
|
31..23
|