Skip to content

Commit

Permalink
Add an option for silent \r\n vs \n EOR handling (permissive EOR).
Browse files Browse the repository at this point in the history
  • Loading branch information
kutukvpavel authored and kutukvpavel committed Oct 6, 2021
1 parent 36649b0 commit a980ba5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions GPIBServer/GpibController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ 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";
public SerialPortConfiguration PortConfiguration { get; set; } = new SerialPortConfiguration();
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]
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -189,6 +191,7 @@ public void Wait(CancellationToken? token)
private readonly Timer _ResponseTimer;
private readonly StringBuilder _ResponseBuilder;
private Dictionary<string, GpibInstrument> _Instruments;
private char[] _Trim;

private bool SendReturnHelper()
{
Expand Down Expand Up @@ -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));
}
}
}
Expand Down

0 comments on commit a980ba5

Please sign in to comment.