Skip to content

Commit d3ec366

Browse files
committed
Create repository
0 parents  commit d3ec366

File tree

8 files changed

+1450
-0
lines changed

8 files changed

+1450
-0
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.

examples/buffered/buffered.ino

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <mcp2515_can.h>
2+
#include <CANSerial.h>
3+
4+
// CAN Constants
5+
const int SPI_CS_PIN = 10;
6+
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
7+
8+
Buffered_MCP_CAN bufCAN(CAN);
9+
CANSerial CS1(0x6E0,bufCAN);
10+
CANSerial CS2(0x6E0,bufCAN);
11+
12+
void setup() {
13+
Serial.begin(115200);
14+
Serial.println("Startup initiated!");
15+
16+
// CAN Setup
17+
while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_12MHz)) { // init can bus : baudrate = 500k
18+
Serial.println("CAN ERROR");
19+
delay(100);
20+
}
21+
22+
// Uncomment the following line if hardware is setup to enable transceiver from MCP PIO
23+
CAN.mcpPinMode(MCP_RX0BF,MCP_PIN_OUT);
24+
25+
static unsigned char UUID1[_CS_UUID_SIZE] = {0xCE, 0xF2, 0x35, 0x2C, 0xE1, 0xB1};
26+
CS1.begin(UUID1,_CS_UUID_SIZE);
27+
static unsigned char UUID2[_CS_UUID_SIZE] = {0xCE, 0xF2, 0x35, 0x2C, 0xE1, 0xB2};
28+
CS2.begin(UUID2,_CS_UUID_SIZE);
29+
}
30+
31+
void loop() {
32+
CS1.println("First");
33+
CS2.println("Second");
34+
delay(1000);
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <mcp2515_can.h>
2+
#include <CANSerial.h>
3+
4+
// CAN Constants
5+
const int SPI_CS_PIN = 10;
6+
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
7+
8+
CANSerial CS(0x6E0,CAN);
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
Serial.println("Startup initiated!");
13+
14+
// CAN Setup
15+
while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_12MHz)) { // init can bus : baudrate = 500k
16+
Serial.println("CAN ERROR");
17+
delay(100);
18+
}
19+
20+
// Uncomment the following line if hardware is setup to enable transceiver from MCP PIO
21+
CAN.mcpPinMode(MCP_RX0BF,MCP_PIN_OUT);
22+
23+
static unsigned char UUID[_CS_UUID_SIZE] = {0xCE, 0xF2, 0x35, 0x2C, 0xE1, 0xB3};
24+
CS.begin(UUID,_CS_UUID_SIZE);
25+
}
26+
27+
void loop() {
28+
CS.println("Hello");
29+
delay(1000);
30+
}

keywords.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Syntax Coloring Map For CANSerial
2+
3+
# Datatypes (KEYWORD1)
4+
Buffered_MCP_CAN KEYWORD1
5+
CANSerial KEYWORD1
6+
7+
# Methods and Functions (KEYWORD2)
8+
enableTxInterrupt KEYWORD2
9+
reserveTxBuffers KEYWORD2
10+
getLastTxBuffer KEYWORD2
11+
setSleepWakeup KEYWORD2
12+
sleep KEYWORD2
13+
wake KEYWORD2
14+
setMode KEYWORD2
15+
getMode KEYWORD2
16+
readMsgBufID KEYWORD2
17+
trySendMsgBuf KEYWORD2
18+
sendMsgBuf KEYWORD2
19+
clearBufferTransmitIfFlags KEYWORD2
20+
readRxTxStatus KEYWORD2
21+
checkClearRxStatus KEYWORD2
22+
checkClearTxStatus KEYWORD2
23+
mcpPinMode KEYWORD2
24+
mcpDigitalWrite KEYWORD2
25+
mcpDigitalRead KEYWORD2
26+
27+
isBuffered KEYWORD2
28+
29+
30+
# Structures (KEYWORD3)
31+
can_frame KEYWORD3
32+
33+
# Instances (KEYWORD2)
34+
35+
36+
# Constants (LITERAL1)
37+
_CS_UUID_SIZE LITERAL1
38+
_CS_MAX_RX_BUFF LITERAL1
39+
_CS_MAX_TX_BUFF LITERAL1
40+
GCC_VERSION LITERAL1
41+
_CAN_MAX_RX_BUF LITERAL1
42+
CAN_EFF_FLAG LITERAL1
43+
CAN_RTR_FLAG LITERAL1
44+
CAN_ERR_FLAG LITERAL1
45+
CAN_SFF_MASK LITERAL1
46+
CAN_EFF_MASK LITERAL1
47+
CAN_ERR_MASK LITERAL1
48+

library.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name=CANSerial
2+
version=0.2.0
3+
author=Jacob Geigle <[email protected]>
4+
maintainer=Jacob Geigle <[email protected]>
5+
sentence=Use CAN bus as physical layer for serial/uart communication
6+
paragraph=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/
7+
category=Communication
8+
url=http://www.github.com/techie66/CANSerial
9+
architectures=*
10+
includes=CANSerial.h
11+
depends=Seeed_Arduino_CAN

0 commit comments

Comments
 (0)