0

I am developing a code in which I need to print value of structure pointer on serial terminal. Below is my code snippet:

typedef struct
{
  uint8_t AB;     ///< Address byte (Start byte)
  uint8_t CB;     ///< Command (index) "ASCII-byte"
  uint8_t SI;     ///< Action (sub index) "ASCII-byte"
  uint8_t PH_1;    ///< Payload "ASCII-byte" of the high nibble of the high byte of the raw payload
  uint8_t PH_2;    ///< Payload "ASCII-byte" of the low nibble of the high byte of the raw payload
  uint8_t PL_1;    ///< Payload "ASCII-byte" of the high nibble of the low byte of the raw payload
  uint8_t PL_2;    ///< Payload "ASCII-byte" of the low nibble of the low byte of the raw payload
  uint8_t CS1;    ///< Checksum "ASCII-byte" of the high nibble of the raw checksum
  uint8_t CS0;    ///< Checksum "ASCII-byte" of the low nibble of the raw checksum
} rs485_message_t;

int proto485ComposeMsg(rs485_message_t* msg, uint8_t address, uint8_t command, uint8_t action, uint16_t value){
  msg->AB = address;   
  msg->CB = command;
  msg->SI = action;
  msg->PH_1 = (int)((value & 0xF000) >> 12) ; 
  msg->PH_2 = (int)((value & 0x0F00) >> 8); 
  msg->PL_1 = (int)((value & 0x00F0) >> 4) ; 
  msg->PL_2 = (int)(value & 0x000F);     
  int checksumRaw = (msg->CB) + (msg->SI) + (msg->PH_1) + (msg->PH_2) + (msg->PL_1) + (msg->PL_2);
  setCheckSum(msg);
}

uint8_t proto485Write(uint8_t address, uint8_t command, uint8_t action, uint16_t value)
{
  int i = 0;     
  int stringOne[9] = {int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00), int(0x00)}; ;
  rs485_message_t rs485Msg;
  proto485ComposeMsg(&rs485Msg, address, command, action, value); // value is passed in "raw" format
  Serial.write(rs485Msg);       // getting error on this line
}

Please help... Thank you

2

You can't just print a structure. After all, how is the program supposed to know how it is supposed to be displayed?

Do you want just the raw data, or do you want it formatted in a human readable form?

For just the raw data you need to cast the pointer to a type that can be printed as an array. For example:

Serial.write((uint8_t *)&rs485Msg, sizeof(struct rs485_message_t));

To print it in a human readable form you will need to take each value that you want to see and print it separately with any data manipulation that you may want. For example:

Serial.print("Checksum 0: ");
Serial.println(rs485Msg.CS0, HEX);

Another thing you can do is to change your structure into a class and inherit the Printable class. You can then implement the size_t printTo(Print &p); member function which sends the data to the p object for printing in the format you want. Then you can just print it directly as you are trying to do in your code.

| improve this answer | |
  • Thank you so much it solved my purpose – swanand Mar 29 '19 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.