Hello all, I'm having problems with a CAN-BUS communication with a MCP2515 using the CAN_BUS_SHIELD library for Arduino, the thing is I'm starting to send data, but when I send the first data it keeps sending the same information, I can't change of data although I indicate to the Arduino to send another data, my code is the next:
#include <SPI.h>
#include "mcp2515_can.h"
const int SPI_CS_PIN = 10;
const int CAN_INT = 2;
mcp2515_can CAN(SPI_CS_PIN);
unsigned int id = 0x01;
byte len = 0;
void setup() {
pinMode(A0,INPUT_PULLUP);
SERIAL_PORT_MONITOR.begin(115200);
while(!Serial);
while(CAN_OK != CAN.begin(CAN_125KBPS)){
SERIAL_PORT_MONITOR.println("CAN not init");
delay(1000);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
}
void loop() {
setAndSend_data();
}
void setAndSend_data(){
if(Serial.available()){
String c = Serial.readString();
unsigned char buff2[1] = {c.toInt()};
if(buff2[1]>255) buff2[1] = 0;
SERIAL_PORT_MONITOR.print("VALUE TO SEND: ");
SERIAL_PORT_MONITOR.println(buff2[1]);
delay(1000);
if(!CAN.sendMsgBuf(id,0,sizeof(buff2),buff2)){
SERIAL_PORT_MONITOR.print("VALUE OF ");
SERIAL_PORT_MONITOR.print(buff2[1]);
SERIAL_PORT_MONITOR.println(" HAS BEEN SENT CORRECTLY ");
}
else{
SERIAL_PORT_MONITOR.print("VALUE OF ");
SERIAL_PORT_MONITOR.print(buff2[1]);
SERIAL_PORT_MONITOR.println(" HASN'T BEEN SENT ");
}
delay(1000);
}
}
what host microcontroller are you using?
can you give a link to the actual CAN shield you are using?
what data are you entering? do you terminate text with a newline?
what does the serial monitor display (upload as text not a screen image)?
what do you think it should display?
are you sure the CAN interface is working?
recommend you get a USB-CAN dongle it is useful for testing the CAN interface and monitoring the bus
Yep that is what it should be doing. I will take a SWAG and say you do not have a receiver. CAN (Controller Area Network) uses an in frame response as part of the protocol. Two things that can bite you is Proper Bus termination and failure to acknowledge the message. When a node sends a message it must be acknowledged by another node on the bus as part of the transmitting frame. The transmitter sends that bit as recessive and the receiver acknowledges it by setting that bit dominate, if that does not happen you get what you are seeing. It will keep trying to send the message until its error counter counts out then it quits.
The recessive and dominate modes of the bits are part of the arbitration and must be supported by the bus. This enables the NDA (Non Destructive Arbitration) of the message.
My recommendation is build a second node to monitor your transmissions and I expert it will work.
You're right, I don't have a receiver 'cause I was only watching the message by oscilloscope, so maybe I'm having that problem you say , the message is not received for any node and my device still sending the same information hoping to be acknowledge, I'm so grateful with you and thank you for the help.