CAN message container. Defines "<<" and ">>" operators to simplify adding/getting data to/from a CAN message.

Dependents:   CAN_Hello EinlesenSensorabstand_digital_2 CAN_STABLE_EINSTEIN CAN_ex_STM32F103C8T6 ... more

CAN message container

In order to simplify adding/getting data to/from a CAN message the CANMsg class extends mbed's built-in CANMessage class by defining "<<" (append) and ">>" (extract) operators. Since it inherits everything from CANMessage, after importing the library into your project, you can use it instead of CANMessage without any additional modification or limitations. The usage of "<<" and ">>" operators is similar to the C++ io-streams operators.

  • Before adding data to a CANMsg object it is recommended to clear it by calling its clear() member function.
  • Then set up all the other properties (ID, type, format) as needed.
  • Finally append data one by one or combine them into a stream. The actual data length of a CAN message is automatically updated when using "<<" or ">>" operators. However, you have to make sure that the total length of data does not exceed eight bytes.

For an example of use have a look at this wiki page.

Import programCAN_Hello

Using CAN bus with (not just NUCLEO) mbed boards

Committer:
hudakz
Date:
Fri Mar 17 16:34:42 2017 +0000
Revision:
0:3d11ed680b6a
Child:
1:34738eb16cf7
Initial release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:3d11ed680b6a 1 #ifndef CANMESSAGE_H
hudakz 0:3d11ed680b6a 2 #define CANMESSAGE_H
hudakz 0:3d11ed680b6a 3
hudakz 0:3d11ed680b6a 4 /* CAN message container.
hudakz 0:3d11ed680b6a 5 * Provides "<<" (append) and ">>" (extract) operators to simplyfy
hudakz 0:3d11ed680b6a 6 * adding/getting data items to/from a CAN message.
hudakz 0:3d11ed680b6a 7 * Usage is similar to the C++ io-stream operators.
hudakz 0:3d11ed680b6a 8 * Data length of CAN message is automatically updated when using "<<" or ">>" operators.
hudakz 0:3d11ed680b6a 9 *
hudakz 0:3d11ed680b6a 10 * See Wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> for demo.
hudakz 0:3d11ed680b6a 11 */
hudakz 0:3d11ed680b6a 12
hudakz 0:3d11ed680b6a 13 #include "CAN.h"
hudakz 0:3d11ed680b6a 14
hudakz 0:3d11ed680b6a 15 class CANMsg : public CANMessage
hudakz 0:3d11ed680b6a 16 {
hudakz 0:3d11ed680b6a 17 public:
hudakz 0:3d11ed680b6a 18 /** Creates empty CAN message.
hudakz 0:3d11ed680b6a 19 */
hudakz 0:3d11ed680b6a 20 CANMsg() :
hudakz 0:3d11ed680b6a 21 CANMessage(){ }
hudakz 0:3d11ed680b6a 22
hudakz 0:3d11ed680b6a 23 /** Creates CAN message with specific content.
hudakz 0:3d11ed680b6a 24 */
hudakz 0:3d11ed680b6a 25 CANMsg(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) :
hudakz 0:3d11ed680b6a 26 CANMessage(_id, _data, _len, _type, _format){ }
hudakz 0:3d11ed680b6a 27
hudakz 0:3d11ed680b6a 28 /** Creates CAN remote message.
hudakz 0:3d11ed680b6a 29 */
hudakz 0:3d11ed680b6a 30 CANMsg(int _id, CANFormat _format = CANStandard) :
hudakz 0:3d11ed680b6a 31 CANMessage(_id, _format){ }
hudakz 0:3d11ed680b6a 32
hudakz 0:3d11ed680b6a 33 /** Clears CAN message content
hudakz 0:3d11ed680b6a 34 */
hudakz 0:3d11ed680b6a 35 void clear(void) {
hudakz 0:3d11ed680b6a 36 len = 0;
hudakz 0:3d11ed680b6a 37 type = CANData;
hudakz 0:3d11ed680b6a 38 format = CANStandard;
hudakz 0:3d11ed680b6a 39 id = 0;
hudakz 0:3d11ed680b6a 40 memset(data, 0, 8);
hudakz 0:3d11ed680b6a 41 };
hudakz 0:3d11ed680b6a 42
hudakz 0:3d11ed680b6a 43 /** Append operator: Appends data (value) to CAN message
hudakz 0:3d11ed680b6a 44 */
hudakz 0:3d11ed680b6a 45 template<class T>
hudakz 0:3d11ed680b6a 46 CANMsg &operator<<(const T val) {
hudakz 0:3d11ed680b6a 47 if(len + sizeof(T) <= 8) {
hudakz 0:3d11ed680b6a 48 *reinterpret_cast < T * > (&data[len]) = val;
hudakz 0:3d11ed680b6a 49 len += sizeof(T);
hudakz 0:3d11ed680b6a 50 }
hudakz 0:3d11ed680b6a 51 #if DEBUG
hudakz 0:3d11ed680b6a 52 else {
hudakz 0:3d11ed680b6a 53 printf("Error: Cannot append data - exceeding CAN data length!\r\n");
hudakz 0:3d11ed680b6a 54 }
hudakz 0:3d11ed680b6a 55 #endif
hudakz 0:3d11ed680b6a 56 return *this;
hudakz 0:3d11ed680b6a 57 }
hudakz 0:3d11ed680b6a 58
hudakz 0:3d11ed680b6a 59 /** Extract operator: Extracts data (value) from CAN message
hudakz 0:3d11ed680b6a 60 */
hudakz 0:3d11ed680b6a 61 template<class T>
hudakz 0:3d11ed680b6a 62 CANMsg &operator>>(T& val) {
hudakz 0:3d11ed680b6a 63 if(sizeof(T) <= len) {
hudakz 0:3d11ed680b6a 64 val = *reinterpret_cast < T * > (&data[0]);
hudakz 0:3d11ed680b6a 65 len -= sizeof(T);
hudakz 0:3d11ed680b6a 66 memcpy(data, data + sizeof(T), len);
hudakz 0:3d11ed680b6a 67 }
hudakz 0:3d11ed680b6a 68 #if DEBUG
hudakz 0:3d11ed680b6a 69 else {
hudakz 0:3d11ed680b6a 70 printf("Error: Cannot extract data - exceeding CAN data length!\r\n");
hudakz 0:3d11ed680b6a 71 }
hudakz 0:3d11ed680b6a 72 #endif
hudakz 0:3d11ed680b6a 73 return *this;
hudakz 0:3d11ed680b6a 74 }
hudakz 0:3d11ed680b6a 75 };
hudakz 0:3d11ed680b6a 76
hudakz 0:3d11ed680b6a 77 #endif // CANMESSAGE_H