/// <summary> /// The on threshold read. /// </summary> /// <param name="e"> /// The e. /// </param> protected virtual void OnThresholdRead(SerialDataReceivedEvenArgs e) { var handler = this.DataReceived; if (handler != null) { handler(this, e); } }
private void port_DataReceived(object sender, SerialDataReceivedEvenArgs e) { if (e.StatusFlag == -1) { var port = SerialPortList.FirstOrDefault(x => x.Key == ((SerialPort)sender).SeriaPortName).Value; if (port == null) return; var message = string.Format(CultureInfo.CurrentCulture, "Port: {0} was closed.", port.SeriaPortName); WriteLog(message); Notify(message); port.Close(); } ThreadPool.QueueUserWorkItem(callback => Deployment.Current.Dispatcher.BeginInvoke( () => { foreach (var item in _collectionOfInstance.Where(x => x.Value.Contains("COM"))) { if (item.Value != e.PortName) continue; var instance = this.InstanceDataVariable(ProcessName, item.Key + CalculatedVariable, e.ReceivedData); var methodInfo = instance.GetType().GetMethod(CalculateSecondExpression); if (methodInfo != null) { methodInfo.Invoke(instance, new object[] { EditableRoot }); } } })); }
private void StartListner() { var bytes = new byte[LengthDataRx]; var data = Read(bytes); if (data == -1) { CloseHandle(pHandle); pHandle = IntPtr.Zero; OnThresholdRead(new SerialDataReceivedEvenArgs { StatusFlag = -1 }); } if (data == 0) { StartListner(); } var recentByte = new byte[data]; Array.Copy(bytes, recentByte, data); var args = new SerialDataReceivedEvenArgs { SerialData = SerialData.Chars, Buffer = recentByte, ReceivedData = Encoding.UTF8.GetString(recentByte, 0, recentByte.Length), PortName = SeriaPortName }; OnThresholdRead(args); if (cts.Token.IsCancellationRequested) { CloseHandle(pHandle); pHandle = IntPtr.Zero; } else { StartListner(); } }