0

I am creating a software that has to ask for data in a unit with a CAN network. For some reason I am only getting a part of the data being send when asked for. The unit has a CAN frequency of a 100 kbit or a 100000 bits as shown in the code. I am using a Nucleo-F767ZI and to make a connection to the CAN network I am using the build in function of the board.

I already looked if there was a fault with the data being send, but this seems fine since a different program is able to read it without fault. The code I currently use to test is this bit.

#include "mbed.h"

CAN can1(PD_0, PD_1);//Sets the pins for the CAN.
Serial pc(USBTX, USBRX);//Selects the type of serial and assigns a name to it.
char buffer[300];

int main (){
    pc.baud(9600);//sets serialportbaud to 9600
    CANMessage test;
    can1.frequency(100000); //Sets frequency speed. it has to be 100000 otherwise the unit wont respond
    test.format = CANStandard; //Selects the format for the can message.
    test.id = 24; //Gives the can message an ID.
    test.len = 2; //How long the message is in bytes.
    test.data[0] = 0; //Select the data for this byte. 
    test.data[1] = 3;
    can1.write(test); //this sends the data of 0 , 3 with the id of 24 and gets data from the unit its being send to.

    printf("\n\rsended \n\r"); //confirmation that it has come this far without crashing.

    while(true){
        can1.read(receive);//receives any message send over the network except his own.
        sprintf(buffer, "%d/%d/%d/%d/%d/%d/%d/%d", receive.data[0], receive.data[1], receive.data[2], receive.data[3], receive.data[4], receive.data[5], receive.data[6], receive.data[7]);//stores data in buffer
        printf("%s \n\r", buffer);//shows what the data was
    }
}
Sam Hendriks
  • 35
  • 10
  • 1
    Why are you using decimal format for CAN identifiers? That's code smell right there, they are always listed in hex. Another thing worth mentioning is that 100kbps isn't one of the standardized baudrates, but a somewhat common non-standard extension. Are you sure all devices involved can handle it? And of course the key to solving this is to watch the actual bus with your CAN listener. You cannot develop CAN applications without one, period. – Lundin Jan 23 '19 at 11:51
  • 1
    (Btw questions like this are likely to get better answers over at https://electronics.stackexchange.com/ where most of the CAN bus gurus lurk. It is perfectly fine to ask software questions there, long as they are about microcontroller firmware or otherwise hardware-related) – Lundin Jan 23 '19 at 11:53

1 Answers1

0

I fixed it by increasing the serial baudrate it appearantly was to slow to print te data fast enough before it was already changed.

Sam Hendriks
  • 35
  • 10