mb_rtu.c
changeset 15 cadd89d14ca5
parent 0 ae252e0fd9b8
--- a/mb_rtu.c	Wed Jun 02 11:09:57 2021 +0100
+++ b/mb_rtu.c	Sun Jun 06 22:40:06 2021 +0100
@@ -1422,8 +1422,11 @@
 #endif
     /* set the data pointer */
   *recv_data_ptr = lb_data(&(buf->data_buf));
-    /* remove the frame bytes off the buffer */
-  lb_data_purge(&(buf->data_buf), frame_length);
+    /* Mark the frame bytes to be deleted off the buffer by the next call to lb_data_purge() */
+	/* Notice that we cannot delete the frame bytes right away, as they will still be accessed by whoever
+	 * called read_frame(). We can only delete this data on the next call to read_frame() 
+	 */
+  lb_data_mark_for_purge(&(buf->data_buf), frame_length);
     /* reset the search_history flag */
   buf->frame_search_history = 0;
     /* if the buffer becomes empty, then reset boundary flag */
@@ -1532,6 +1535,16 @@
   *recv_data_ptr = NULL;
 
   /*===================================*
+   * Delete any previously received data that has already been returned as a valid frame in *
+   *===================================*/  
+   /* Delete any previously received data that has already been returned as a valid frame in 
+    * the previous invocation of read_frame().
+	* Notice that the data that will be deleted hass ben marked for deletion by calling
+	* lb_data_mark_for_purge() in return_frame()
+	*/
+  lb_data_purge(&(recv_buf->data_buf), 0);
+
+  /*===================================*
    * Check for frame in left over data *
    *===================================*/
   /* If we have any data left over from previous call to read_frame()