Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 months ago.
This example does not work for me. Does anyone know what the issue might be?
I've tried this exact example, and I'm unable to read any CAN messages. The program only enters the can1.write if statement 1 time, and prints "Message sent: 1". Also, there is never a Message received: " printed. I've checked the hw/the connection multiple times and it is consistent with the diagram shown. I added the send function in the while loop, but it also does not send the message.
Question relating to:
1 Answer
9 years, 8 months ago.
Really an answer to the comment above rather than the main question but the comment edit box is too small to be usable.
It always prints 0 because msg.data is never being set.
void send() { //Sends a message from CAN1 pc.printf("send()\n"); CANMessage msg; // msg.data is undefined at this point. can1.write(CANMessage(887, &counter, 1)); // sends the message directly, doesn't change msg. if(can1.write(CANMessage(887, &counter, 1))) { // sends the same message a second time, still doesn't change msg counter++; printf("MESSAGE SENT: %d, %d\n", counter, msg.data[0]);// msg.data[0] had not been set and so the output is random. led1 = !led1; } else{ pc.printf("Failed to send the message\n"); } //led1 = !led1; }
Instead you want something like:
void send() { pc.printf("send()\n"); CANMessage msg(887, &counter, 1); // create and initialize msg to being the message to send. if(can1.write(msg)) { // sends the message counter++; printf("MESSAGE SENT: %d, %d\n", counter, msg.data[0]); led1 = !led1; } else{ pc.printf("Failed to send the message\n"); } //led1 = !led1; }
I changed the frequency to 500k, and it transmits every time now! Also, I modified the print statement within the send function to see what it is I'm sending: printf("MESSAGE SENT: %d, %d\n", counter, msg.data[0]); This does not however show the counter value. It always prints '0'. Why is that?
posted by Tron Tronian 22 Jun 2015Unless you've made other changes msg is not defined in your send function, it's certainly never getting set to anything.
posted by Andy A 22 Jun 2015Good point. I forgot to mention that I added "CANMessage msg;" as well in the send function.
posted by Tron Tronian 22 Jun 2015Right now all I know is that your code is something different to what's posted above. That leaves a fair amount of uncertainty as to what it is. e.g. did you also set the value of msg in that function?
Please use
posted by Andy A 24 Jun 2015<<code>> and <</code>>
and post the code rather than a screen shot of what it used to be before it was changed.Thanks for the tip Andy. Here is the current code: