コード例 #1
0
        } // end of GetDataWithModbusList

        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="obj">事件源</param>
        /// <param name="newData">事件源上传的数据</param>
        public void UpdataOnRegisterValueChangeEvent(object obj, UpDataEventArgs newData)
        {
            Debug.WriteLine("UpdataOnRegisterValueChangeEvent方法");

            const int maxUpdata = 2;
            //数据拆分成若干包
            List <List <UpDataBase.RTWriteProxy.PointRTModel> > dataPacketList = SplitDataPacket(newData.Updata, maxUpdata);

            for (int i = 0; i < dataPacketList.Count; ++i)
            {
                //分包上传数据
                UsrWrite(dataPacketList[i]);
            }
            //Debug.WriteLine("count={0},object={1}:", count, obj);
        }
コード例 #2
0
        } //end of StartModbusServer

        /// <summary>
        /// 发送请求和接收读取寄存器的值
        /// </summary>
        ///
        //public void RequsAndRecvRegisterValue(object obj, EventArgs e)
        public void RequsAndRecvRegisterValue()
        {
            tcpClient = new TcpClient();

            //连接服务器
            try
            {
                tcpClient.Connect(serverIP, serverPort);
                Debug.WriteLine("Connect...");
            }
            catch (Exception e1)
            {
                Debug.WriteLine("Connect...error:" + e1.Message);
            }

            //获取网络传输流
            try
            {
                netIOStream              = tcpClient.GetStream();
                netIOStream.ReadTimeout  = 500;
                netIOStream.WriteTimeout = 500;
            }
            catch (Exception e2)
            {
                Debug.WriteLine("tcpClient error:" + e2.Message);
            }

            if (netIOStream.CanWrite)
            {
                //发送请求包
                sendData = new ModbusSendDataStruct();
                //sendData.RegCount = 20;
                SetSendDataFormat(0, 6);        //该方法需要改造,根据xml设定请求数据包
                sendData.FunNum = 3;
                byte[] temp = sendData.ToArray();
                netIOStream.Write(temp, 0, temp.Length);

                netIOStream.Flush();
            }

            if (netIOStream.CanRead)
            {
                byte[] recvdata = new byte[1026];
                int    recvlen  = netIOStream.Read(recvdata, 0, recvdata.Length); //接收回应包

                ///////////////////////////////////////////////
                recData = new ModbusRecvDataStruct();     //考虑到接收到的数据只是暂时保持,因此读后要清空或重新分配内,这里重新分配内存

                //recData.RecvDataToUInt16WithFunNum(recvdata);
                //recData.ParseRecvResData(recvdata);   //解析byte[],并保存在recData.recData.RecvResData中

                SetRegisterValueWithRecResData();         //将收到的值保存到寄存器(列表)中
                /////////////////////////////////////////////

                List <UpDataBase.RTWriteProxy.PointRTModel> updataList = new List <UpDataBase.RTWriteProxy.PointRTModel>();   //ModbusUpDataBase需要上传的数据列表

                // 从ModBus中的寄存器列表列表registerList中提取数据并封装成UpDataBase.RTWriteProxy.PointRTModel类型
                for (int indexRes = 0; indexRes < registerList.Count; ++indexRes)
                {
                    updataList.Add(new UpDataBase.RTWriteProxy.PointRTModel()
                    {
                        ID = new UpDataBase.RTWriteProxy.IDModel()
                        {
                            DevID   = registerList[indexRes].DevId,
                            Type    = registerList[indexRes].Type,
                            PointID = registerList[indexRes].PointId,
                        },
                        Value = registerList[indexRes].ResValue,
                        Time  = DateTime.Now,
                    }
                                   );      //Add

                    Debug.WriteLine("[{0}]: {1}", indexRes, registerList[indexRes].ResValue);
                }                                       //for

                if (null != OnRegisterValueChangeEvent) //确认有方法可以执行
                {
                    //封装ModbusUpDataBase需要上传的数据列表
                    UpDataEventArgs upData = new UpDataEventArgs(updataList);
                    //触发事件,并上传数据
                    OnRegisterValueChangeEvent(this, upData);
                }
            }    //if

            netIOStream.Close();
            tcpClient.Close();
        } //end of StartRequestfu