public async Task Send(SerialMessage message) { if (!IsConnected) { throw new RovNotConnectedException(); } await OpenConnection.Send(message); }
public static bool TryParse(string source, out SerialMessage message) { if (!source.StartsWith("!") || source.Length <= 1 || source.Contains("\n")) { message = new SerialMessage { Type = null, Parameters = null }; return(false); } var allParts = source.Split(" "); message = new SerialMessage { Type = allParts.First().Substring(1), Parameters = allParts.Skip(1).ToArray() }; return(true); }
public async Task Send(SerialMessage message) { if (Writer == null) { throw new InvalidOperationException("An attempt was made to send a message, but there is no underlying data writer open."); } Writer.WriteString(message.Serialize() + "\n"); try { await Writer.StoreAsync(); } catch (Exception e) when( // The semaphore timeout period has expired. (Exception from HRESULT: 0x80070079) e.HResult == unchecked ((int)0x80070079) // The device does not recognize the command. (Exception from HRESULT: 0x80070016) || e.HResult == unchecked ((int)0x80070016) // A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F) || e.HResult == unchecked ((int)0x8007001F) ) { throw new RovSendOperationFailedException(e); } }
public async void Open() { if (ReadTask != null) { throw new InvalidOperationException("An attempt was made to open the connection, but there is already an open connection."); } ConnectedDevice = await SerialDevice.FromIdAsync(ConnectedDeviceId); ConnectedDevice.BaudRate = 115200; ConnectedDevice.StopBits = SerialStopBitCount.One; ConnectedDevice.DataBits = 8; ConnectedDevice.Parity = SerialParity.None; ConnectedDevice.Handshake = SerialHandshake.None; ConnectedDevice.WriteTimeout = TimeSpan.FromMilliseconds(10); ConnectedDevice.ReadTimeout = TimeSpan.FromMilliseconds(10); Writer = new DataWriter(ConnectedDevice.OutputStream); ReadTaskCancellationToken = new CancellationTokenSource(); ReadTask = Task.Run(async() => { DataReader reader = new DataReader(ConnectedDevice.InputStream); StringBuilder builder = new StringBuilder(); while (true) { try { // TODO: investigate larger chunks // TODO: Can larger code points cause issues? var loadTask = reader.LoadAsync(1); var loadResult = await loadTask.AsTask(ReadTaskCancellationToken.Token); if (loadResult < 1) { continue; } if (loadResult != 1 || reader.UnconsumedBufferLength != 1) { Debugger.Break(); } char nextChar = reader.ReadString(1)[0]; if (nextChar == '\n') { string line = builder.ToString(); if (SerialMessage.TryParse(line, out SerialMessage message)) { try { OnMessageReceived(message); } catch (Exception e) { Debug.WriteLine(e.ToString()); Debugger.Break(); } } else { try { OnRawStringReceived(line); } catch (Exception e) { Debug.WriteLine(e.ToString()); Debugger.Break(); } } builder.Clear(); } else { builder.Append(nextChar); } } // The operation attempted to access data outside the valid range catch (System.Runtime.InteropServices.COMException e) when(e.HResult == -2147483637) { // TODO } } }); IsOpen = true; }