Beispiel #1
0
        /// <summary>
        /// 解析接收数据
        /// </summary>
        private void ParseReceive()
        {
            while (true)
            {
                Thread.Sleep(100);

                if (this.dataReceiveStr != "")
                {
                    string tmpReceiveStr = dataReceiveStr;

                    if (tmpReceiveStr.IndexOf("B5") == 0 && tmpReceiveStr.Length % 9 == 0)
                    {
                        this.dataReceiveStr = "";

                        byte[] bufferSend = Converts.StringToBytes(tmpReceiveStr);

                        ByteConvertString(bufferSend);

                        if (EventDataArrival != null && dataArrival.Count > 0)
                        {
                            this.EventDataArrival(dataArrival);
                        }

                        this.dataArrival.Clear();
                    }
                    else
                    {
                        this.dataReceiveStr = "";
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 接收数据
        /// </summary>
        private void SocketThread()
        {
            while (true)
            {
                Thread.Sleep(1);

                try
                {
                    byte[] buffer = new byte[1024];
                    int    count  = this.socket.Receive(buffer);

                    this.countReceive = this.countReceive + count;

                    if (buffer[0] == 0xB5 && count % 9 == 0)
                    {
                        byte[] data = new byte[count];
                        Array.Copy(buffer, 0, data, 0, data.Length);
                        this.dataReceiveStr = this.dataReceiveStr + Converts.BytesToString(data);
                    }
                    else
                    {
                        this.dataReceiveStr = "";
                    }
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                }
            }
        }
Beispiel #3
0
        public bool SendData(string[] strData)
        {
            try
            {
                string message = "A5" + strData[0] + strData[1] + "5A";

                byte[] buffer = Converts.StringToBytes(message);

                this.socket.Send(buffer);

                this.countSend = this.countSend + buffer.Length;

                return(true);
            }
            catch
            {
                return(false);
            }
        }