|
| 1 | +# CANSerial |
| 2 | + |
| 3 | +Use CAN bus as physical layer for serial/uart communication. |
| 4 | +Aheres to Stream interface, so it can be used exactly like a regular Serial port. Supports multiple concurrent objects when used in cunjunction with included Buffered_MCP_CAN class. Designed to interface with https://github.com/bondus/CanSerial/ |
| 5 | + |
| 6 | +## History |
| 7 | +One day I found myself needing an additional serial port on my Arduino nano. Because it's based on the atmega328p it only has one hardware port. Normally, I'd just use SoftwareSerial, but I was already using that for something else. I also happened to already have my device connected to a CAN bus and I was debugging the messages being sent out. |
| 8 | + |
| 9 | +It then occured to me that instead of trying to make another SoftwareSerial and adding more wires to the mix (and another uart adapter on the computer), I could just pipe those debug messages over the CAN bus too. |
| 10 | + |
| 11 | +After looking around online, I eventuially found https://github.com/bondus/CanSerial/ which claims to be obsolete. I took a look anyways and figured that it would work fine on the computer side of things. I just needed to write the Arduino side "client" code, and that's how we got here. |
| 12 | + |
| 13 | +## Usage |
| 14 | +This library is actually two libraries in one. CANSerial is the star of the show, but Buffered_MCP_CAN is in there too. |
| 15 | +CANSerial can be used in one of two ways; either directly with an MCP_CAN object or using an intermediary Buffered_MCP_CAN. |
| 16 | + |
| 17 | +Direct usage of MCP_CAN has the following problems: |
| 18 | + - Unable to have more than one CANSerial Object |
| 19 | + - Unable to read from the CAN bus with any other part of the program. |
| 20 | + |
| 21 | +These problems are fixed by wrapping access to the CAN object in Buffered_MCP_CAN, examples for each are included in the `examples/` folder. |
| 22 | + |
| 23 | +#### Requirements |
| 24 | +##### Seeed_Arduino_CAN |
| 25 | +I cannot possibly know what hardware you might be using and in what configuration, so I won't attempt to explain how to get your CAN interface working. This library is designed to work with Seeed_Arduino_CAN and either an mcp2515 or mcp2518fd. |
| 26 | +You will probably have something like the following to have a working CAN interface: |
| 27 | +``` |
| 28 | +// CAN Constants |
| 29 | +const int SPI_CS_PIN = 10; |
| 30 | +mcp2515_can CAN(SPI_CS_PIN); // Set CS pin |
| 31 | +``` |
| 32 | +... |
| 33 | +``` |
| 34 | +void setup() { |
| 35 | + // CAN Setup |
| 36 | + while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_12MHz)) { // init can bus : baudrate = 500k |
| 37 | + Serial.println("CAN ERROR"); |
| 38 | + delay(100); |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | +##### Setup |
| 43 | +To add CANSerial to the above, include the header: |
| 44 | +``` |
| 45 | +#include <CANSerial.h> |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | +Declare an object, passing in a "base address" and your CAN object: |
| 50 | +``` |
| 51 | +CANSerial CS(0x6E0,CAN); |
| 52 | +``` |
| 53 | +Or, if using Buffered_MCP_CAN: |
| 54 | +``` |
| 55 | +Buffered_MCP_CAN bufCAN(CAN); |
| 56 | +CANSerial CS(0x6E0,bufCAN); |
| 57 | +``` |
| 58 | +Notice that `CAN` is passed to Buffered_MCP_CAN and then the Buffered_MCP_CAN object is passed to CANSerial. |
| 59 | + |
| 60 | +--- |
| 61 | +The "base address" is what lets this client library and the PC server software mentioned above talk to each other. It needs to match `PKT_ID_UUID_FILTER` from `cansock.h` in the [CanSerial utility program](https://github.com/bondus/CanSerial/). |
| 62 | + |
| 63 | +UUIDs for this are a maximum of 6 bytes, they need to be unique on your network for each device, not necessary to be pseudo-random. |
| 64 | +In `setup()` add: |
| 65 | +``` |
| 66 | + static unsigned char UUID[_CS_UUID_SIZE] = {0xCE, 0xF2, 0x35, 0x2C, 0xE1, 0xB3}; |
| 67 | + CS.begin(UUID,_CS_UUID_SIZE); |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | +Finally, use it: |
| 72 | +``` |
| 73 | +loop( |
| 74 | + CS.println("Hello"); |
| 75 | + delay(1000); |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +## Issues |
| 80 | +This library has been tested to work in several configurations. It probably has edge cases that completely break it. Future updates to Seeed_Arduino_CAN could also break it. |
| 81 | +If you find an issue, [Report it](https://github.com/techie66/CANSerial/issues/new) |
| 82 | + |
| 83 | +## Contributing |
| 84 | +All pull requests welcome! |
| 85 | + |
| 86 | +## License |
| 87 | +Licensed under GPLv3, see LICENSE file for details. |
0 commit comments