Try to use a MKR zero and the MKR CAN SHIELD to send J1939 CAN Messages. I started with the example file for the CAN send in library MCP_CAN_lib. I changed the CS Pin to 3 as it defaults to 10 but still receiving "error sending message...". Any help is appreciated.
// CAN Send Example
//
#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN0(3); // Set CS to pin 3 for MKR
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) for (int i = 0; i <= 20; i++){
Serial.println("MCP2515 Initialized Successfully!");
delay(500);
}
else for (int i = 0; i <= 20; i++) {
Serial.println("Error Initializing MCP2515...");
delay(500);
}
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
void loop()
{
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
delay(500); // send data per 100ms
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
What is the best way to go from float to hex to send out on my CAN message? I am reading an analog input then taking it to a float to scale then want to send it out as a CAN message.
You need to have a look at how can works - how you send it out depends on how you will read it back , you can split your number into several fields to transmit , putting the length in the “ message”, ( as you can see in your sketch) but you need to also read in the same way .
If you are trying to simulate an existing can message ( eg into a car ECU) then look at its format , then how you might translate your number to that format.
Floats are stored in memory as 4 bytes on Arduino , so you can just send these bytes . There are ways to store data in specific locations ( not sure how without googling) , which you could then copy into your message field , but that may give you a solution …I’m unsure ..you may even be able to print a float variable in hex directly ???
I have the structure of the CAN message determined.
Thanks I was able to look into splitting the float into its bytes and then used that in my CAN message.
Now I just need to determine why my value seems to be off. I am getting the integer I expect on the serial monitor but a much larger value on the CAN monitor I am using. This is what I have so far.