Help with CAN bus (Start bit, length?)

Hi there,

I've got a nice CAN bus board set up using a Teensy 3.2 which I've used successfully to interrogate an OBD port of my car using the Collin80 fork of FlexCAN. No issues with it, and my typical message forming code looks like this (for OBD use):

void CAN_Request(byte CAN_MODE, byte PID_CALL) {
  can_MsgTx.ext = 0;
  can_MsgTx.id = PID_REQUEST;
  can_MsgTx.len = 8;

  can_MsgTx.buf[0] = 0x02; //Length in number of bytes of remaining data (2 bytes = 02, 3 bytes = 03 etc)
  can_MsgTx.buf[1] = CAN_MODE;
  can_MsgTx.buf[2] = PID_CALL;
  can_MsgTx.buf[3] = 0;
  can_MsgTx.buf[4] = 0;
  can_MsgTx.buf[5] = 0;
  can_MsgTx.buf[6] = 0;
  can_MsgTx.buf[7] = 0;

  can_MsgTx.flags.extended = 0;
  can_MsgTx.flags.remote = 0;

  //Serial.print("CAN SENT: "); hexDump(8, can_MsgTx.buf); //Diagnostic - Dump message to Serial//@@@@@@@@@@@@@@@@@@@@@@@@@@@CAN DIAGNOSTICS@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  //Serial.println(currentMillis / 10);
  Can0.write(can_MsgTx); //Send message
}

My next challenge is that I want to try to use this to communicate with a Bosch wiper motor, for which I have the CAN message table, but I'm not entirely sure how to use it. I have a number of functions listed. I'll list three of them below as examples:

  1. Teach Mode:
    ID = 0x6000
    Data = 1 or 0
    Start bit = 14
    Length = 1
    Byte = 2

  2. Torque Limit:
    ID = 0x6006
    Data = 00 or 01 or 10 or 11
    Start bit = 54
    Length = 2
    Byte = 7

  3. Wipe Command:
    ID: 0x6000
    Data: 0, 1, 2, 3, 4, 5, 6 or 7
    Start bit: 8
    Length = 3
    Byte = 2

So to translate this into a format that my CAN code can interpret, do I need to convert this into Hex or can I input directly in binary?

As I understand it, for the first example, I would need to set it up like this:

ID = 0x6000,
Length = 1
Buf [0] = 00000010 = 0x02???

For example 2
ID = 0x6006
Length = 7
Buf[0] = 0
Buf[1] = 0
Buf[2] = 0
Buf[3] = 0
Buf[4] = 0
Buf[5] = 0
Buf[6] = 00000011 (for 11) = 0x03??

For example 3
ID = 0x6000
Length = 2
Buf[0] = 0
Buf[1] = 01100000 (for 6) = 0x60???

Does this look right to you guys? I've not seen message instructions written like this before so I'm a bit unsure... I'll hopefully have a unit to test this on in a couple of weeks but I want to try to make sure I have the correct message construction before attempting to communicate

They would be the same, just the way you express them changes. Binary and Hexadecimal number systems are examples of positional number systems with different bases. Binary number systems use a base of two while hexadecimal uses a base of 16. In a hexadecimal system, it is necessary to count to 15. To represent the numbers 10 – 15, the letters A – F are used respectively. In base -10 , each digit of a number can have an integer value ranging from 0 to 9 (10 possibilities) depending on its position. The places or positions of the numbers are based on powers of 10. Each number position is 10 times the value to the right of it, hence the term base - 10. You can generally use what you want, the important thing is to let the compiler know what you are telling it.

1 Like

Thanks for confirming, I thought that was the case but wasn't sure. I guess the last question is how do I let the compiler know when I'm using hex or binary? I usually use #define to pre-label the various CAN messages I want to send, and currently I'm using hex messages and at no point am I telling the compiler that they're hex explicitly.

A quick Google suggests that I could write for example:

Buf[1] = B01100000;

or

#define CAN_Message B01100000

Buf[1] = CAN_Message;

Does this look right?

You might not know but you did in your example in your first post.

The most common and easiest to notice notations for hexadecimal and binary are the following:

x = 0x2;  // hexadecimal notation
b = 0b10; // binary notation

// or with leading zeros e.g., to show a complete bit pattern

x = 0x02;       // hexadecimal notation
b = 0b00000010; // binary notation

The compiler will accept other variants as well. e.g., 0X.. , B010, ...

1 Like

Brilliant, I get it now... Man there sure is a lot to learn about this stuff but every day's a school day! Thanks very much both :slight_smile:

Try this link it should help: Integer Constants - Arduino Reference

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.