Пример #1
0
        public void Write(SendDataPacket CMD)
        {
            byte[] cmd;
            if (CMD.btdata == null)
            {
                cmd        = this.Encoder.Encode(CMD.data);
                CMD.btdata = cmd;
            }
            else
            {
                cmd = CMD.btdata;
            }

            this.WriteLog("Length = " + cmd.Length.ToString() + ":" + ByteToString(cmd));

            this.SerialPort.Write(cmd, 0, cmd.Length);
        }
Пример #2
0
        public void Write(SendDataPacket CMD)
        {
            byte[] cmd;
            if (CMD.btdata == null)
            {
                cmd = this.Encoder.Encode(CMD.data);
                CMD.btdata = cmd;
            }
            else
            {
                cmd = CMD.btdata;
            }

            this.WriteLog("Length = " + cmd.Length.ToString() + ":" + ByteToString(cmd));

            this.SerialPort.Write(cmd, 0, cmd.Length);
        }
Пример #3
0
        /// <summary>
        /// 采用独立线程对数据进行,存取处理。
        /// </summary>
        private void Run()
        {
            try
            {
                if (this.DataQueue.Count != 0 && this.CurrentData == null)
                {
                    this.CurrentData = this.DataQueue.First.Value;
                    this.DataQueue.RemoveFirst();
                    this.com.Write(this.CurrentData);
                    this.CurrentData.sendTime = this.Timeout;
                    Thread.Sleep(4000);
                }
            }
            catch { }

            while (this.com.SerialPort != null && this.com.SerialPort.IsOpen)
            {
                try
                {
                    while (this.DataQueue.Count == 0 && this.CurrentData == null)
                    {
                        this.com.DataReceived();
                        Thread.Sleep(100);
                        Com.strTest = "1";
                    }
                    int i = 0;
                    while (this.CurrentData != null && i < 10)
                    {
                        Thread.Sleep(50);
                        this.com.DataReceived();
                        i++;
                    }

                    if (this.CurrentData != null)
                    {
                        this.DataQueue.AddFirst(this.CurrentData);
                        this.CurrentData = null;
                    }

                    while (this.DataQueue.First == null && this.DataQueue.Count > 0)
                    {
                        this.DataQueue.RemoveFirst();
                    }

                    if (this.DataQueue.Count > 0)
                    {
                        this.CurrentData = this.DataQueue.First.Value;
                        this.DataQueue.RemoveFirst();

                        if (this.CurrentData.sendTime <= this.Timeout)
                        {
                            this.CurrentData.sendTime++;
                            this.com.Write(this.CurrentData);
                        }
                        else
                        {
                            if (TimeOutEvent != null)
                            {
                                TimeOutEvent();
                            }
                            // 发送超时次数超过设定值,应记录故障,以便查找原因。
                            this.com.WriteLog("当前数据包发送多次超时:" + this.com.ByteToString(this.CurrentData.btdata));
                            this.CurrentData = null;
                        }
                    }
                }
                catch { }
            }
        }
Пример #4
0
        /// <summary>
        /// 采用独立线程对数据进行,存取处理。
        /// </summary>
        private void Run()
        {
            try
            {
                if (this.DataQueue.Count != 0 && this.CurrentData == null)
                {
                    this.CurrentData = this.DataQueue.First.Value;
                    this.DataQueue.RemoveFirst();
                    this.com.Write(this.CurrentData);
                    this.CurrentData.sendTime = this.Timeout;
                    Thread.Sleep(4000);
                }
            }
            catch { }

            while (this.com.SerialPort != null && this.com.SerialPort.IsOpen)
            {
                try
                {
                    while (this.DataQueue.Count == 0 && this.CurrentData == null)
                    {
                        this.com.DataReceived();
                        Thread.Sleep(100);
                        Com.strTest = "1";
                    }
                    int i = 0;
                    while (this.CurrentData != null && i <10)
                    {
                        Thread.Sleep(50);
                        this.com.DataReceived();
                        i++;
                    }

                    if (this.CurrentData != null)
                    {
                        this.DataQueue.AddFirst(this.CurrentData);
                        this.CurrentData = null;
                    }

                    while (this.DataQueue.First == null && this.DataQueue.Count > 0)
                    {
                        this.DataQueue.RemoveFirst();
                    }

                    if (this.DataQueue.Count > 0)
                    {
                        this.CurrentData = this.DataQueue.First.Value;
                        this.DataQueue.RemoveFirst();

                        if (this.CurrentData.sendTime <= this.Timeout)
                        {
                            this.CurrentData.sendTime++;
                            this.com.Write(this.CurrentData);
                        }
                        else
                        {
                            if (TimeOutEvent != null)
                            {
                                TimeOutEvent();
                            }
                            // 发送超时次数超过设定值,应记录故障,以便查找原因。
                            this.com.WriteLog("当前数据包发送多次超时:" + this.com.ByteToString(this.CurrentData.btdata));
                            this.CurrentData = null;
                        }
                    }
                }
                catch { }
            }
        }