├── .project ├── .settings └── org.eclipse.core.resources.prefs ├── LICENSE.md ├── README.md └── can-usb.ino /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | can-usb 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Dmitry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CanHacker (lawicel) CAN adapter on Arduino + MCP2515 2 | 3 | Testes with Arduino Nano. 4 | On Arduino Uno have problem with too long boot period and losing first command when work with CanHacker application 5 | 6 | ## Installation 7 | 8 | 1. Install [MCP2515 Library](https://github.com/autowp/arduino-mcp2515) 9 | 2. Install [CanHacker Library](https://github.com/autowp/arduino-canhacker) 10 | 3. Configure sketch: MCP2515 CS pin, SoftwareSerial pins if required, change baudrates 11 | 4. Use with [CanHacker](http://www.mictronics.de/projects/usb-can-bus/) or CANreader (soon) 12 | -------------------------------------------------------------------------------- /can-usb.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | const int SPI_CS_PIN = 10; 12 | const int INT_PIN = 2; 13 | 14 | const int SS_RX_PIN = 3; 15 | const int SS_TX_PIN = 4; 16 | 17 | CanHackerLineReader *lineReader = NULL; 18 | CanHacker *canHacker = NULL; 19 | 20 | SoftwareSerial softwareSerial(SS_RX_PIN, SS_TX_PIN); 21 | 22 | void setup() { 23 | Serial.begin(115200); 24 | while (!Serial); 25 | SPI.begin(); 26 | softwareSerial.begin(115200); 27 | 28 | Stream *interfaceStream = &Serial; 29 | Stream *debugStream = &softwareSerial; 30 | 31 | 32 | canHacker = new CanHacker(interfaceStream, debugStream, SPI_CS_PIN); 33 | //canHacker->enableLoopback(); // uncomment this for loopback 34 | lineReader = new CanHackerLineReader(canHacker); 35 | 36 | pinMode(INT_PIN, INPUT); 37 | } 38 | 39 | void loop() { 40 | CanHacker::ERROR error; 41 | 42 | if (digitalRead(INT_PIN) == LOW) { 43 | error = canHacker->processInterrupt(); 44 | handleError(error); 45 | } 46 | 47 | error = lineReader->process(); 48 | handleError(error); 49 | } 50 | 51 | void handleError(const CanHacker::ERROR error) { 52 | 53 | switch (error) { 54 | case CanHacker::ERROR_OK: 55 | case CanHacker::ERROR_UNKNOWN_COMMAND: 56 | case CanHacker::ERROR_NOT_CONNECTED: 57 | case CanHacker::ERROR_MCP2515_ERRIF: 58 | case CanHacker::ERROR_INVALID_COMMAND: 59 | return; 60 | 61 | default: 62 | break; 63 | } 64 | 65 | softwareSerial.print("Failure (code "); 66 | softwareSerial.print((int)error); 67 | softwareSerial.println(")"); 68 | 69 | digitalWrite(SPI_CS_PIN, HIGH); 70 | pinMode(LED_BUILTIN, OUTPUT); 71 | 72 | while (1) { 73 | int c = (int)error; 74 | for (int i=0; i