受信側スケッチです。改善の余地あり(ちょっと直しました。)
ChatGPT先生にいっぱいヒントを頂きました。
参考:コンパイル途中に次のようなメッセージ表示されていました。
警告:ライブラリAdafruit_CANはアーキテクチャsamdに対応したものであり、
アーキテクチャrp2040で動作するこのボードとは互換性がないかもしれません。
AdafruitのMCP2515ライブラリからのメッセージと思われます。
/* Adafruit MCP2515_CAN_Receiver_Exampleを改変しています。
一部省略 転記ミスや表示されてない記号があるかも <は半角に直して */
#include<Adafruit_MCP2515.h>
#define CS_PIN 20
// Set CAN bus baud rate
#define CAN_BAUDRATE (250000)
Adafruit_MCP2515 mcp(CS_PIN);
byte receivedData[8];
uint32_t id1_value = 0;
uint32_t id2_value = 0;
uint32_t id3_value = 0;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("MCP2515 Receiver test!");
if (!mcp.begin(CAN_BAUDRATE)) {
Serial.println("Error initializing MCP2515.");
while (1) delay(10);
}
Serial.println("MCP2515 chip found");
}
void loop() {
// try to parse packet
//このスケッチは受信側Receiver(read)
int packetSize = mcp.parsePacket();
if (packetSize) {
if (mcp.packetId() == 0x11) {
Serial.print(mcp.packetId(), HEX); Serial.print(" ");
id1_value = 0;
while (mcp.available()) { //8byte固定 このスケッチは受信側Receiver(read)
for (int i = 0; i <= 7; i++) {
receivedData[i] = mcp.read();
}
/*receivedData[0] = mcp.read();
receivedData[1] = mcp.read();
receivedData[2] = mcp.read();
receivedData[3] = mcp.read();
receivedData[4] = mcp.read();
receivedData[5] = mcp.read();
receivedData[6] = mcp.read();
receivedData[7] = mcp.read();*/
}
for (int i = 0; i <= 7; i++) {
id1_value |= (uint32_t)(receivedData[i] << (7 * i));
}
Serial.print("timeValue0 ");
Serial.println(id1_value);
}else if (mcp.packetId() == 0x12) {
Serial.print(mcp.packetId(), HEX); Serial.print(" ");
id2_value = 0;
while (mcp.available()) {
for (int i = 0; i <= 7; i++) {
receivedData[i] = mcp.read();
}/*
receivedData[0] = mcp.read();
receivedData[1] = mcp.read();
receivedData[2] = mcp.read();
receivedData[3] = mcp.read();
receivedData[4] = mcp.read();
receivedData[5] = mcp.read();
receivedData[6] = mcp.read();
receivedData[7] = mcp.read();*/
}
for (int i = 0; i <= 7; i++) {
id2_value |= (uint32_t)(receivedData[i] << (7 * i));
}
Serial.print("timeValue1 ");
Serial.println(id2_value);
}else if (mcp.packetId() == 0x13) {
Serial.print(mcp.packetId(), HEX); Serial.print(" ");
id3_value = 0;
while (mcp.available()) {
for (int i = 0; i <= 7; i++) {
receivedData[i] = mcp.read();
}/*receivedData[0] = mcp.read();
receivedData[1] = mcp.read();
receivedData[2] = mcp.read();
receivedData[3] = mcp.read();
receivedData[4] = mcp.read();
receivedData[5] = mcp.read();
receivedData[6] = mcp.read();
receivedData[7] = mcp.read();*/
}
for (int i = 0; i <= 7; i++) {
id3_value |= (uint32_t)(receivedData[i] << (7 * i));
}
Serial.print("timeValue2 ");
Serial.println(id3_value);
}
}
}