Esempio n. 1
0
        public THOK.MCP.Message Parse(string msg)
        {
            THOK.MCP.Message message = null;

            buffer += msg;
            int headPos = buffer.IndexOf(headChar, 0);

            if (headPos != -1)
            {
                int tailPos = buffer.IndexOf(tailChar, headPos);
                if (tailPos != -1)
                {
                    if (headPos + 1 < tailPos)
                    {
                        //当终止符前有两个起始符,则以第二个为准.
                        int tmp = buffer.IndexOf(this.headChar, (headPos + 1), (tailPos - headPos));
                        if (tmp >= 0)
                        {
                            headPos = headPos + tmp;
                        }

                        //提取位于开始和结束字符串中的数据信息
                        string scanData = buffer.Substring((headPos + 1), (tailPos - headPos - 1));
                        int    pos      = scanData.IndexOf(separator);
                        string command  = "";
                        string barcode  = "";
                        if (pos != -1)
                        {
                            command = scanData.Substring(0, pos).Trim();
                            barcode = scanData.Substring(pos + 1).Trim();

                            Dictionary <string, string> parameters = new Dictionary <string, string>();
                            parameters.Add("barcode", barcode);
                            message = new THOK.MCP.Message(true, msg, command, parameters);
                        }
                        else
                        {
                            message = new THOK.MCP.Message(msg);
                        }
                        //将读到的条码去除
                        buffer = buffer.Remove(0, (tailPos + 1));
                        //避免错读,只读取一码后清空buffer
                        buffer = "";
                    }
                    else
                    {
                        buffer = buffer.Substring(headPos);
                    }
                }
                else
                {
                    message = new THOK.MCP.Message(msg);
                }
            }
            else
            {
                message = new THOK.MCP.Message(msg);
            }
            return(message);
        }
Esempio n. 2
0
        public THOK.MCP.Message Parse(string msg)
        {
            THOK.MCP.Message message = null;

            buffer += msg;
            int headPos = buffer.IndexOf(headChar, 0);

            if (headPos != -1)
            {
                int tailPos = buffer.IndexOf(tailChar, headPos);
                if (tailPos != -1)
                {
                    if (headPos + 1 < tailPos)
                    {
                        //����ֹ��ǰ��������ʼ��,���Եڶ���Ϊ׼.
                        int tmp = buffer.IndexOf(this.headChar, (headPos + 1), (tailPos - headPos));
                        if (tmp >= 0)
                        {
                            headPos = headPos + tmp;
                        }

                        //��ȡλ�ڿ�ʼ�ͽ����ַ����е�������Ϣ
                        string scanData = buffer.Substring((headPos + 1), (tailPos - headPos - 1));
                        int pos = scanData.IndexOf(separator);
                        string command = "";
                        string barcode = "";
                        if (pos != -1)
                        {
                            command = scanData.Substring(0, pos).Trim();
                            barcode = scanData.Substring(pos + 1).Trim();

                            Dictionary<string, string> parameters = new Dictionary<string, string>();
                            parameters.Add("barcode", barcode);
                            message = new THOK.MCP.Message(true, msg, command, parameters);

                        }
                        else
                        {
                            message = new THOK.MCP.Message(msg);
                        }
                        //������������ȥ��
                        buffer = buffer.Remove(0, (tailPos + 1));
                        //������,ֻ��ȡһ������buffer
                        buffer = "";
                    }
                    else
                    {
                        buffer = buffer.Substring(headPos);
                    }
                }
                else
                    message = new THOK.MCP.Message(msg);
            }
            else
            {
                message = new THOK.MCP.Message(msg);
            }
            return message;
        }
Esempio n. 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;
        }
Esempio n. 4
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);
                }
            }
        }