Beispiel #1
0
        private void BtnCreateServer_Click(object sender, EventArgs e)
        {
            m_MyTcpServerArm = MyTcpServer.GetInstance();
            if (m_MyTcpServerArm != null)
            {
                IPAddress ServerIp   = IPAddress.Parse(TextBoxIp.Text);
                int       ServerPort = int.Parse(TextBoxPort.Text);
                m_MyTcpServerArm.CreatServer(ServerIp, ServerPort);
            }

            m_OutStream = File.Open(m_UpdateFileName, FileMode.OpenOrCreate);
            m_Writer    = new BinaryWriter(m_OutStream);

            //创建升级线程
            m_MainThread = new Thread(new ThreadStart(MainThreadFunc));
            m_MainThread.IsBackground = true;
            m_MainThread.Start();
        }
Beispiel #2
0
        public void ProcessTcpServerMessage()
        {
            if (m_MyTcpServerArm != null)
            {
                while (m_MyTcpServerArm.m_RecvMeasQueue.Count != 0)
                {
                    TcpMeas TempMeas = m_MyTcpServerArm.m_RecvMeasQueue.Dequeue();
                    if (TempMeas != null && TempMeas.Client != null)
                    {
                        if (TempMeas.MeasType == TcpMeasType.MEAS_TYPE_ARM)  // 处理和PLC之间的消息
                        {
                            Message.MessageCodeARM Code = (Message.MessageCodeARM)TempMeas.MeasCode;
                            Debug.WriteLine(Code);

                            switch (Code)
                            {
                            case Message.MessageCodeARM.ArmUpdate:
                            {
                                //断开服务后重新创建
                                m_MyTcpServerArm.CloseServer();
                                Thread.Sleep(1000);
                                if (m_MyTcpServerArm != null)
                                {
                                    IPAddress ServerIp   = IPAddress.Parse(TextBoxIp.Text);
                                    int       ServerPort = int.Parse(TextBoxPort.Text);
                                    m_MyTcpServerArm.CreatServer(ServerIp, ServerPort);
                                }
                            }
                            break;

                            case Message.MessageCodeARM.ArmUpdateFileLength:
                            {
                                m_FileSize = BitConverter.ToInt32(TempMeas.Param, Message.MessageCommandIndex + 1);
                            }
                            break;

                            case Message.MessageCodeARM.ArmUpdateFileData:
                            {
                                m_TotalPages++;
                                if (m_TotalPages != 0 && m_TotalPages * 16 >= m_FileSize)
                                {
                                    m_Writer.Write(TempMeas.Param, Message.MessageCommandIndex + 3, (m_FileSize - (m_TotalPages - 1) * 16));
                                }
                                else
                                {
                                    m_Writer.Write(TempMeas.Param, Message.MessageCommandIndex + 3, 16);
                                }

                                m_CurrentPage = BitConverter.ToInt16(TempMeas.Param, Message.MessageCommandIndex + 1);
                                Debug.WriteLine("Current Page: " + m_CurrentPage);

                                if (m_TotalPages != 0 && m_TotalPages * 16 >= m_FileSize)
                                {
                                    TempMeas.Param[Message.MessageCommandIndex] = (byte)Message.MessageCodeARM.ArmUpdateSucceed;
                                    TempMeas.Param[TempMeas.Param.Length - 2]   = 0x00;

                                    byte Sum = MyMath.CalculateSum(TempMeas.Param, TempMeas.Param.Length);
                                    TempMeas.Param[TempMeas.Param.Length - 2] = Sum;
                                }
                            }
                            break;

                            default: break;
                            }

                            NetworkStream stream = TempMeas.Client.GetStream();
                            stream.Write(TempMeas.Param, 0, TempMeas.Param.Length);
                        }
                    }
                }
            }
        }