How do I send these 2 data frames to a CAN bus?

Dsolberg8132

Active member
I am scaling back the use cases for my CAN bus interface to a Huawei power supply so that I no longer will be reading and decoding the CAN bus data frames but just sending data frames.

I have been sending 0x108040FE 00 00 00 00 00 00 00 00 which was the data frame to instruct the power supply to respond with data values but now I have to send the following:

0x108180FE 01 00 00 00 00 A5 5A

When I used the data message format that I used with the request data frame message I get the following error Compilation error: unable to find numeric literal operator 'operator""A' This is the code from this function

Code:
void SetVolt41 (){
  CAN_message_t msg;
    msg.flags.extended = 1;  //sets extended frames
    msg.len = 8;
    msg.id = 0x108180FE;
    msg.buf[0] = 00;
    msg.buf[1] = 00;
    msg.buf[2] = 00;
    msg.buf[3] = 00;
    msg.buf[4] = 00;
    msg.buf[5] = 00;
    msg.buf[6] = A5;
    msg.buf[7] = 5A;
 
  //Set the on-line output voltage to 41.5V
   if (myCan.write(msg)) {
    Serial.print("Message sent on CAN3=");
    Serial.println (msg.id, HEX);
    Serial.print(" Data=");
    for (int i = 0; i < msg.len; i++) {
      Serial.println(msg.buf[i], HEX);
      Serial.print(" ");
      } 
    }
  
  }

Any help would be greatly appreciated.

Don
 
. . . but now I have to send the following:

0x108180FE 01 00 00 00 00 A5 5A

msg.flags.extended = 1; //sets extended frames
msg.len = 7;
msg.id = 0x108180FE;
msg.buf[0] = 0x01;
msg.buf[1] = 0x00;
msg.buf[2] = 0x00;
msg.buf[3] = 0x00;
msg.buf[4] = 0x00;
msg.buf[5] = 0xA5;
msg.buf[6] = 0x5A;

Taking the indicagted format of the new desired message to be sent, the contents should look as shown above ("0x" preceding a number indicates that it is in HEX format . . . the "00" & "01" don't absolutely need the "0x" prefix, but it is probably better to always use the prefix to avoid any ambiguity, e.g. the difference between 10 [ten decimal] & 0x10 [one-zero hex, which is equal to sixteen decimal]). Note particularly that the updated frame length is specified as "7" & that the value of msg.buf[7] is insignificant, as it is not used when sending this frame.

Hope that helps . . .

Mark J Culross
KD5RXT
 
Thanks again Mark for your help. Hex is a new to me.

Don K9AQ
Hex is often used when dealing with hardware or fields that are a fixed size because each hex character represents 4 bits of binary. One byte will always be two hex characters. This fixed relationship makes calculating the number of binary bits for a number and converting between hex and binary far simpler. This is very handy when debugging code that uses bitmasks or registers with lots of flags.

Back in the early days of programming there was a simple convention in order to make life simpler for the compiler, if it started with a letter is was a variable, if it started with a digit between 0 and 9 it was a constant number. Those rules still apply, variable names can't start with numbers and all numbers must start with a digit, even if they are in hex. To get around this the convention was that hexadecimal numbers always start with 0x.

One other thing that can catch people out, an integer number starting with a 0 that isn't followed by an x is taken as being in octal (base 8)
This means that 010 = 8.
Octal is very rarely used, realistically so all you have to remember is to never put leading 0's on the front of integer values.

You could avoid using hex and instead specify all the values in decimal:
Code:
    msg.id = 276922622; // = 0x108180FE;
    msg.buf[6] = 165; // = 0xA5;
    msg.buf[7] = 90; // = 0x5A;
but this may then make it harder to relate the code back to the documentation.
 
Back
Top