Пример #1
0
        public ModbusRTUResponseBody RequestModbusRTUData(ModbusRTURequestBody request)
        {
            ModbusRTUResponseBody response = new ModbusRTUResponseBody();

            response.ServerID     = request.ServerID;
            response.DeviceAddr   = request.DeviceAddr;
            response.FunctionCode = request.FunctionCode;
            response.ErrorMessage = string.Empty;
            response.ResponseTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            ModbusRtuTcpServerHost tcpServerHost = HostContainer.GetModbusRtuTcpServerHostByServerID(request.ServerID);

            if (tcpServerHost == null)
            {
                response.ErrorMessage = string.Format("数据采集服务未启动或未成功连接数据源");
                response.Status       = "0";
                return(response);
            }

            byte devAddr;

            if (!byte.TryParse(request.DeviceAddr, out devAddr))
            {
                response.ErrorMessage = string.Format("设备地址[{0}]无效", request.DeviceAddr);
                response.Status       = "0";
                return(response);
            }

            byte funcCode;

            if (!byte.TryParse(request.FunctionCode, out funcCode))
            {
                response.ErrorMessage = string.Format("功能代码[{0}]无效", request.FunctionCode);
                response.Status       = "0";
                return(response);
            }

            ushort startAddr;

            if (!ushort.TryParse(request.StartAddr, out startAddr))
            {
                response.ErrorMessage = string.Format("起始寄存器地址[{0}]无效", request.StartAddr);
                response.Status       = "0";
                return(response);
            }

            ushort regCount;

            if (!ushort.TryParse(request.RegCount, out regCount))
            {
                response.ErrorMessage = string.Format("读取寄存器数量[{0}]无效", request.RegCount);
                response.Status       = "0";
                return(response);
            }

            try
            {
                string respCRC = string.Empty;
                response.DataContent  = tcpServerHost.RequestModbusRTUData(devAddr, funcCode, startAddr, regCount, out respCRC);
                response.DataLength   = (regCount * 2).ToString();
                response.ResponseTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                response.ResponseCRC  = respCRC;
                response.Status       = "1";
            }
            catch (Exception ex)
            {
                response.DataContent  = null;
                response.DataLength   = "0";
                response.ErrorMessage = ex.Message;
                response.Status       = "0";
                response.ResponseTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }

            return(response);
        }