Exemplo n.º 1
0
        private List <ModbusTcpDataEntity> GetModbusTcpData(ModbusTcpResult mbTcpResult)
        {
            List <ModbusTcpDataEntity> mbTcpDataList = new List <ModbusTcpDataEntity>();

            //查找对应的operation
            var ops = from n in m_config.ModbusOperations
                      where n.Identifier == mbTcpResult.Identifier
                      select n;
            ModbusTcpOperation mbTcpOp = null;

            foreach (ModbusTcpOperation op in ops)
            {
                mbTcpOp = op;
            }

            if (mbTcpOp != null)
            {
                for (int i = 0; i < mbTcpOp.RegCount; i++)
                {
                    ushort currentAddr = (ushort)(mbTcpOp.StartAddr + i);
                    ModbusTcpConfigItem mbTcpCfgItem = GetModbusTcpConfigItemByRegAddr(currentAddr);
                    if (mbTcpCfgItem != null)
                    {
                        ModbusTcpDataEntity dataEntity = new ModbusTcpDataEntity();
                        dataEntity.RID         = Guid.NewGuid().ToString();
                        dataEntity.Device_Addr = mbTcpCfgItem.DeviceAddr.ToString();
                        dataEntity.Station     = m_config.ServerName;
                        dataEntity.Sensor_Type = "-1";
                        dataEntity.Sensor_Name = mbTcpCfgItem.Name;
                        dataEntity.Ori_Value   = mbTcpResult.ResultData[i];
                        dataEntity.Trans_Value = dataEntity.Ori_Value * mbTcpCfgItem.Multiplier;
                        dataEntity.Trans_Unit  = mbTcpCfgItem.Unit;
                        dataEntity.DataAcqTime = mbTcpResult.DataAcqTime;

                        mbTcpDataList.Add(dataEntity);
                    }
                }
            }

            return(mbTcpDataList);
        }
Exemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            Socket clientSocket = null;

            try
            {
                DisplayMessage("");
                DisplayMessage("正在接收信息...");

                int         byteRead = -1;
                StateObject stateObj = (StateObject)ar.AsyncState;
                clientSocket = stateObj.ClientSocket;
                if (clientSocket != null)
                {
                    byteRead = clientSocket.EndReceive(ar);
                }
                else
                {
                    DisplayMessage("客户端连接无效");
                }

                if (byteRead > 0)
                {
                    string msg = Encoding.Default.GetString(stateObj.Buffer, 0, byteRead);

                    DisplayMessage(string.Format("接收到来自[{0}:{1}]的数据:\r\n{2}", m_serverIP, m_serverPort, msg));

                    //解析接收到的数据信息
                    Console.WriteLine(string.Format("Bytes: {0}", BitConverter.ToString(stateObj.Buffer)));
                    Console.WriteLine("");

                    byte[] identifier = new byte[2];
                    identifier[0] = stateObj.Buffer[1];
                    identifier[1] = stateObj.Buffer[0];

                    byte[] protocol = new byte[2];
                    protocol[0] = stateObj.Buffer[3];
                    protocol[1] = stateObj.Buffer[2];

                    byte[] length = new byte[2];
                    length[0] = stateObj.Buffer[5];
                    length[1] = stateObj.Buffer[4];

                    byte devAddr  = stateObj.Buffer[6];
                    byte funcCode = stateObj.Buffer[7];

                    byte datalen = stateObj.Buffer[8];

                    ushort[] values = new ushort[datalen / 2];

                    int dataIdx = 9;

                    string sValue = string.Empty;

                    for (int i = 0; i < datalen; i += 2)
                    {
                        byte[] value = new byte[2];
                        value[0] = stateObj.Buffer[dataIdx + 1];
                        value[1] = stateObj.Buffer[dataIdx];

                        values[i / 2] = BitConverter.ToUInt16(value, 0);

                        sValue += string.Format("Value: {0}\r\n", values[i / 2]);

                        dataIdx += 2;
                    }

                    DisplayMessage(string.Format("Identifier: {0}, Data Length: {1}\r\n{2}", BitConverter.ToUInt16(identifier, 0), datalen, sValue));


                    //返回结果 -> 对应请求 -> 寄存器与值配对
                    ModbusTcpResult mbTcpResult = new ModbusTcpResult();
                    mbTcpResult.Identifier       = BitConverter.ToUInt16(identifier, 0);
                    mbTcpResult.Protocol         = BitConverter.ToUInt16(protocol, 0);
                    mbTcpResult.Length           = BitConverter.ToUInt16(length, 0);
                    mbTcpResult.DeviceAddr       = devAddr;
                    mbTcpResult.FunctionCode     = funcCode;
                    mbTcpResult.ResultDataLength = datalen;
                    mbTcpResult.ResultData       = values;
                    mbTcpResult.DataAcqTime      = DateTime.Now;
                    //
                    List <ModbusTcpDataEntity> mbTcpDataEntities = GetModbusTcpData(mbTcpResult);


                    //clientSocket.BeginReceive(stateObj.Buffer, 0, StateObject.BufferSize,
                    //    0, new AsyncCallback(ReceiveCallback), stateObj);
                }
            }
            catch (SocketException se)
            {
                DisplayMessage(string.Format("套接字连接出现异常:\r\n{0},\r\n错误代码:{1}", se.Message, se.SocketErrorCode));
                try
                {
                    if (clientSocket.Connected)
                    {
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Disconnect(false);
                    }
                    clientSocket.Close();
                }
                catch (Exception)
                {
                }
                return;
            }
            catch (Exception e)
            {
                DisplayMessage(string.Format("连接出现异常:{0}", e.Message));
                try
                {
                    if (clientSocket.Connected)
                    {
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Disconnect(false);
                    }
                    clientSocket.Close();
                }
                catch (Exception)
                {
                }
                return;
            }
        }