Example #1
0
        private void server_OnReceive(object sender, ReceiveEventArgs e)
        {
            //object mes = null;

            //lock (message)
            //{
            try
            {
                Message message = null;
                string  text    = "";
                text = string.Format("recv: <--- {0}", e.Read());
                WriteToLog(text);
                if (null != protocol)
                {
                    message = protocol.Parse(e.Read());
                }
                else
                {
                    message = new Message(e.Read());
                }

                //过滤收到重复的报文,流水号0000除外
                //if (message.Parameters["SeqNo"] != "0000")
                //{
                //    if (lastSeqNo != message.Parameters["SeqNo"])
                //    {
                //text = string.Format("recv: <--- {0}", e.Read());
                //WriteToLog(text);

                if (message.Parsed)
                {
                    DispatchState(message.Command, message.Parameters);
                }
                //}
                //lastSeqNo = message.Parameters["SeqNo"];
                //}
                //else
                //{
                //    //text = string.Format("recv: <--- {0}", e.Read());
                //    //WriteToLog(text);

                //    if (message.Parsed)
                //        DispatchState(message.Command, message.Parameters);
                //}

                dtTime = DateTime.Now;
            }

            catch (Exception ex)
            {
                Logger.Debug("server_OnReceive Error " + ex.Message.ToString());
            }
            //}
        }
Example #2
0
        void Run()
        {
            resetEvent.WaitOne();
            while (isConnected && serialPort.IsOpen)
            {
                while (isConnected && serialPort.IsOpen && serialPort.BytesToRead == 0)
                {
                    resetEvent.WaitOne(100, false);
                }

                string msg = null;
                if (isHex)
                {
                    int    bytes  = serialPort.BytesToRead;
                    byte[] buffer = new byte[bytes];
                    serialPort.Read(buffer, 0, bytes);
                    msg = BitConverter.ToString(buffer);
                }
                else
                {
                    msg = serialPort.ReadExisting();
                }

                Message message = null;

                if (null != protocol)
                {
                    message = protocol.Parse(msg);
                }
                else
                {
                    message = new Message(msg);
                }

                if (message.Parsed)
                {
                    DispatchState(message.Command, message.Parameters);
                }
            }
        }
Example #3
0
        private void server_OnReceive(object sender, ReceiveEventArgs e)
        {
            Message message = null;

            if (null != protocol)
            {
                message = protocol.Parse(e.Read());
            }
            else
            {
                message = new Message(e.Read());
            }
            string text = string.Format("recv: <--- {0}", e.Read());

            WriteToLog(text);

            if (message.Parsed)
            {
                DispatchState(message.Command, message.Parameters);
            }
            dtTime = DateTime.Now;
        }