Пример #1
0
        private void PortReceiveData(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] receiveData = new byte[serialPort.BytesToRead];
            try
            {
                int len = serialPort.Read(receiveData, 0, receiveData.Length);
                if (len > 0)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.Append(Encoding.Default.GetString(receiveData, 0, len));
                    string str = sb.ToString().Trim();
                    str = str.Trim("\n\r".ToCharArray());
                    ReceiveMsgEventArgs msg = new ReceiveMsgEventArgs(str);
                    ReceiveDataEvent?.Invoke(this, msg);
                }
            }
            catch (Exception ex)
            {
                CommonModules.Notifier.NotifyHelper.Notify(CommonModules.Notifier.NotifyLevel.ERROR,
                                                           $"串口{serialPort.PortName}接收数据出错,错误信息:{ex.Message}。");
            }
            finally
            {
                serialPort.DiscardInBuffer();
            }
        }
Пример #2
0
 ///读取数据事件
 private void ComDevice_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     try
     {
         byte[] ReDatas = new byte[ComDevice.BytesToRead];
         ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据
         SourceData = aSCIIEncoding.GetString(ReDatas);
         ReceiveDataEvent?.Invoke(SourceData);
     }
     catch (Exception ex)
     {
         MessageBox.Show("接受数据出错:" + ex.Message);
     }
 }
Пример #3
0
 protected virtual void OnReceiveDataEvent(object source, ReceiveDataArgs args)
 {
     ReceiveDataEvent?.Invoke(source, args);
 }