Exemplo n.º 1
0
 public void Stop()
 {
     try
     {
         ModbusTCPServer.Stop();
     }
     catch (Exception ex)
     {
         LoggingFactory.GetLogger().WriteErrorLogger(string.Format("ModbusService->Stop()异常:{0}--{1}", ex.Message, ex.InnerException));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 通过配置文件初始化ModbusTcp服务
        /// </summary>
        /// <param name="modbusConfigFile">配置文件物理路径(包含名称+后缀):例如:Config/ModbusConfig.xml</param>
        /// <returns></returns>
        private ModbusTCPServer GetModbusTCPServerFromConfigFile(string modbusConfigFile)
        {
            ModbusTCPServer modbusTCPServer = null;

            try
            {
                modbusTCPServer = new ModbusTCPServer();
                modbusTCPServer.InitializeFromConfigFile(modbusConfigFile);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().WriteErrorLogger(string.Format("GetModbusTCPServerFromConfigFile发生异常:{0}--{1}", ex.Message, ex.InnerException));
                return(modbusTCPServer);
            }

            return(modbusTCPServer);
        }
        public void GetDataPointRealValueTest()
        {
            ModbusTCPServer modbusTCPServer = new ModbusTCPServer();

            modbusTCPServer.InitializeFromConfigFile(modbusConfigFile);

            List <DataPoint> allDataPointsWhoseRealTimeDataChanged = new List <DataPoint>();

            foreach (var modbusUint in modbusTCPServer.ModbusUnits)
            {
                if (modbusUint.Connector.Connect())
                {
                    int i = 0;
                    foreach (var readRegisterCommand in modbusUint.AllReadRegisterCommands)
                    {
                        ++i;
                        var recvBytes = modbusUint.Connector.Send(readRegisterCommand.ReadCommand);
                        var sendMsg   = string.Format("{0}SendBytes:{1} ", i, readRegisterCommand.ReadCommand.ToString());

                        Debug.WriteLine(sendMsg);

                        if (recvBytes != null)
                        {
                            var recvMsg = string.Format("{0}RecvBytes:{1} ", i, recvBytes.ToString());
                            Debug.WriteLine(recvMsg);

                            AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, readRegisterCommand.ReadCommand, recvBytes);
                            if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess)
                            {
                                List <DataPoint> dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, modbusUint.AllDataPoints);
                                allDataPointsWhoseRealTimeDataChanged.AddRange(dataPointsWhoseRealTimeDataChanged);
                            }
                        }
                        else
                        {
                            Debug.WriteLine(i + ".RecvBytes: null");
                        }
                    }
                }
            }
        }
        public void SetDataPointRealValueByFunNun16Test()
        {
            ModbusTCPServer modbusTCPServer = new ModbusTCPServer();

            modbusTCPServer.InitializeFromConfigFile(modbusConfigFile);

            foreach (var modbusUint in modbusTCPServer.ModbusUnits)
            {
                List <DataPoint> dataPointsWhoseRealTimeDataChanged = new List <DataPoint>();

                List <DataPoint> dataPoints = new List <DataPoint>();

                dataPoints.Add(new DataPoint()
                {
                    DeviceAddress        = 1,
                    StartRegisterAddress = 83,
                    DataPointType        = DataPointType.WriteAndReadByFunNum03,
                    DataPointDataType    = DataPointDataType.F32,
                    RealTimeValue        = -1,
                    ValueToSet           = 101.7
                });

                List <List <byte> > writeRegisterCommandBytes = ModbusTCP.CreateWriteRegisterCommands(DataAnalyzeMode.DataHighToLow, dataPoints);

                if (modbusUint.Connector.Connect())
                {
                    foreach (var writeBytes in writeRegisterCommandBytes)
                    {
                        var recvBytes = modbusUint.Connector.Send(writeBytes);
                        if (recvBytes != null)
                        {
                            AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, writeBytes, recvBytes);
                            if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess)
                            {
                                dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, dataPoints);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            ILog log = new DefaultLog("ModbusTCPServer");
            ModbusPointMapping mapping = ModbusPointMapping.GetInstance(log);

            for (int i = 1; i < 1001; i++)
            {
                var key = string.Format("{0:D5}", 40000 + i);
                mapping.Register(key, new VirtulPoint <ushort>(key, "ushort", new ushort[] { (ushort)i }));
                var key1 = string.Format("{0:D5}", 00000 + i);
                mapping.Register(key1, new VirtulPoint <bool>(key1, "bool", new bool[] { true }));
                var key2 = string.Format("{0:D5}", 10000 + i);
                mapping.Register(key2, new VirtulPoint <bool>(key2, "bool", new bool[] { true }));
                var key3 = string.Format("{0:D5}", 30000 + i);
                mapping.Register(key3, new VirtulPoint <ushort>(key3, "ushort", new ushort[] { (ushort)(i * 2) }));
            }
            ModbusTCPServer server = new ModbusTCPServer(new EthernetSetUp("127.0.0.1", 502), new TimeOut("ModbusServer", 10000, log), log, 5, 1);

            server.Init();
            server.Start();
            Console.ReadKey();
        }
        public void SetDataPointRealValueByTest()
        {
            ModbusTCPServer modbusTCPServer = new ModbusTCPServer();

            modbusTCPServer.InitializeFromConfigFile(modbusConfigFile);

            foreach (var modbusUint in modbusTCPServer.ModbusUnits)
            {
                List <DataPoint>            allDataPointsWhoseRealTimeDataChangeds = new List <DataPoint>();
                List <WriteRegisterCommand> allWriterRegisterCommands = new List <WriteRegisterCommand>();

                List <DataPoint> canWriteDataPoints =
                    modbusUint.AllDataPoints.FindAll(p => p.DataPointType == DataPointType.WriteAndReadByFunNum01 ||
                                                     p.DataPointType == DataPointType.WriteAndReadByFunNum03);

                #region 模拟数据

                foreach (var writeDataPoint in canWriteDataPoints)
                {
                    if (writeDataPoint.DataPointType == DataPointType.WriteAndReadByFunNum01)
                    {
                        var dataPointNo = Convert.ToInt32(writeDataPoint.Number);
                        if (dataPointNo % 2 == 0)
                        {
                            writeDataPoint.ValueToSet = 1;
                        }
                        else
                        {
                            writeDataPoint.ValueToSet = 0;
                        }
                    }
                    else if (writeDataPoint.DataPointType == DataPointType.WriteAndReadByFunNum03)
                    {
                        writeDataPoint.ValueToSet = Convert.ToDouble(writeDataPoint.Number);
                    }
                }


                #endregion

                List <List <byte> > requestBytes = ModbusTCP.CreateWriteRegisterCommands(modbusUint.DataAnalyzeMode, canWriteDataPoints);

                foreach (var requestByte in requestBytes)
                {
                    WriteRegisterCommand writeRegisterCommand = new WriteRegisterCommand();
                    writeRegisterCommand.WriteCommand = requestByte;

                    allWriterRegisterCommands.Add(writeRegisterCommand);
                }

                if (modbusUint.Connector.Connect())
                {
                    int i = 0;
                    foreach (var writeRegisterCommand in allWriterRegisterCommands)
                    {
                        ++i;
                        var recvBytes = modbusUint.Connector.Send(writeRegisterCommand.WriteCommand);
                        if (recvBytes != null)
                        {
                            AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, writeRegisterCommand.WriteCommand, recvBytes);
                            if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess)
                            {
                                var dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, modbusUint.AllDataPoints);

                                allDataPointsWhoseRealTimeDataChangeds.AddRange(dataPointsWhoseRealTimeDataChanged);
                            }
                        }
                    }
                }
            }
        }