aa

Revision:
4:5017a65ff423
Parent:
3:4e42fdc0459f
Child:
5:4f03cb1e882b
--- a/CANMsg.h	Wed Apr 25 11:47:00 2018 +0000
+++ b/CANMsg.h	Tue Feb 05 14:50:19 2019 +0000
@@ -12,7 +12,7 @@
 
 #include "CAN.h"
 
-class CANMsg : public CANMessage
+class CANMsg : public mbed::CANMessage
 {
 public:
     /** Creates empty CAN message.
@@ -45,7 +45,6 @@
     template<class T>
     CANMsg &operator<<(const T val) {
         MBED_ASSERT(len + sizeof(T) <= 8);
-        //*reinterpret_cast<T*>(&data[len]) = val; // This used to work but now it hangs at run time.
         memcpy(&data[len], &val, sizeof(T));
         len += sizeof(T);
         return *this;
@@ -56,8 +55,14 @@
     template<class T>
     CANMsg &operator>>(T& val) {
         MBED_ASSERT(sizeof(T) <= len);
-        val = *reinterpret_cast<T*>(&data[0]);
-        len -= sizeof(T);
+        if (sizeof(T) > len) {
+            memcpy(&val, data, len);
+            len = 0;
+        }
+        else {
+            memcpy(&val, data, sizeof(T));
+            len -= sizeof(T);
+        }
         memcpy(data, data + sizeof(T), len);
         return *this;
     }