Esempio n. 1
0
        public int AppMain_start()
        {
            m_Protocol = new TcpClientAppProtocol(updTimer, true, DataArea, false);

            m_Comm            = new TCPClient(IpStr, port, CommConst.BUFFERSIZE, m_Protocol);
            m_Protocol.m_Comm = m_Comm;
            //开始尝试连接
            m_Protocol.ClearCache();
            int StartTick = HyTick.TickTimeGet();

            m_Comm.StartClient();
            while (!m_Comm.Connected)
            {
                //等待10秒是否连接成功
                if (HyTick.TickTimeIsArrived(StartTick, 10000))
                {
                    //MessageBox.Show("连接失败!");
                    return(-1);
                }
                Application.DoEvents();
            }
            m_Protocol.Main_Start();
            //MessageBox.Show("连接成功!");
            return(1);
        }
Esempio n. 2
0
        /// <summary>
        /// 标准的通信协议发送处理驱动
        /// </summary>
        public virtual void Main_CommDriver_Send()
        {
            try
            {
                // 将发送缓冲区中的第一个命令发送出去
                if (SendCache.Count > 0)
                {
                    se = SendCache.Peek();
                    if (se.WaitingFlag)
                    {
                        //当前命令已经发送,正在等待
                        Main_CommDriver_Resend();
                    }
                    else
                    {
                        //发送一个新的命令
                        se.SendTick = HyTick.TickTimeGet();
                        Main_SendCmd();

                        if (se.NeedReply)
                        {
                            // 需要等待回复的报文:应该置标志并等待接收、重发等处理
                            se.WaitingFlag = true;
                        }
                        else
                        {
                            // 不需要等待回复的报文:应该直接从发送缓冲区清除
                            lock (SendCacheLock)
                            {
                                SendCache.Dequeue();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("方法Main_CommDriver_Send()出现了错误!" + ex.Message);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 标准的通信协议轮询处理驱动
        /// </summary>
        public virtual void Polling_Driver()
        {
            try
            {
                /// 通信未连接则不进行轮询
                if (!this.m_Comm.Connected)
                {
                    return;
                }

                // 定时把轮询的报文发送到发送缓冲区中
                if (this.PollingList.Count == 0)
                {
                    return;
                }

                // 若发送缓冲区非空,则意味着前次的轮询未完或者有命令正在处理,目前可以不轮询
                if (SendCache.Count > 0)
                {
                    return;
                }

                if (this.m_PollingIndex == 0)
                {
                    ///表示要开始一次新的轮询周期

                    if (m_TimeDifferenceCalcFlag_Polling)
                    {
                        m_TimeDifferenceCalcFlag_Polling = false;
                        //记录上次轮询所花费的时间
                        this.m_LastPollingPeriod = HyTick.TickTimeDifference(this.m_PollingTick, HyTick.TickTimeGet());
                        if (this.m_MinPollingPeriod > this.m_LastPollingPeriod)
                        {
                            this.m_MinPollingPeriod = this.m_LastPollingPeriod;
                        }
                    }

                    if (!HyTick.TickTimeIsArrived(this.m_PollingTick, this.m_PollingInterval))
                    {
                        return;
                    }
                    ///记录本次轮询周期开始的时刻
                    this.m_PollingTick = HyTick.TickTimeGet();
                    m_TimeDifferenceCalcFlag_Polling = true;
                }

                // 现在可以把当前的一个轮询报文发送到缓冲区
                ProtocolFrameStruct pl = PollingList[this.m_PollingIndex].Clone();
                pl.NeedReply   = true;
                pl.ResendCount = 0;
                pl.WaitingFlag = false;
                lock (SendCacheLock)
                {
                    SendCache.Enqueue(pl);
                    this.m_PollingIndex++;
                    this.m_PollingIndex %= PollingList.Count;
                }
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("方法Polling_Driver()出现了错误!" + ex.Message);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 标准的通信协议重发处理
        /// </summary>
        public virtual void Main_CommDriver_Resend()
        {
            if (HyTick.TickTimeIsArrived(se.SendTick, this.m_WaitTick))
            {
                //判断该子站通信是否出现问题
                if (m_HardAddress != null)
                {
                    if (m_HardAddress.ContainsKey(se.Address))
                    {
                        if (m_HardAddress[se.Address].ErrorCount < 5)
                        {
                            m_HardAddress[se.Address].ErrorCount++;
                        }
                        if (m_HardAddress[se.Address].ErrorCount >= 5)
                        {
                            m_HardAddress[se.Address].CommStatus = false;
                        }
                    }
                }
                else
                {
                    if (this.m_ErrorCount < 5)
                    {
                        this.m_ErrorCount++;
                    }
                    if (this.m_ErrorCount >= 5)
                    {
                        this.m_CommStatus = false;
                    }
                }
                if (this.IsPolling(se.AFN))
                {
                    //轮询的报文不需要重发处理,可等待下一次轮询
                    if (SendCache.Count > 0)
                    {
                        lock (SendCacheLock)
                        {
                            SendCache.Dequeue();
                        }
                    }
                }
                else
                {
                    if (se.ResendCount >= CommConst.MAXRESEND)
                    {
                        //超过最大重发次数,当前命令失败,从发送缓冲区清除
                        if (SendCache.Count > 0)
                        {
                            lock (SendCacheLock)
                            {
                                SendCache.Dequeue();
                            }
                        }
                    }
                    else
                    {
                        //重发当前命令
                        se.ResendCount++;
                        se.SendTick = HyTick.TickTimeGet();

                        Main_SendCmd();
                    }
                }
            }
        }
Esempio n. 5
0
        private void cmdStart_Click(object sender, EventArgs e)
        {
            if (!HyNetUtility.IsValidIP(this.txtIP.Text))
            {
                MessageBox.Show("设定的IP地址不正确!");
                this.txtIP.Focus();
                return;
            }

            int port;

            if (!int.TryParse(this.txtPort.Text, out port) || port <= 0 || port > 65535)
            {
                MessageBox.Show("设定的端口不正确!");
                this.txtPort.Focus();
                return;
            }

            if (m_Comm != null && m_Comm.Connected)
            {
                MessageBox.Show("当前已连接!");
                return;
            }


            byte[] autosendData = null;
            if (chkHex.Checked)
            {
                autosendData = HexEncoding.Instance.GetBytes(this.TextBoxSend.Text);
            }
            else
            {
                //用GB2312编码
                autosendData = Encoding.GetEncoding("gb2312").GetBytes(this.TextBoxSend.Text);
            }
            m_Protocol = new ByteStreamProtocol((int)updTimer.Value, checkBoxAuto.Checked, autosendData, false);

            m_Comm            = new TCPClient(this.txtIP.Text, port, CommConst.BUFFERSIZE, m_Protocol);
            m_Protocol.m_Comm = m_Comm;

            this.commFrameShow1.Init(m_Protocol);

            //开始尝试连接
            m_Protocol.ClearCache();
            int StartTick = HyTick.TickTimeGet();

            m_Comm.StartClient();
            while (!m_Comm.Connected)
            {
                //等待10秒是否连接成功
                if (HyTick.TickTimeIsArrived(StartTick, 10000))
                {
                    MessageBox.Show("连接失败!");
                    return;
                }
                Application.DoEvents();
            }

            MessageBox.Show("连接成功!");

            m_Protocol.Main_Start();
        }