CAN is not working without Ticker

04 May 2017

Hello there,

I'm currently working on a STM32F469I-Disco Board. I'm successfully using CAN and TouchScreen for my application. At the moment I'm using the CAN example program sending a value each second on the bus.

But once I try to call the send funtion with e.g. a touch press, my compiler gives me the following error:

"No instance of constructor "mbed::CANMessage::CANMessage" matches the argument list in "main.cpp" - pointing towards "CANMessage"

In short, this is working:

Works

Ticker ticker; // Define ticker

void send() {
    if(can2.write(CANMessage(1337, &npressed, 1))) {
        sprintf((char*)text, "Message sent: %d", counter);
        lcd.DisplayStringAt(100, 100, (uint8_t *)&text, LEFT_MODE);
        counter++;
    }
    led1 = !led1;
}
...
void main(){
ticker.attach(&send, 1); // Call function every second
}

This not:

Not working

...
x = TS_State.touchX[idx];
y = TS_State.touchY[idx];
        if (y > 100 && y < 150)
        {
            if(x > 100 && x < 330) // "Press me" Button
            {
                can2.frequency(500000);
                can2.write(CANMessage(1337, &npressed, 1));
            }  
...

Any idea how to fix it?

Best, ludgerf321

04 May 2017

Hello Ludger,
You don't show how npressed is declared but it seems that in the second case it isn't a char variable. Because the CANMessage constructor requires a const char* as second argument

CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard);

casting &npresseed to char* as follows

can2.write(CANMessage(1337, (const char*)&npressed, sizeof(npressed)));

or

can2.write(CANMessage(1337, (char*)&npressed, sizeof(npressed)));

should fix the issue.

NOTE: As an alternative to the built in CANMessage class, to simplify adding/getting data to/from a CAN message, I have published a library. If you are interested you can have look at this demo how it works.

05 May 2017

Hello Zoltan,

sure that was the point! Thanks a lot, also for your great library!

Best, Ludger