protected sealed override async Task ConnectAndGetValueImplAsync(CancellationToken cancel = default) { await Task.Yield(); ComPortClient?client = null; try { client = new ComPortClient(this.Settings.ComSettings); await using ShellClientSock sock = await client.ConnectAndGetSockAsync(cancel); await TaskUtil.DoAsyncWithTimeout(async (c) => { await Task.Yield(); await GetValueFromComPortImplAsync(sock, c); return(0); }, cancelProc : () => { sock._DisposeSafe(new OperationCanceledException()); }, cancel : cancel); } catch { await client._DisposeSafeAsync(); throw; } }
protected override async Task GetValueFromComPortImplAsync(ShellClientSock sock, CancellationToken cancel = default) { var reader = new BinaryLineReader(sock.Stream); while (true) { string?line = await reader.ReadSingleLineStringAsync(cancel : cancel); if (line == null) { break; } // ここで line には "0123.4A" のようなアンペア文字列が入っているはずである。 if (line.EndsWith("A")) { string numstr = line._SliceHead(line.Length - 1); if (double.TryParse(numstr, out double value)) { SensorData data = new SensorData(value.ToString("F2")); this.UpdateCurrentData(data); } } } }
public UnixShellProcessor(ShellClientSock sock, bool disposeObject = false, UnixShellProcessorSettings?settings = null) : base(sock, disposeObject, settings ?? new UnixShellProcessorSettings()) { try { } catch { this._DisposeSafe(); throw; } }
public ShellProcessor(ShellClientSock sock, bool disposeObject = false, ShellProcessorSettings?settings = null) { try { if (settings == null) { settings = new ShellProcessorSettings(); } this.Sock = sock; this.DisposeObject = disposeObject; this.Settings = settings; this.Reader = new BinaryLineReader(this.Sock.Stream); } catch { this._DisposeSafe(); throw; } }
protected abstract Task GetValueFromComPortImplAsync(ShellClientSock sock, CancellationToken cancel = default);