Skip to content

Commit 85648cd

Browse files
committed
Add: baudrate 800KBPS to canbus-monitor
1 parent 8b82b6e commit 85648cd

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

examples/canbus-monitor/can-serial.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (C) 2015 Anton Viktorov <[email protected]>
88
* https://github.com/latonita/can-ascii
99
*
10-
* This library is free software. You may use/redistribute it under The MIT License terms.
10+
* This library is free software. You may use/redistribute it under The MIT License terms.
1111
*
1212
*****************************************************************************************/
1313

@@ -124,6 +124,7 @@ void Can232::loopFunc() {
124124
Serial.flush();
125125
}
126126
}
127+
127128
void Can232::serialEventFunc() {
128129
while (Serial.available()) {
129130
char inChar = (char)Serial.read();
@@ -151,7 +152,7 @@ INT8U Can232::exec() {
151152
case LW232_ERR_NOT_IMPLEMENTED:
152153
// Choose behavior: will it fail or not when not implemented command comes in. Some can monitors might be affected by this selection.
153154
Serial.write(LW232_RET_ASCII_ERROR);
154-
//Serial.write(LW232_RET_ASCII_OK);
155+
//Serial.write(LW232_RET_ASCII_OK);
155156
break;
156157
default:
157158
Serial.write(LW232_RET_ASCII_ERROR);
@@ -169,20 +170,20 @@ INT8U Can232::parseAndRunCommand() {
169170
// __debug_buf("RX:", (char*)lw232Message, strlen((char*)lw232Message));
170171

171172
switch (lw232Message[0]) {
172-
case LW232_CMD_SETUP:
173+
case LW232_CMD_SETUP:
173174
// Sn[CR] Setup with standard CAN bit-rates where n is 0-9.
174175
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
175176
idx = HexHelper::parseNibbleWithLimit(lw232Message[1], LW232_CAN_BAUD_NUM);
176-
lw232CanSpeedSelection = lw232CanBaudRates[idx];
177+
lw232CanSpeedSelection = lw232CanBaudRates[idx];
177178
}
178179
else {
179180
ret = LW232_ERR;
180181
}
181182
break;
182-
case LW232_CMD_SETUP_BTR:
183+
case LW232_CMD_SETUP_BTR:
183184
// sxxyy[CR] Setup with BTR0/BTR1 CAN bit-rates where xx and yy is a hex value.
184185
ret = LW232_ERR; break;
185-
case LW232_CMD_OPEN:
186+
case LW232_CMD_OPEN:
186187
// O[CR] Open the CAN channel in normal mode (sending & receiving).
187188
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
188189
ret = openCanBus();
@@ -194,7 +195,7 @@ INT8U Can232::parseAndRunCommand() {
194195
ret = LW232_ERR;
195196
}
196197
break;
197-
case LW232_CMD_LISTEN:
198+
case LW232_CMD_LISTEN:
198199
// L[CR] Open the CAN channel in listen only mode (receiving).
199200
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
200201
ret = openCanBus();
@@ -206,7 +207,7 @@ INT8U Can232::parseAndRunCommand() {
206207
ret = LW232_ERR;
207208
}
208209
break;
209-
case LW232_CMD_CLOSE:
210+
case LW232_CMD_CLOSE:
210211
// C[CR] Close the CAN channel.
211212
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) {
212213
lw232CanChannelMode = LW232_STATUS_CAN_CLOSED;
@@ -228,7 +229,7 @@ INT8U Can232::parseAndRunCommand() {
228229
ret = LW232_ERR;
229230
} else if (lw232AutoPoll) {
230231
ret = LW232_OK_SMALL;
231-
}
232+
}
232233
}
233234
else {
234235
ret = LW232_ERR;
@@ -412,8 +413,6 @@ INT8U Can232::readMsgBufID(INT32U *ID, INT8U *len, INT8U buf[]) {
412413
#endif
413414
}
414415

415-
416-
417416
INT8U Can232::receiveSingleFrame() {
418417
INT8U ret = LW232_OK;
419418
INT8U idx = 0;
@@ -445,7 +444,7 @@ INT8U Can232::receiveSingleFrame() {
445444
INT32U time = millis();
446445
if (lw232TimeStamp == LW232_TIMESTAMP_ON_NORMAL) {
447446
// standard LAWICEL protocol. two bytes.
448-
time %= 60000;
447+
time %= 60000;
449448
} else {
450449
// non standard protocol - 4 bytes timestamp
451450
HexHelper::printFullByte(HIGH_BYTE(HIGH_WORD(time)));
@@ -473,7 +472,7 @@ INT8U Can232::isExtendedFrame() {
473472

474473

475474
INT8U Can232::checkPassFilter(INT32U addr) {
476-
if (userAddressFilterFunc == 0)
475+
if (userAddressFilterFunc == 0)
477476
return LW232_FILTER_PROCESS;
478477

479478
return (*userAddressFilterFunc)(addr);

examples/canbus-monitor/can-serial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const INT32U lw232SerialBaudRates[] //PROGMEM
155155
= { 230400, 115200, 57600, 38400, 19200, 9600, 2400 };
156156

157157
const INT32U lw232CanBaudRates[] //PROGMEM
158-
= { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS /*CAN_800KBPS*/, CAN_1000KBPS, CAN_83K3BPS };
158+
= { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_800KBPS, CAN_1000KBPS, CAN_83K3BPS };
159159

160160
class Can232
161161
{

examples/canbus-monitor/canbus-monitor.ino

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (C) 2015 Anton Viktorov <[email protected]>
88
* https://github.com/latonita/arduino-canbus-monitor
99
*
10-
* This library is free software. You may use/redistribute it under The MIT License terms.
10+
* This library is free software. You may use/redistribute it under The MIT License terms.
1111
*
1212
*****************************************************************************************/
1313

@@ -42,8 +42,19 @@ void setup() {
4242
// associate the Can232 and the CAN lowlevel driver
4343
Can232::attach(&CAN);
4444

45+
/*
46+
* Warning:
47+
* CAN232 protocol command Sn[CR] --- setup with standard CAN bit-rates
48+
* not works on ARDUINO UNO when used with Linux slcand.
49+
*
50+
* Linux slcand open the USB-SERAIL and send commands imediately,
51+
* but the UNO are in startup(reset fired by UNO builtin USB-SERIAL),
52+
* UNO missed those commands.
53+
*
54+
*/
55+
4556
// Can232::init (RATE, CLOCK)
46-
// Rates: CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS, CAN_1000KBPS, CAN_83K3BPS
57+
// Rates: CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_800KBPS, CAN_1000KBPS, CAN_83K3BPS
4758
// Default is CAN_83K3BPS.
4859
// Clock: MCP_16MHz or MCP_8MHz.
4960
// Default is MCP_16MHz. Please note, not all CAN speeds supported. check big switch in mcp_can.cpp
@@ -53,7 +64,7 @@ void setup() {
5364
Can232::init(CAN_125KBPS, MCP_16MHz); // set default rate you need here and clock frequency of CAN shield. Typically it is 16MHz, but on some MCP2515 + TJA1050 it is 8Mhz
5465

5566
// optional custom packet filter to reduce number of messages comingh through to canhacker
56-
// Can232::setFilter(myCustomAddressFilter);
67+
// Can232::setFilter(myCustomAddressFilter);
5768
}
5869

5970
INT8U myCustomAddressFilter(INT32U addr) {
@@ -84,6 +95,11 @@ void loop() {
8495
Can232::loop();
8596
}
8697

87-
void serialEventRun() {
98+
#if defined(ARDUINO_ARCH_AVR)
99+
void serialEvent()
100+
#else
101+
void serialEventRun()
102+
#endif
103+
{
88104
Can232::serialEvent();
89105
}

0 commit comments

Comments
 (0)