From 85648cd0fc0a664734e67e3d93eecbe0b7b2cb22 Mon Sep 17 00:00:00 2001 From: turmary Date: Tue, 2 Feb 2021 21:11:38 +0800 Subject: [PATCH] Add: baudrate 800KBPS to canbus-monitor --- examples/canbus-monitor/can-serial.cpp | 25 +++++++++++----------- examples/canbus-monitor/can-serial.h | 2 +- examples/canbus-monitor/canbus-monitor.ino | 24 +++++++++++++++++---- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/examples/canbus-monitor/can-serial.cpp b/examples/canbus-monitor/can-serial.cpp index 92c2972..3caebd5 100644 --- a/examples/canbus-monitor/can-serial.cpp +++ b/examples/canbus-monitor/can-serial.cpp @@ -7,7 +7,7 @@ * Copyright (C) 2015 Anton Viktorov * https://github.com/latonita/can-ascii * -* This library is free software. You may use/redistribute it under The MIT License terms. +* This library is free software. You may use/redistribute it under The MIT License terms. * *****************************************************************************************/ @@ -124,6 +124,7 @@ void Can232::loopFunc() { Serial.flush(); } } + void Can232::serialEventFunc() { while (Serial.available()) { char inChar = (char)Serial.read(); @@ -151,7 +152,7 @@ INT8U Can232::exec() { case LW232_ERR_NOT_IMPLEMENTED: // Choose behavior: will it fail or not when not implemented command comes in. Some can monitors might be affected by this selection. Serial.write(LW232_RET_ASCII_ERROR); - //Serial.write(LW232_RET_ASCII_OK); + //Serial.write(LW232_RET_ASCII_OK); break; default: Serial.write(LW232_RET_ASCII_ERROR); @@ -169,20 +170,20 @@ INT8U Can232::parseAndRunCommand() { // __debug_buf("RX:", (char*)lw232Message, strlen((char*)lw232Message)); switch (lw232Message[0]) { - case LW232_CMD_SETUP: + case LW232_CMD_SETUP: // Sn[CR] Setup with standard CAN bit-rates where n is 0-9. if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) { idx = HexHelper::parseNibbleWithLimit(lw232Message[1], LW232_CAN_BAUD_NUM); - lw232CanSpeedSelection = lw232CanBaudRates[idx]; + lw232CanSpeedSelection = lw232CanBaudRates[idx]; } else { ret = LW232_ERR; } break; - case LW232_CMD_SETUP_BTR: + case LW232_CMD_SETUP_BTR: // sxxyy[CR] Setup with BTR0/BTR1 CAN bit-rates where xx and yy is a hex value. ret = LW232_ERR; break; - case LW232_CMD_OPEN: + case LW232_CMD_OPEN: // O[CR] Open the CAN channel in normal mode (sending & receiving). if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) { ret = openCanBus(); @@ -194,7 +195,7 @@ INT8U Can232::parseAndRunCommand() { ret = LW232_ERR; } break; - case LW232_CMD_LISTEN: + case LW232_CMD_LISTEN: // L[CR] Open the CAN channel in listen only mode (receiving). if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) { ret = openCanBus(); @@ -206,7 +207,7 @@ INT8U Can232::parseAndRunCommand() { ret = LW232_ERR; } break; - case LW232_CMD_CLOSE: + case LW232_CMD_CLOSE: // C[CR] Close the CAN channel. if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) { lw232CanChannelMode = LW232_STATUS_CAN_CLOSED; @@ -228,7 +229,7 @@ INT8U Can232::parseAndRunCommand() { ret = LW232_ERR; } else if (lw232AutoPoll) { ret = LW232_OK_SMALL; - } + } } else { ret = LW232_ERR; @@ -412,8 +413,6 @@ INT8U Can232::readMsgBufID(INT32U *ID, INT8U *len, INT8U buf[]) { #endif } - - INT8U Can232::receiveSingleFrame() { INT8U ret = LW232_OK; INT8U idx = 0; @@ -445,7 +444,7 @@ INT8U Can232::receiveSingleFrame() { INT32U time = millis(); if (lw232TimeStamp == LW232_TIMESTAMP_ON_NORMAL) { // standard LAWICEL protocol. two bytes. - time %= 60000; + time %= 60000; } else { // non standard protocol - 4 bytes timestamp HexHelper::printFullByte(HIGH_BYTE(HIGH_WORD(time))); @@ -473,7 +472,7 @@ INT8U Can232::isExtendedFrame() { INT8U Can232::checkPassFilter(INT32U addr) { - if (userAddressFilterFunc == 0) + if (userAddressFilterFunc == 0) return LW232_FILTER_PROCESS; return (*userAddressFilterFunc)(addr); diff --git a/examples/canbus-monitor/can-serial.h b/examples/canbus-monitor/can-serial.h index 2521fbd..a45756e 100644 --- a/examples/canbus-monitor/can-serial.h +++ b/examples/canbus-monitor/can-serial.h @@ -155,7 +155,7 @@ const INT32U lw232SerialBaudRates[] //PROGMEM = { 230400, 115200, 57600, 38400, 19200, 9600, 2400 }; const INT32U lw232CanBaudRates[] //PROGMEM - = { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS /*CAN_800KBPS*/, CAN_1000KBPS, CAN_83K3BPS }; + = { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_800KBPS, CAN_1000KBPS, CAN_83K3BPS }; class Can232 { diff --git a/examples/canbus-monitor/canbus-monitor.ino b/examples/canbus-monitor/canbus-monitor.ino index 0df97bd..07e8a04 100644 --- a/examples/canbus-monitor/canbus-monitor.ino +++ b/examples/canbus-monitor/canbus-monitor.ino @@ -7,7 +7,7 @@ * Copyright (C) 2015 Anton Viktorov * https://github.com/latonita/arduino-canbus-monitor * -* This library is free software. You may use/redistribute it under The MIT License terms. +* This library is free software. You may use/redistribute it under The MIT License terms. * *****************************************************************************************/ @@ -42,8 +42,19 @@ void setup() { // associate the Can232 and the CAN lowlevel driver Can232::attach(&CAN); + /* + * Warning: + * CAN232 protocol command Sn[CR] --- setup with standard CAN bit-rates + * not works on ARDUINO UNO when used with Linux slcand. + * + * Linux slcand open the USB-SERAIL and send commands imediately, + * but the UNO are in startup(reset fired by UNO builtin USB-SERIAL), + * UNO missed those commands. + * + */ + // Can232::init (RATE, CLOCK) - // Rates: CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS, CAN_1000KBPS, CAN_83K3BPS + // Rates: CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_800KBPS, CAN_1000KBPS, CAN_83K3BPS // Default is CAN_83K3BPS. // Clock: MCP_16MHz or MCP_8MHz. // Default is MCP_16MHz. Please note, not all CAN speeds supported. check big switch in mcp_can.cpp @@ -53,7 +64,7 @@ void setup() { 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 // optional custom packet filter to reduce number of messages comingh through to canhacker - // Can232::setFilter(myCustomAddressFilter); + // Can232::setFilter(myCustomAddressFilter); } INT8U myCustomAddressFilter(INT32U addr) { @@ -84,6 +95,11 @@ void loop() { Can232::loop(); } -void serialEventRun() { +#if defined(ARDUINO_ARCH_AVR) +void serialEvent() +#else +void serialEventRun() +#endif +{ Can232::serialEvent(); }