From a980ba5d7be6bf3af3d64bf8b2c919a5f7e21326 Mon Sep 17 00:00:00 2001 From: kutukvpavel Date: Wed, 6 Oct 2021 16:37:07 +0300 Subject: [PATCH] Add an option for silent \r\n vs \n EOR handling (permissive EOR). --- GPIBServer/GpibController.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/GPIBServer/GpibController.cs b/GPIBServer/GpibController.cs index b696c18..fbb2cac 100644 --- a/GPIBServer/GpibController.cs +++ b/GPIBServer/GpibController.cs @@ -31,6 +31,7 @@ public GpibController() public string Name { get; set; } = "Example"; public string EndOfReceive { get; set; } = "\r\n"; + public bool PermissiveEndOfReceive { get; set; } = true; public string EndOfSend { get; set; } = "\r\n"; public string AddressSelectCommandName { get; set; } = "select"; public string AddressQueryCommandName { get; set; } = "query"; @@ -38,7 +39,7 @@ public GpibController() public GpibInstrument[] InstrumentSet { get; set; } [JsonIgnore] - public string LastResponse { get; private set; } + public GpibResponseEventArgs LastResponse { get; private set; } [JsonIgnore] public SerialPortStream SerialPort { get; private set; } [JsonIgnore] @@ -57,6 +58,7 @@ public GpibController() public void Initialize() { _Instruments = InstrumentSet.ToDictionary(x => x.Name); + if (PermissiveEndOfReceive) _Trim = EndOfReceive.ToCharArray(); } public GpibInstrument GetInstrument(string name) @@ -146,7 +148,7 @@ public bool SelectInstrument(GpibInstrument instrument, CancellationToken? token if (!Send(selCmd)) return false; Wait(token); } - if (LastCommand.ExpectedResponse == null || LastResponse == LastCommand.ExpectedResponse) + if (LastCommand.ExpectedResponse == null || LastResponse.Response == LastCommand.ExpectedResponse) { LastInstrument = instrument; return true; @@ -189,6 +191,7 @@ public void Wait(CancellationToken? token) private readonly Timer _ResponseTimer; private readonly StringBuilder _ResponseBuilder; private Dictionary _Instruments; + private char[] _Trim; private bool SendReturnHelper() { @@ -227,16 +230,21 @@ private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs string newData = SerialPort.ReadExisting(); Task.Run(() => LogTerminal?.Invoke(this, newData)); _ResponseBuilder.Append(newData); - if (newData.EndsWith(EndOfReceive)) + if (PermissiveEndOfReceive ? + newData.EndsWith(EndOfReceive[EndOfReceive.Length - 1]) : + newData.EndsWith(EndOfReceive)) { _ResponseTimer.Stop(); - string resp = _ResponseBuilder.ToString() - .Remove(_ResponseBuilder.Length - EndOfReceive.Length, EndOfReceive.Length); + string resp = _ResponseBuilder.ToString(); + resp = PermissiveEndOfReceive ? + resp.TrimEnd(_Trim) : + resp.Remove(_ResponseBuilder.Length - EndOfReceive.Length, EndOfReceive.Length); _ResponseBuilder.Clear(); + if (LastCommand == null) return; resp = ParseResponse(resp); - LastResponse = resp; //TODO: RECHECK!! - Task.Run(() => ResponseReceived?.Invoke(this, - new GpibResponseEventArgs(LastCommand, LastInstrument, resp))); + LastResponse = new GpibResponseEventArgs(LastCommand, LastInstrument, resp); + LastCommand = null; //TODO: RECHECK!! + Task.Run(() => ResponseReceived?.Invoke(this, LastResponse)); } } }