/*
CANWrite
Write and send CAN Bus messages
See the full documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/can
*//**************************************************************************************
* INCLUDE
**************************************************************************************/#include<Arduino_CAN.h>/**************************************************************************************
* CONSTANTS
**************************************************************************************/staticuint32_tconstCAN_ID=0x20;/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/voidsetup(){Serial.begin(115200);while(!Serial){}if(!CAN.begin(CanBitRate::BR_250k)){Serial.println("CAN.begin(...) failed.");for(;;){}}}staticuint32_tmsg_cnt=0;voidloop(){/* Assemble a CAN message with the format of
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
*/uint8_tconstmsg_data[]={0xCA,0xFE,0,0,0,0,0,0};memcpy((void*)(msg_data+4),&msg_cnt,sizeof(msg_cnt));CanMsgconstmsg(CanStandardId(CAN_ID),sizeof(msg_data),msg_data);/* Transmit the CAN message, capture and display an
* error core in case of failure.
*/if(intconstrc=CAN.write(msg);rc<0){Serial.print("CAN.write(...) failed with error code ");Serial.println(rc);for(;;){}}/* Increase the message counter. */msg_cnt++;/* Only send one message per second. */delay(1000);}
/*
CANWrite
Write and send CAN Bus messages
See the full documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/can
*//**************************************************************************************
* INCLUDE
**************************************************************************************/#include<Arduino_CAN.h>/**************************************************************************************
* CONSTANTS
**************************************************************************************/staticuint32_tconstCAN_ID=0x20;/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/voidsetup(){Serial.begin(115200);while(!Serial){}if(!CAN.begin(CanBitRate::BR_250k)){Serial.println("CAN.begin(...) failed.");for(;;){}}}staticuint32_tmsg_cnt=0;voidloop(){/* Assemble a CAN message with the format of
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
*/uint8_tconstmsg_data[]={0xCA,0xFE,0,0,0,0,0,0};memcpy((void*)(msg_data+4),&msg_cnt,sizeof(msg_cnt));CanMsgconstmsg(CanStandardId(CAN_ID),sizeof(msg_data),msg_data);/* Transmit the CAN message, capture and display an
* error core in case of failure.
*/if(intconstrc=CAN.write(msg);rc<0){Serial.print("CAN.write(...) failed with error code ");Serial.println(rc);for(;;){}}/* Increase the message counter. */msg_cnt++;/* Only send one message per second. */delay(1000);}
/*
CANRead
Receive and read CAN Bus messages
See the full documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/can
*//**************************************************************************************
* INCLUDE
**************************************************************************************/#include<Arduino_CAN.h>/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/voidsetup(){Serial.begin(115200);while(!Serial){}if(!CAN.begin(CanBitRate::BR_250k)){Serial.println("CAN.begin(...) failed.");for(;;){}}}voidloop(){if(CAN.available()){CanMsgconstmsg=CAN.read();Serial.println(msg);}}