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

Revision:
0:3d11ed680b6a
Child:
1:34738eb16cf7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CANMsg.h	Fri Mar 17 16:34:42 2017 +0000
@@ -0,0 +1,77 @@
+#ifndef CANMESSAGE_H
+#define CANMESSAGE_H
+
+/* CAN message container.
+ * Provides "<<" (append) and ">>" (extract) operators to simplyfy
+ * adding/getting data items to/from a CAN message.
+ * Usage is similar to the C++ io-stream operators.
+ * Data length of CAN message is automatically updated when using "<<" or ">>" operators.
+ *
+ * See Wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> for demo.
+ */
+
+#include "CAN.h"
+
+class CANMsg : public CANMessage
+{
+public:
+    /** Creates empty CAN message.
+     */
+    CANMsg() :
+        CANMessage(){ }
+
+    /** Creates CAN message with specific content.
+     */
+    CANMsg(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) :
+        CANMessage(_id, _data, _len, _type, _format){ }
+
+    /** Creates CAN remote message.
+     */
+    CANMsg(int _id, CANFormat _format = CANStandard) :
+        CANMessage(_id, _format){ }
+
+    /** Clears CAN message content
+     */
+    void clear(void) {
+        len    = 0;
+        type   = CANData;
+        format = CANStandard;
+        id     = 0;
+        memset(data, 0, 8);
+    };
+
+    /** Append operator: Appends data (value) to CAN message
+     */
+    template<class T>
+    CANMsg &operator<<(const T val) {
+        if(len + sizeof(T) <= 8) {
+            *reinterpret_cast < T * > (&data[len]) = val;
+            len += sizeof(T);
+        }
+#if DEBUG
+        else {
+            printf("Error: Cannot append data - exceeding CAN data length!\r\n");
+        }
+#endif
+       return *this;
+    }
+
+    /** Extract operator: Extracts data (value) from CAN message
+     */
+    template<class T>
+    CANMsg &operator>>(T& val) {
+        if(sizeof(T) <= len) {
+            val = *reinterpret_cast < T * > (&data[0]);
+            len -= sizeof(T);
+            memcpy(data, data + sizeof(T), len);
+        }
+#if DEBUG
+        else {
+            printf("Error: Cannot extract data - exceeding CAN data length!\r\n");
+        }
+#endif
+        return *this;
+    }
+};
+
+#endif // CANMESSAGE_H