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:
Tue Feb 05 14:50:19 2019 +0000
Revision:
4:5017a65ff423
Parent:
3:4e42fdc0459f
Modified.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 1:34738eb16cf7 1 #ifndef CANMSG_H
hudakz 1:34738eb16cf7 2 #define CANMSG_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 4:5017a65ff423 15 class CANMsg : public mbed::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 2:2b8425b12d05 47 MBED_ASSERT(len + sizeof(T) <= 8);
hudakz 3:4e42fdc0459f 48 memcpy(&data[len], &val, sizeof(T));
hudakz 2:2b8425b12d05 49 len += sizeof(T);
hudakz 2:2b8425b12d05 50 return *this;
hudakz 0:3d11ed680b6a 51 }
hudakz 0:3d11ed680b6a 52
hudakz 0:3d11ed680b6a 53 /** Extract operator: Extracts data (value) from CAN message
hudakz 0:3d11ed680b6a 54 */
hudakz 0:3d11ed680b6a 55 template<class T>
hudakz 0:3d11ed680b6a 56 CANMsg &operator>>(T& val) {
hudakz 2:2b8425b12d05 57 MBED_ASSERT(sizeof(T) <= len);
hudakz 4:5017a65ff423 58 if (sizeof(T) > len) {
hudakz 4:5017a65ff423 59 memcpy(&val, data, len);
hudakz 4:5017a65ff423 60 len = 0;
hudakz 4:5017a65ff423 61 }
hudakz 4:5017a65ff423 62 else {
hudakz 4:5017a65ff423 63 memcpy(&val, data, sizeof(T));
hudakz 4:5017a65ff423 64 len -= sizeof(T);
hudakz 4:5017a65ff423 65 }
hudakz 2:2b8425b12d05 66 memcpy(data, data + sizeof(T), len);
hudakz 0:3d11ed680b6a 67 return *this;
hudakz 0:3d11ed680b6a 68 }
hudakz 0:3d11ed680b6a 69 };
hudakz 0:3d11ed680b6a 70
hudakz 1:34738eb16cf7 71 #endif // CANMSG_H