Example #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);
        }
Example #2
0
        //获取命令缓冲区中的命令信息
        public int cmdmany_getcmd(int devindex)
        {
            int ExecSec = 0;

            if (m_autorunfalg == 0)
            {
                m_CmdIndex = 0;
                return(0);
            }
            else if (m_autorunfalg == 1)
            {
                if (m_CmdIndex == 0 && m_process_tick == 0)
                {
                    m_process_tick    = HyTick.GetTickCount(); //设定当前的Ticket时钟
                    m_StopPeriod_tick = 0;
                }

                if (SequenceCmdList.Count > 0 && m_CmdIndex < SequenceCmdList.Count) //表示有多命令要开始执行了
                {
                    if (devindex == SequenceCmdList[m_CmdIndex].devIndex)            //
                    {
                        ExecSec = SequenceCmdList[m_CmdIndex].secs * 1000 + m_StopPeriod_tick;
                        if (HyTick.TickTimeIsArrived(m_process_tick, ExecSec))  //说明时间到了需要下发命令了,进行下发命令的组织。
                        {
                            devd[devindex].CmdFlag = true;
                            m_CmdIndex             = m_CmdIndex + 1;
                            return(m_CmdIndex);
                        }
                    }
                }

                if (m_CmdIndex == SequenceCmdList.Count)  //表示已经执行完成
                {
                    m_autorunfalg = 0;
                    m_CmdIndex    = 0;
                    LogHelper.Log("************" + System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + "程序运行成功结束" + "***********");
                    return(m_CmdIndex);
                }
            }
            return(0);
        }
Example #3
0
        //处理总的通讯调度
        public int process(int m_appdataindex)
        {
            bool acmd = false;

            try
            {
                switch (m_process_step)
                {
                case 0:      //建立IP端口的连接
                    if (m_appdata.devd[m_appdataindex].ComType == "TCP")
                    {
                        AppMain_start();
                    }
                    m_process_step = 1;     //主通信流程调度
                    break;

                case 1:     //判断端口是否连接成功
                    if (m_appdata.devd[m_appdataindex].ComType == "TCP" && m_Comm.Connected == true)
                    {
                        m_appdata.devd[m_appdataindex].CommFlag = true;
                        m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, true); //更新设备设备的通讯状态
                        m_process_step = 2;                                                          //连接成功准备发送命令
                    }
                    else
                    {
                        if (m_appdata.devd[m_appdataindex].ComType == "TCP")
                        {
                            m_process_step = 0;                                                           //否则进行重新连接
                            m_appdata.devd[m_appdataindex].CommFlag = false;
                            m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, false); //更新设备设备的通讯状态
                        }
                        else
                        {
                            m_appdata.devd[m_appdataindex].CommFlag = true;
                            m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, true);
                            m_process_step = 2;      //连接成功准备发送命令
                        }
                    }
                    break;

                case 2:
                    acmd = false;
                    int cmdindex = 0;
                    cmdindex = m_appdata.cmdmany_getcmd(m_appdataindex);
                    if (cmdindex > 0)                                       //判断是否有批命令发送
                    {
                        if (m_appdata.devd[m_appdataindex].CmdFlag == true) //  获取当前的命令
                        {
                            // 根据cmdindex获取命令
                            acmd = true;
                        }
                    }
                    if (acmd)      //根据获取到的
                    {
                        string sendIP   = string.Empty;
                        string ComType  = m_appdata.devd[m_appdataindex].ComType;
                        byte[] sendbyte = m_appdata.Create_cmdbyte_ByIndex(cmdindex - 1);
                        if (ComType == "TCP")
                        {
                            m_Comm.SendData(sendbyte, ref sendIP);
                            m_process_step = 4;     //处理发送后的反馈
                        }
                        else if (ComType == "UDP")  //UDP方式的命令发送
                        {
                            UdpSendMessage(m_appdataindex, sendbyte);
                            //需要进行处理接收事务
                            if (m_appdata.devd[m_appdataindex].remoteSendDateChk == "TRUE")
                            {
                                m_appdata.Process_EquComm_RunStatus("01");    //暂停当前的进程
                                m_UdpServer = new UdpServer();
                                m_UdpServer.StartListen(m_appdata.devd[m_appdataindex].LocalIp, m_appdata.devd[m_appdataindex].Recvport);
                                if (m_UdpServer.RecvLength > 0)
                                {
                                    m_appdata.Process_EquComm_RunStatus("02");    //继续执行
                                }
                                else
                                {
                                    //执行程序中止执行
                                    m_appdata.Process_EquComm_RunStatus("03");    //中止执行,在这里需要调用
                                }
                            }
                            m_process_step = 2;
                        }
                        LogHelper.Log(System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + "  " + ComType + (cmdindex - 1).ToString("00") + "Send:" + HexEncoding.Instance.GetString(sendbyte));
                        m_appdata.Process_EquComm_Message(System.DateTime.Now, m_appdata.GetSendMessage(cmdindex - 1), true, true);

                        /*
                         * if (cmdindex == 1)
                         * {
                         *  m_appdata.CmdIndexStatus(cmdindex, true);
                         * }
                         * if (cmdindex > 1 && m_appdata.SequenceCmdList[cmdindex].secs > m_appdata.SequenceCmdList[cmdindex - 1].secs)
                         * {
                         *  m_appdata.CmdIndexStatus(cmdindex, true);
                         * }
                         */
                        m_appdata.CmdIndexStatus(cmdindex - 1, true);
                    }
                    else
                    {
                        string sendIP   = string.Empty;
                        byte[] sendbyte = m_appdata.GetCmd_Bydev(m_appdataindex);
                        m_Comm.SendData(sendbyte, ref sendIP);
                        m_appdata.Process_EquComm_Message(System.DateTime.Now, HexEncoding.Instance.GetString(sendbyte), true, false);
                        m_process_step = 3;
                        m_process_tick = HyTick.GetTickCount();
                    }
                    break;

                case 3:      //处理获得的数据内容
                    if (m_Protocol.m_recvdatalen > 0)
                    {
                        //处理接收到的数据
                        Receive_Date(m_appdataindex, m_Protocol.m_recvdata);
                        m_appdata.Process_EquComm_Message(System.DateTime.Now, HexEncoding.Instance.GetString(m_Protocol.m_recvdata), false, true);
                        //处理完了就清除掉数据
                        m_Protocol.m_recvdatalen = 0;
                        //处理完成后继续发送
                        m_process_step = 2;
                    }
                    else
                    {
                        if (HyTick.TickTimeIsArrived(m_process_tick, 1000))
                        {
                            m_process_step = 2;
                        }
                    }
                    // 判断通信,只有TCP的方式才进行这么处理
                    if (m_Comm.Connected == false && m_appdata.devd[m_appdataindex].ComType == "TCP")
                    {
                        m_process_step = 0;
                    }
                    break;

                case 4:                               //Tcp的方式处理控制命令返回协议处理
                    if (m_Protocol.m_recvdatalen > 0) //只要接收到数据就进行继续发送
                    {
                        m_process_step = 2;
                    }
                    break;
                }
                return(0);
            }
            catch
            {
                return(-2);
            }
        }
Example #4
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);
            }
        }
Example #5
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();
                    }
                }
            }
        }
Example #6
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();
        }