|
| 1 | +# CanHacker (lawicel) CAN adapter on Arduino + MCP2515 |
| 2 | + |
| 3 | +Use that [Library](https://github.com/autowp/arduino-mcp2515) to communicate with MCP2515 |
| 4 | + |
| 5 | +## Library Installation |
| 6 | + |
| 7 | +1. Install [MCP2515 Library](https://github.com/autowp/arduino-mcp2515) |
| 8 | +2. Download the ZIP file from https://github.com/autowp/arduino-canhacker/archive/master.zip |
| 9 | +3. From the Arduino IDE: Sketch -> Include Library... -> Add .ZIP Library... |
| 10 | +4. Restart the Arduino IDE to see the new "canhacker" library with examples |
| 11 | + |
| 12 | +Testes with Arduino Nano. |
| 13 | +On Arduino Uno when works with CanHacker for windows have problem with too long boot period and losing first command |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Example |
| 18 | + |
| 19 | +``` |
| 20 | +#include <can.h> |
| 21 | +#include <mcp2515.h> |
| 22 | +
|
| 23 | +#include <CanHacker.h> |
| 24 | +#include <CanHackerLineReader.h> |
| 25 | +#include <lib.h> |
| 26 | +
|
| 27 | +#include <SPI.h> |
| 28 | +
|
| 29 | +const int SPI_CS_PIN = 10; |
| 30 | +const int INT_PIN = 2; |
| 31 | +
|
| 32 | +CanHackerLineReader *lineReader = NULL; |
| 33 | +CanHacker *canHacker = NULL; |
| 34 | +
|
| 35 | +void setup() { |
| 36 | + Serial.begin(115200); |
| 37 | + while (!Serial); |
| 38 | + SPI.begin(); |
| 39 | + softwareSerial.begin(115200); |
| 40 | +
|
| 41 | + Stream *interfaceStream = &Serial; |
| 42 | + |
| 43 | + canHacker = new CanHacker(interfaceStream, null, SPI_CS_PIN); |
| 44 | + lineReader = new CanHackerLineReader(canHacker); |
| 45 | + |
| 46 | + pinMode(INT_PIN, INPUT); |
| 47 | +} |
| 48 | +
|
| 49 | +void loop() { |
| 50 | + canHacker->processInterrupt(); |
| 51 | + lineReader->process(); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Protocol |
| 56 | + |
| 57 | +Protocol CanHacker (lawicel) described in (CanHacker for Windows documentation)[http://www.mictronics.de/projects/usb-can-bus/] |
| 58 | + |
| 59 | +Library implements it partially. Suported commands listed below. |
| 60 | + |
| 61 | +### `C[CR]` |
| 62 | + |
| 63 | +This command switches the CAN controller from operational in reset mode. The |
| 64 | +controller is no longer involved in bus activities. |
| 65 | + |
| 66 | +Command is only active if controller was set to operational mode with command `O` before. |
| 67 | + |
| 68 | +Return: [CR] or [BEL] |
| 69 | + |
| 70 | +### `L[CR]` |
| 71 | + |
| 72 | +This command will switch the CAN controller in Listen Only mode. No channel open |
| 73 | +command (`O`) is required after issuing `L`. |
| 74 | + |
| 75 | +Use the close channel command `C` to return to reset mode. |
| 76 | + |
| 77 | +Return: [CR] |
| 78 | + |
| 79 | +### `Mxxxxxxxx[CR]` |
| 80 | + |
| 81 | +Set acceptance code. This command works only if controller in reset mode. |
| 82 | + |
| 83 | +xxxxxxxx = Acceptance Code in hexadecimal |
| 84 | + |
| 85 | +Default value after power-up is 0x00000000 to receive all frames. |
| 86 | + |
| 87 | +Return: [CR] or [BEL] |
| 88 | + |
| 89 | +### `mxxxxxxxx[CR]` |
| 90 | + |
| 91 | +Set acceptance mask. This command works only if controller is setup with command `S` and in reset mode. |
| 92 | + |
| 93 | +xxxxxxxx = Acceptance Mask in hexadecimal |
| 94 | + |
| 95 | +Default value after power-up is 0xFFFFFFFF to receive all frames. |
| 96 | + |
| 97 | +Return [CR] or [BEL] |
| 98 | + |
| 99 | +### `N[CR]` |
| 100 | + |
| 101 | +Read serial number from device. |
| 102 | + |
| 103 | +Return: Nxxxx[CR] |
| 104 | + |
| 105 | +xxxx = Serial number in alphanumeric characters. |
| 106 | + |
| 107 | +### `O[CR]` |
| 108 | + |
| 109 | +This command switches the CAN controller from reset in operational mode. The controller is then involved in bus activities. It works only if the initiated with `S` command before, or controller was set to reset mode with command `C`. |
| 110 | + |
| 111 | +Return: [CR] or [BEL] |
| 112 | + |
| 113 | +### `riiiL [CR]` |
| 114 | + |
| 115 | +This command transmits a standard remote 11 Bit CAN frame. It works only if |
| 116 | +controller is in operational mode after command `O`. |
| 117 | + |
| 118 | +``` |
| 119 | +iii - Identifier in hexadecimal (000-7FF) |
| 120 | +L - Data length code (0-8) |
| 121 | +``` |
| 122 | + |
| 123 | +Return: [CR] or [BEL] |
| 124 | + |
| 125 | +### `RiiiiiiiiL [CR]` |
| 126 | + |
| 127 | +This command transmits an extended remote 29 Bit CAN frame. It works only if |
| 128 | +controller is in operational mode after command `O`. |
| 129 | + |
| 130 | +``` |
| 131 | +iiiiiiii - Identifier in hexadecimal (00000000-1FFFFFFF) |
| 132 | +L - Data length code (0-8) |
| 133 | +``` |
| 134 | + |
| 135 | +Return: [CR] or [BEL] |
| 136 | + |
| 137 | +### `Sn[CR]` |
| 138 | + |
| 139 | +This command will set the CAN controller to a predefined standard bit rate. |
| 140 | +It works only after power up or if controller is in reset mode after command `C`. |
| 141 | + |
| 142 | +The following bit rates are available: |
| 143 | + |
| 144 | +``` |
| 145 | +S0 - 10Kbps |
| 146 | +S1 - 20Kbps |
| 147 | +S2 - 50Kbps |
| 148 | +S3 - 100Kbps |
| 149 | +S4 - 125Kbps |
| 150 | +S5 - 250Kbps |
| 151 | +S6 - 500Kbps |
| 152 | +S7 - 800Kbps |
| 153 | +S8 - 1Mbps |
| 154 | +``` |
| 155 | + |
| 156 | +Return: [CR] or [BEL] |
| 157 | + |
| 158 | +### `tiiiLDDDDDDDDDDDDDDDD[CR]` |
| 159 | + |
| 160 | +This command transmits a standard 11 Bit CAN frame. It works only if controller is in operational mode after command `O`. |
| 161 | + |
| 162 | +``` |
| 163 | +iii - Identifier in hexadecimal (000-7FF) |
| 164 | +L - Data length code (0-8) |
| 165 | +DD - Data byte value in hexadecimal (00-FF). Number of given data bytes will be |
| 166 | +checked against given data length code. |
| 167 | +``` |
| 168 | + |
| 169 | +Return: [CR] or [BEL] |
| 170 | + |
| 171 | +### `TiiiiiiiiLDDDDDDDDDDDDDDDD[CR]` |
| 172 | + |
| 173 | +This command transmits an extended 29 Bit CAN frame. It works only if controller is in operational mode after command `O`. |
| 174 | +``` |
| 175 | +iiiiiiii - Identifier in hexadecimal (00000000-1FFFFFFF) |
| 176 | +L - Data length code (0-8) |
| 177 | +DD - Data byte value in hexadecimal (00-FF). Number of given data bytes will be checked against given data length code. |
| 178 | +``` |
| 179 | + |
| 180 | +Return: [CR] or [BEL] |
| 181 | + |
| 182 | +### `V[CR]` |
| 183 | + |
| 184 | +Read hardware and firmware version from device. |
| 185 | +Return: Vhhff[CR] |
| 186 | + |
| 187 | +``` |
| 188 | +hh - hardware version |
| 189 | +ff - firmware version |
| 190 | +``` |
| 191 | + |
| 192 | +### `v[CR]` |
| 193 | + |
| 194 | +Read detailed firmware version from device. |
| 195 | +Return: vmami[CR] |
| 196 | + |
| 197 | +``` |
| 198 | +ma = major version number |
| 199 | +mi = minor version number |
| 200 | +``` |
| 201 | + |
| 202 | +### `Zv[CR]` |
| 203 | + |
| 204 | +This command will toggle the time stamp setting for receiving frames. Time stamping is disabled by default. |
| 205 | + |
| 206 | +If time stamping is enabled for received frames, an incoming frame includes 2 more bytes at the end which is a time stamp in milliseconds. |
| 207 | + |
| 208 | +The time stamp starts at 0x0000 and overflows at 0xEA5F which is equal to 59999ms. |
| 209 | + |
| 210 | +Each increment time stamp indicates 1ms within the 60000ms frame. |
| 211 | + |
| 212 | +### Incoming messages |
| 213 | + |
| 214 | +All incoming frames are sent after successful receiving, optional with time stamp. |
| 215 | + |
| 216 | +No polling is needed. |
| 217 | + |
| 218 | +They will be sent in the following format: |
| 219 | + |
| 220 | +11 Bit ID Frame |
| 221 | +``` |
| 222 | +tiiiLDDDDDDDDDDDDDDDD[ssss][CR] |
| 223 | +``` |
| 224 | + |
| 225 | +11 Bit ID Remote Frame |
| 226 | +``` |
| 227 | +riiiL[ssss][CR] |
| 228 | +``` |
| 229 | + |
| 230 | +29 Bit ID Frame |
| 231 | +``` |
| 232 | +TiiiiiiiiLDDDDDDDDDDDDDDDD[ssss][CR] |
| 233 | +``` |
| 234 | + |
| 235 | +29 Bit ID RemoteFrame |
| 236 | +``` |
| 237 | +RiiiiiiiiL[ssss][CR] |
| 238 | +``` |
| 239 | + |
| 240 | +``` |
| 241 | +r - Identifier for Remote 11 bit frame |
| 242 | +R - Idenifier for Remote 29 bit frame |
| 243 | +t - Identifier for 11 bit frame |
| 244 | +T - Idenifier for 29 bit frame |
| 245 | +i - ID bytes (000-7FF) or (00000000-1FFFFFFF) |
| 246 | +L - Data length code (0-8) |
| 247 | +DD - Data bytes (00-FF) |
| 248 | +ssss - Optinal time stamp (0000-EA5F) |
| 249 | +``` |
0 commit comments