Libraries to support working with GMLAN - General Motors CAN BUS network in most of their vehicles between 2007-present day. Please note this is a work in progress and not guaranteed to be correct, use at your own risk! Read commit logs / subscribe to see what has been added, it's a work in progress after all ;)

Revision:
7:5ec65e6e8095
Parent:
6:32592425aa57
Child:
8:bc97fa5d306e
--- a/GMLAN.cpp	Wed Apr 03 16:09:10 2013 +0000
+++ b/GMLAN.cpp	Sun Apr 07 18:20:01 2013 +0000
@@ -77,9 +77,10 @@
         return CANMessage(arbitration, datatochars, data.size(), CANData, CANStandard);
 }
 
-GMLAN_11Bit_Request::GMLAN_11Bit_Request(int _id, vector<char> _request) {
+GMLAN_11Bit_Request::GMLAN_11Bit_Request(int _id, vector<char> _request, bool _await_response) {
     id = _id;
     request_data = _request;
+    await_response = _await_response;
     tx_bytes = rx_bytes = 0;
     tx_frame_counter = rx_frame_counter = 1;
     const char _fp [8] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
@@ -117,7 +118,10 @@
         if (tx_frame_counter > 0xF) tx_frame_counter = 0x0;
     }
     
-    if (tx_bytes >= request_data.size()) request_state = GMLAN_STATE_AWAITING_REPLY;
+    if (tx_bytes >= request_data.size()) {
+        if (await_response == true) request_state = GMLAN_STATE_AWAITING_REPLY;
+        else request_state = GMLAN_STATE_COMPLETED;
+    }
     
     return CANMessage(id, datatochars, 8, CANData, CANStandard);
 }
@@ -134,7 +138,7 @@
         
         if (((datatochars[0] >> 4) & 0xF) == GMLAN_PCI_UNSEGMENTED) {
             // Unsegmented frame
-            rx_bytes = datatochars[0] & 0xF;
+            rx_bytes = (datatochars[0] & 0xF);
             if (datatochars[1] == GMLAN_SID_ERROR) {
                 // Error frame
                 if ((rx_bytes == 3) && (datatochars[3] == 0x78)) return; // "Still processing request" message, ignore this one
@@ -143,7 +147,7 @@
             for (int i = 1; i < (rx_bytes+1); i++) response_data.push_back(datatochars[i]);
         } else if (((datatochars[0] >> 4) & 0xF) == GMLAN_PCI_SEGMENTED) {
             // First segmented frame
-            rx_bytes = datatochars[0] & 0xF;
+            rx_bytes = (datatochars[0] & 0xF);
             rx_bytes = (rx_bytes << 8) | datatochars[1];
             for (int i = 2; i < 8; i++) {
                 if ((i - 2) >= rx_bytes) {
@@ -158,12 +162,16 @@
             // Additional segmented frame
             // TODO check for frame order
             for (int i = 1; i < 8; i++) {
-                if (response_data.size() + 1 >= rx_bytes) {
+                if (response_data.size() >= rx_bytes) {
                     request_state = GMLAN_STATE_COMPLETED;
                     return;
                 }
                 response_data.push_back(datatochars[i]);
             }
+            if (response_data.size() >= rx_bytes) {
+                request_state = GMLAN_STATE_COMPLETED;
+                return;
+            }
         } else if (((datatochars[0] >> 4) & 0xF) == GMLAN_PCI_FLOW_CONTROL) {
             // Flow control frame
             request_state = GMLAN_STATE_SEND_DATA;