Q&A
前提
ArduinoでCAN通信を行いたい。
実現したいこと
2台のArduino UnoとCANバスモジュールをもちいて双方向の通信を行いたい。
https://qiita.com/covao/items/d30fa5e36470bbee3be7
を参考にしました。
CAN0(一つ目のArduinoとCANバスモジュール)からByte列を送り、CAN1(2つ目のArduinoとCANバスモジュール)でそれをそのままオウム返しする。
発生している問題・エラーメッセージ
下記のようなコードを書きましたが、往路CAN0→CAN1は送信受信できているが、復路CAN1→CAN0は復号?できない。
復路受け側でif (CAN0.checkReceive() == CAN_MSGAVAIL) {}はTrueになっていることをSerial.printで確認しているが
その内容は長さが0となっている。
受信できている往路受け側ではこの長さが8となっていることも確認済。
該当のソースコード
Arduino CAN0
1// MCP2515 CAN Send and Receive 2 3#include <mcp_can.h> //https://github.com/coryjfowler/MCP_CAN_lib 4#include <SPI.h> 5 6unsigned long rxId; 7byte len; 8byte rxBuf[8]; 9byte txBuf0[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 10byte txBuf1[] = {0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8}; 11long Pre_millis; 12 13MCP_CAN CAN0(10);// CAN0 CS: pin 10 14MCP_CAN CAN1(9); // CAN1 CS: pin 9 15 16void setup() 17{ 18 Serial.begin(115200); 19 20 // init CAN0 bus, baudrate: 250kbps@8MHz 21 if (CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK) { 22 Serial.println("CAN0: Init OK!"); 23 CAN0.setMode(MCP_NORMAL); 24 } else { 25 Serial.println("CAN0: Init Fail!"); 26 } 27} 28int count = 0; 29 30void loop() { 31 if (millis() - Pre_millis > 1000) { 32 if (count++ % 2 == 0)CAN0.sendMsgBuf(0x100, 0, 8, txBuf0); 33 else CAN0.sendMsgBuf(0x101, 0, 8, txBuf1); 34 Serial.print("Send ID: "); 35 Serial.println(count); 36 Pre_millis = millis(); 37 } 38 39 if (CAN0.checkReceive() == CAN_MSGAVAIL) { 40 CAN1.readMsgBuf(&rxId, &len, rxBuf); 41 if (len > 0) { 42 Serial.print("Length: "); 43 Serial.print(len); 44 Serial.print(" Recive ID: "); 45 Serial.print(rxId, HEX); 46 Serial.print(" Data: "); 47 for (byte i = 0; i < len; i++) { 48 Serial.print(rxBuf[i], HEX); 49 Serial.print(" "); 50 } 51 Serial.println(); 52 } 53 } 54}
Arduino
1// MCP2515 CAN Recieve and Return 2 3#include <mcp_can.h> //https://github.com/coryjfowler/MCP_CAN_lib 4#include <SPI.h> 5 6unsigned long rxId; 7byte len; 8byte rxBuf[8]; 9byte txBuf0[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 10byte txBuf1[] = {0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8}; 11long Pre_millis; 12 13MCP_CAN CAN0(10);// CAN0 CS: pin 10 14MCP_CAN CAN1(9); // CAN1 CS: pin 9 15 16void setup() 17{ 18 Serial.begin(115200); 19 20 // init CAN1 bus, baudrate: 250kbps@8MHz 21 if (CAN1.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK) { 22 Serial.println("CAN1: Init OK!"); 23 CAN1.setMode(MCP_NORMAL); 24 } else { 25 Serial.println("CAN1: Init Fail!"); 26 } 27} 28 29void loop() { 30 static int count = 0; 31 32 if (CAN1.checkReceive() == CAN_MSGAVAIL) { 33 CAN1.readMsgBuf(&rxId, &len, rxBuf); 34 Serial.print("Length: "); 35 Serial.print(len); 36 Serial.print(" Recive ID: "); 37 Serial.print(rxId, HEX); 38 Serial.print(" Data: "); 39 for (byte i = 0; i < len; i++) { 40 Serial.print(rxBuf[i], HEX); 41 Serial.print(" "); 42 } 43 Serial.println(); 44 //オウム返し送信 45 Serial.println("Send..."); 46 CAN1.sendMsgBuf(0x100, 0, 8, rxBuf); 47 } 48} 49
試したこと
Arduinoとモジュールの送受信を入れ替え
補足情報(FW/ツールのバージョンなど)
Arduino IDE 1.8.13
Widows 10
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。