private static async void ReaderTask(Win32Serial.Device device) { try { // // Continuously read from serial device // and send those values to edge hub. // // Create write overlapped structure for async operation var overlapped = Win32Serial.Device.CreateOverlapped(); int i = 0; uint numbytes; const uint size = MessageBody.SerialSize; var inbuf = new byte[size]; while (true) { // Clear input buffer Array.Clear(inbuf, 0, inbuf.Length); // Start Async Read, using overlapped structure device.Read(inbuf, size, out numbytes, ref overlapped); Log.WriteLineVerbose($"Async Read {i} Started"); // Block until Read finishes device.GetOverlappedResult(ref overlapped, out numbytes, true); var message = Encoding.ASCII.GetString(inbuf); Log.WriteLine($"Async Read {i} Completed. Received {numbytes} bytes: \"{message}\""); // Send it over Edge as a messagebody if (Options.UseEdge) { var tempData = new MessageBody(); tempData.SerialEncode = message; tempData.TimeCreated = DateTime.Now; if (tempData.isValid) { string dataBuffer = JsonConvert.SerializeObject(tempData); var eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer)); Log.WriteLine($"SendEvent: [{dataBuffer}]"); await ioTHubModuleClient.SendEventAsync("temperatureOutput", eventMessage); } else { Log.WriteLineError($"Invalid temp data"); } } i++; } } catch (Exception ex) { Log.WriteLineException(ex); } }
private static void TransmitTask(Win32Serial.Device device) { // Create write overlapped structure for async operation var overlapped = Win32Serial.Device.CreateOverlapped(); uint numbytes; int i = 1; const uint size = MessageBody.SerialSize; int limit = 15; if (Options.TestCount.HasValue) { limit = Options.TestCount.Value; } while (!Options.Test || i <= limit) { // Come up with a new message var tempData = new MessageBody { Machine = new Machine { Temperature = Rnd.NextDouble() * 100.0, Pressure = Rnd.NextDouble() * 100.0, }, Ambient = new Ambient { Temperature = Rnd.NextDouble() * 100.0, Humidity = Rnd.Next(24, 27) }, Number = i }; // Async write, using overlapped structure var message = tempData.SerialEncode; device.Write(Encoding.ASCII.GetBytes(message), size, out numbytes, ref overlapped); Log.WriteLineVerbose($"Write {i} Started"); // Block until write completes device.GetOverlappedResult(ref overlapped, out numbytes, true); Log.WriteLine($"Write {i} Completed. Wrote {numbytes} bytes: \"{message}\""); i++; Thread.Sleep(1000); } Environment.Exit(0); }
private static void TransmitTask(Win32Serial.Device device) { // Create write overlapped structure for async operation var overlapped = Win32Serial.Device.CreateOverlapped(); uint numbytes; int i = 1; const uint size = MessageBody.SerialSize; while (true) { Thread.Sleep(1000); // Come up with a new message var tempData = new MessageBody { Machine = new Machine { Temperature = Rnd.NextDouble() * 100.0, Pressure = Rnd.NextDouble() * 100.0, }, Ambient = new Ambient { Temperature = Rnd.NextDouble() * 100.0, Humidity = Rnd.Next(24, 27) }, Number = i }; // Async write, using overlapped structure var message = tempData.SerialEncode; device.Write(Encoding.ASCII.GetBytes(message), size, out numbytes, ref overlapped); Console.WriteLine($"{DateTime.Now.ToLocalTime()} Write {i} Started"); // Block until write completes device.GetOverlappedResult(ref overlapped, out numbytes, true); Console.WriteLine($"{DateTime.Now.ToLocalTime()} Write {i} Completed. Wrote {numbytes} bytes: \"{message}\""); i++; } }