arduino:wio-canprot-framing-en
Table of Contents
CAN ASCII Framing
ASCII format
Communication over serial or TCP/IP the ASCII framing is used.
:XhhhhhhhhNd0d1d2d3d4d5d6d7;\n :X -> X=extended S=short (start of CAN Frame) hhhhhhhh -> ID (4 bytes extended and 2 bytes short) N -> N=normal R=RTR d0-d7 -> Data (Zero to eight data bytes) ; -> end of frame (semi colon) \n -> linefeed
The DLC is calculated by the number of data bytes following the type 'N' or 'R'.
Short IDs are not used in the RCAN protocol.
Example
:X04070101N112233445566;
ID | 0x4711 |
Type | Normal |
Data | 0x11 0x22 0x33 0x44 0x55 0x66 |
DLC | 6 |
Example Code
Encode ASCII Frame
int rcan_makeExtASCIIFrame(char* frame, int prio, byte* databytes, int datalen, long id, Boolean linefeed ) { int i = 0; StrOp.fmtb( (char*)(frame+1), ":X%02X%02X%02X%02XN;%c", (id >> 24) & 0xFF, (id >> 16) & 0xFF, (id >> 8) & 0xFF, id & 0xFF, linefeed?'\n':'\0' ); if( datalen > 0 ) { for( i = 0; i < datalen; i++ ) { // :X00080004N StrOp.fmtb( (char*)(frame+1+11+i*2), "%02X;%c", databytes[i], linefeed?'\n':'\0' ); } } frame[0] = StrOp.len((char*)(frame+1)); return frame[0]; }
Decode ASCII Frame
static char hexb[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15}; void rcan_ASCIIFrame2CANFrame(const char* frame, iCANFrame canFrame) { Boolean ok = True; int offset = 0; int idh = 0; int idl = 0; int edh = 0; int edl = 0; int opc = 0; int i = 0; int len = StrOp.len(frame); canFrame->flags = 0; if(frame[1] == 'X') { offset = 4; canFrame->flags = CANFLAG_EXTENDED; edh = mbus_HEXA2Byte(frame + 2); edl = mbus_HEXA2Byte(frame + 4); idh = mbus_HEXA2Byte(frame + 6); idl = mbus_HEXA2Byte(frame + 8); } else { idh = mbus_HEXA2Byte(frame + 2); idl = mbus_HEXA2Byte(frame + 4); } canFrame->id = (edh << 24 ) + (edl << 16 ) + (idh << 8 ) + idl; // Lookup ';' to determine the dlc. canFrame->dlc = 0; for( i = 0; i < len; i++ ) { if( frame[7+offset+ i*2 + 2] == ';' ) { canFrame->dlc = i + 1; break; } } for( i = 0; i < canFrame->dlc; i++ ) { canFrame->data[i] = (hexb[frame[7 + offset + 2 * i] - 0x30] << 4) + hexb[frame[7 + offset + 2 * i + 1] - 0x30]; } }
arduino/wio-canprot-framing-en.txt · Last modified: 2024/05/09 07:20 by rjversluis