From cf1fbfaddfedb0688b2b5d89ebab27d251fd1a5e Mon Sep 17 00:00:00 2001 From: turmary Date: Sun, 27 Dec 2020 14:02:19 +0800 Subject: [PATCH] Add: example to receive all frames and print all fields id/type/data --- examples/receive_monitor/receive_monitor.ino | 105 +++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 examples/receive_monitor/receive_monitor.ino diff --git a/examples/receive_monitor/receive_monitor.ino b/examples/receive_monitor/receive_monitor.ino new file mode 100644 index 0000000..76fb9c1 --- /dev/null +++ b/examples/receive_monitor/receive_monitor.ino @@ -0,0 +1,105 @@ +/* + * demo: CAN-BUS Shield, receive all frames and print all fields id/type/data + * to receive frame fastly, a poll in loop() is required. + * + * Copyright (C) 2020 Seeed Technology Co.,Ltd. + */ +#include + +/*SAMD core*/ +#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE + #define SERIAL SerialUSB +#else + #define SERIAL Serial +#endif + +#define CAN_2515 +// #define CAN_2518FD + +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 + +// Set SPI CS Pin according to your hardware +// For Wio Terminal w/ MCP2518FD RPi Hat: +// Channel 0 SPI_CS Pin: BCM 8 +// Channel 1 SPI_CS Pin: BCM 7 +// Interupt Pin: BCM25 +// ***************************************** +// For Arduino MCP2515 Hat: +// SPI_CS Pin: D9 + +#ifdef CAN_2518FD +#include "mcp2518fd_can.h" +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +mcp2518fd CAN(SPI_CS_PIN); // Set CS pin +#define MAX_DATA_SIZE 64 +#endif + +#ifdef CAN_2515 +#include "mcp2515_can.h" +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +mcp2515_can CAN(SPI_CS_PIN); // Set CS pin +#define MAX_DATA_SIZE 8 +#endif + +void setup() { + SERIAL.begin(115200); + +#ifdef CAN_2518FD + while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k +#else + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k +#endif + SERIAL.println("CAN BUS Shield init fail"); + SERIAL.println(" Init CAN BUS Shield again"); + delay(100); + } + SERIAL.println("CAN BUS Shield init ok!"); +} + +uint32_t id; +uint8_t type; // bit0: ext, bit1: rtr +uint8_t len; +byte cdata[MAX_DATA_SIZE] = {0}; + +void loop() { + // check if data coming + if (CAN_MSGAVAIL != CAN.checkReceive()) { + return; + } + + char prbuf[0x40]; + int i, n; + + unsigned long t = millis(); + // read data, len: data length, buf: data buf + CAN.readMsgBuf(&len, cdata); + + id = CAN.getCanId(); + type = (CAN.isExtendedFrame() << 0) | + (CAN.isRemoteRequest() << 1); + /* + * MCP2515(or this driver) could not handle properly + * the data carried by remote frame + */ + + n = sprintf(prbuf, "%04lu.%03d ", t / 1000, int(t % 1000)); + /* Displayed type: + * + * 0x00: standard data frame + * 0x02: extended data frame + * 0x30: standard remote frame + * 0x32: extended remote frame + */ + static const byte type2[] = {0x00, 0x02, 0x30, 0x32}; + n += sprintf(prbuf + n, "RX: [%08lX](%02X) ", (unsigned long)id, type2[type]); + // n += sprintf(prbuf, "RX: [%08lX](%02X) ", id, type); + + for (i = 0; i < len; i++) { + n += sprintf(prbuf + n, "%02X ", cdata[i]); + } + SERIAL.println(prbuf); +} +// END FILE