From 47bd350398607f06c093807288ffb8cf6cb7e05e Mon Sep 17 00:00:00 2001
From: Howard Li <bighorn@pursuitofchallenge.com>
Date: 周二, 28 4月 2020 13:49:19 +0800
Subject: [PATCH] Fix event commands adding semi-colons to output

---
 libscpi/inc/scpi/types.h   |    1 +
 libscpi/src/parser.c       |   14 +++++++++++---
 libscpi/test/test_parser.c |    3 +++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/libscpi/inc/scpi/types.h b/libscpi/inc/scpi/types.h
index ce238dc..98fde02 100644
--- a/libscpi/inc/scpi/types.h
+++ b/libscpi/inc/scpi/types.h
@@ -371,6 +371,7 @@
         scpi_interface_t * interface;
         int_fast16_t output_count;
         int_fast16_t input_count;
+        scpi_bool_t first_output;
         scpi_bool_t cmd_error;
         scpi_fifo_t error_queue;
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index 67b9bd5..fbf7b23 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -93,7 +93,7 @@
  * @return number of characters written
  */
 static size_t writeNewLine(scpi_t * context) {
-    if (context->output_count > 0) {
+    if (!context->first_output) {
         size_t len;
 #ifndef SCPI_LINE_ENDING
 #error no termination character defined
@@ -127,9 +127,12 @@
     const scpi_command_t * cmd = context->param_list.cmd;
     lex_state_t * state = &context->param_list.lex_state;
     scpi_bool_t result = TRUE;
+    scpi_bool_t is_query = context->param_list.cmd_raw.data[context->param_list.cmd_raw.length - 1] == '?';
 
-    /* conditionaly write ; */
-    writeSemicolon(context);
+    /* conditionally write ; */
+    if(!context->first_output && is_query) {
+        writeData(context, ";", 1);
+    }
 
     context->cmd_error = FALSE;
     context->output_count = 0;
@@ -146,6 +149,10 @@
         } else {
             if (context->cmd_error) {
                 result = FALSE;
+            } else {
+                if(context->first_output && is_query) {
+                    context->first_output = FALSE;
+                }
             }
         }
     }
@@ -197,6 +204,7 @@
 
     state = &context->parser_state;
     context->output_count = 0;
+    context->first_output = TRUE;
 
     while (1) {
         r = scpiParser_detectProgramMessageUnit(state, data, len);
diff --git a/libscpi/test/test_parser.c b/libscpi/test/test_parser.c
index fc33a74..204a334 100644
--- a/libscpi/test/test_parser.c
+++ b/libscpi/test/test_parser.c
@@ -257,6 +257,9 @@
     TEST_INPUT("*IDN?;*IDN?;*IDN?;*IDN?\r\n", "MA,IN,0,VER;MA,IN,0,VER;MA,IN,0,VER;MA,IN,0,VER\r\n");
     output_buffer_clear();
 
+    TEST_INPUT("*IDN?;STUB\r\n", "MA,IN,0,VER\r\n");
+    output_buffer_clear();
+
     TEST_INPUT("*IDN?;*OPC;*IDN?\r\n", "MA,IN,0,VER;MA,IN,0,VER\r\n");
     output_buffer_clear();
 

--
Gitblit v1.9.1