コード例 #1
0
        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadComplete(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (sender.GetType() != typeof(System.IO.Ports.SerialPort))
            {
                return;
            }
            System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender;

            try
            {
                comPort.ReceivedBytesThreshold = comPort.ReadBufferSize;
                int bytesRead = comPort.BytesToRead;
                if (bytesRead == 0)
                {
                    comPort.ReceivedBytesThreshold = 1;
                    return;
                }
                else
                {
                    byte[] byteMessage = new byte[bytesRead];
                    comPort.Read(byteMessage, 0, byteMessage.Length);

                    logger.Warn(string.Format("Receive Cmd:{0}", BytesHelper.BytesToHexStr(byteMessage)));
                    if (receiveMessageInfo.OverReceiveBytes != null && receiveMessageInfo.OverReceiveBytes.Length > 0)
                    {
                        receiveMessageInfo.TempReceiveBytes = new byte[receiveMessageInfo.OverReceiveBytes.Length + bytesRead];
                        Array.Copy(receiveMessageInfo.OverReceiveBytes, 0, receiveMessageInfo.TempReceiveBytes, 0, receiveMessageInfo.OverReceiveBytes.Length);
                        Array.Copy(byteMessage, 0, receiveMessageInfo.TempReceiveBytes, receiveMessageInfo.OverReceiveBytes.Length, bytesRead);
                    }
                    else
                    {
                        receiveMessageInfo.TempReceiveBytes = new byte[bytesRead];
                        Array.Copy(byteMessage, receiveMessageInfo.TempReceiveBytes, bytesRead);
                    }

                    receiveMessageInfo.IsCanRunNext = true;
                    while (receiveMessageInfo.IsCanRunNext)
                    {
                        if (receiveMessageInfo.TempReceiveBytes == null)
                        {
                            receiveMessageInfo.TempReceiveBytes = receiveMessageInfo.OverReceiveBytes;
                            receiveMessageInfo.OverReceiveBytes = null;
                        }
                        NTPParseReceiveMsg.ParseResponseMessage(receiveMessageInfo);
                        if (receiveMessageInfo.IsReadNTPFinished)
                        {
                            NTP ntp = receiveMessageInfo.ntp;
                            receiveMessageInfo.ntp = new NTP();
                            receiveMessageInfo.IsReadNTPFinished       = false;
                            receiveMessageInfo.IsReadNTPHeaderFinished = false;
                            bool check = ntp.CheckXORSUM();
                            if (check)
                            {
                                if (ReceiveMsgEvent != null)
                                {
                                    byte[] bytes = ntp.ToBytes();
                                    ReceiveMsgEvent(ntp);
                                }
                            }
                            else
                            {
                                logger.Error("Receive message checked error!");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
            finally
            {
                comPort.ReceivedBytesThreshold = 1;
            }
        }
コード例 #2
0
ファイル: SerialClient.cs プロジェクト: A1X71/twilight
 /// <summary>
 /// 发送指令
 /// </summary>
 /// <param name="ntp">NTP协议</param>
 private static void SendCommand(NTP ntp)
 {
     byte[] bytes = ntp.ToBytes();
     logger.Info(string.Format("Send Cmd:{0}", BytesHelper.BytesToHexStr(bytes)));
     conn.SendMessage(bytes);
 }