Go Down

Topic: Arduino MKR CAN shield only works when turning on the IDE Serial Monitor  (Read 388 times) previous topic - next topic

hansng

I am trying to send messages from the Arduino MKR ZERO to another system through MKR CAN shield.
The system is first tested with the sample script in the MCP2515 library.

Code: [Select]

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(3);


void setup() {

  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x8E;
  canMsg1.data[1] = 0x87;
  canMsg1.data[2] = 0x32;
  canMsg1.data[3] = 0xFA;
  canMsg1.data[4] = 0x26;
  canMsg1.data[5] = 0x8E;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 0x0E;
  canMsg2.data[1] = 0x00;
  canMsg2.data[2] = 0x00;
  canMsg2.data[3] = 0x08;
  canMsg2.data[4] = 0x01;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;
 
  while (!Serial);
  Serial.begin(115200);

  mcp2515.begin();
  mcp2515.setBitrate(CAN_1000KBPS,MCP_16MHZ);
  mcp2515.setNormalMode();
 
  Serial.println("Example: Write to CAN");
}

void loop() {
 
  mcp2515.sendMessage(&canMsg1);
  mcp2515.sendMessage(&canMsg2);

  Serial.println("Messages sent");
 
  delay(1000);

}



However, the CAN messages cannot be sent after uploading the script to the board.
It only starts to work when I turn on the Serial Monitor on the Arduino IDE.
I try to disconnect the Arduino with the PC and then reconnect again, the CAN Shield stop sending messages. And it restarts to send messages after I turn the Serial Monitor on again.
I test this script with the Arduino UNO and MCP2515 CAN-BUS shield, and it starts to send messages after uploading the script to the board no matter I turn on the monitor or not.
Does anyone have similar problems before and know how to solve it?
Thanks.

ballscrewbob

See that "while (!Serial);"

It is waiting for the connection to become available.
You may be able to do one of two things.

A. remove the WHILE but that means it would be sending data the moment you power up the board.

B. send something from the PC or application you are using to tell the board you are wanting it to start.

Either should work.


It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

hansng

See that "while (!Serial);"

It is waiting for the connection to become available.
You may be able to do one of two things.

A. remove the WHILE but that means it would be sending data the moment you power up the board.

B. send something from the PC or application you are using to tell the board you are wanting it to start.

Either should work.



Yes, now it works. Thank you so much.

ballscrewbob

Great thanks for getting back and enjoy your Arduino adventures !

Bob.

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up