8 years, 10 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.

/media/uploads/kTronic/can.png

Question relating to:

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 2015

Unless 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 2015

Good point. I forgot to mention that I added "CANMessage msg;" as well in the send function.

posted by Tron Tronian 22 Jun 2015

Right 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 <<code>> and <</code>> and post the code rather than a screen shot of what it used to be before it was changed.

posted by Andy A 24 Jun 2015

Thanks for the tip Andy. Here is the current code:

#include <mbed.h>
Serial pc(USBTX, USBRX); //Declaring pc 
Ticker ticker;      //ticker is used to call a function at a recurring interval
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10);
CAN can2(p30, p29);
char counter = 0;
void send();    //Function declaration
 
void send() {           //Sends a message from CAN1
    pc.printf("send()\n");
    CANMessage msg;     
    can1.write(CANMessage(887, &counter, 1));
    if(can1.write(CANMessage(887, &counter, 1))) {
        counter++;
        printf("MESSAGE SENT: %d, %d\n", counter, msg.data[0]);//!! Why does it print 0 for msg.data[0]. It should be the current counter value
        led1 = !led1;
    } 
    else{
        pc.printf("Failed to send the message\n");
        }
    //led1 = !led1;
}
 
int main() {
    pc.printf("main()\n");
    can2.frequency(500000);
    can1.frequency(500000);
    ticker.attach(&send, 1);        //Calls the function send() every second
    CANMessage msg;     
    send(); 
    while(1) {                     //Constantly check for a Rx'ed message on CAN2, and notify if there is one
        send();
        pc.printf("loop()\n");
        pc.printf("RXed MSG:%d\n", can2.read(msg));
        if(can2.read(msg)) {
            pc.printf("Message received: %d\n", msg.data[0]);
            led2 = !led2;
        } 
        wait(0.2);
    }
}
posted by Tron Tronian 25 Jun 2015

1 Answer

8 years, 9 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;
}