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); } } } }
async Task EasyRecvDataOneLineRecvCallbackAsync(PipeStream st) { bool returnedFalseOnce = false; var r = new BinaryLineReader(st); while (true) { List <Tuple <Memory <byte>, bool> >?lines = await r.ReadLinesWithTimeoutAsync(1000); if (lines == null) { break; } foreach (var line in lines) { string lineDecoded = line.Item1._GetString(this.OutputEncoding); if (returnedFalseOnce == false) { if (this.Options.EasyOneLineRecvCallbackAsync != null) { try { bool ret = await this.Options.EasyOneLineRecvCallbackAsync(lineDecoded); if (ret == false) { returnedFalseOnce = true; } } catch (Exception ex) { ex._Debug(); returnedFalseOnce = true; } if (returnedFalseOnce) { TaskUtil.StartAsyncTaskAsync(async() => { await Task.Yield(); KillProcessInternal(); await Task.CompletedTask; })._LaissezFaire(true); } } } } } }
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; } }
public static void CommitTempFile(FileStream fs, string path) { // first strip the headers fs.Seek(0, SeekOrigin.Begin); // lets use a BinaryReader with special line handling support BinaryLineReader sr = new BinaryLineReader(fs); // loops until we get a blank line while (sr.ReadLine().Trim() != "") { ; } // our file cursor is now at the top of the video file, lets start copying. // first delete tempfile var tempFile = path + ".stmp"; if (File.Exists(tempFile)) { File.Delete(tempFile); } // lets copy byte[] buffer = new byte[1024 * 32]; //32k buffer using (FileStream fw = new FileStream(tempFile, FileMode.CreateNew)) { //make sure we stil read from the BinaryReader sr //as they will still be data in it's buffers var bytesRead = sr.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { fw.Write(buffer, 0, bytesRead); bytesRead = sr.Read(buffer, 0, buffer.Length); } } // ok, everything copied OK, lets rename if (File.Exists(path)) { File.Delete(path); } File.Move(tempFile, path); }
async Task EasyPrintRealTimeRecvDataPrintCallbackAsync(PipeStream st) { var r = new BinaryLineReader(st); while (true) { List <Tuple <Memory <byte>, bool> >?lines = await r.ReadLinesWithTimeoutAsync(1000); if (lines == null) { break; } foreach (var line in lines) { string tagStr = ""; if (this.Options.PrintTag._IsFilled()) { tagStr = $"{this.Options.PrintTag}: "; } string lineDecoded = line.Item1._GetString(this.OutputEncoding); string str = tagStr + lineDecoded; if (line.Item2 == false) { str += " [...!!pending!!...]"; } using (await WriteLineLocker.LockWithAwait()) { Con.WriteLine(str); } } } }