示例#1
0
 public ModbusTCPReader(IDataServer server, short id, string name)
 {
     _id       = id;
     _name     = name;
     _server   = server;
     modbusNet = new ModbusTcpNet();
 }
示例#2
0
        public void BuildReadRegisterCommandTest2( )
        {
            ModbusTcpNet modbusTcp = new ModbusTcpNet("127.0.0.1");

            OperateResult <byte[]> command = modbusTcp.BuildReadRegisterCommand("x=4;s=2;123", 10);

            Assert.IsTrue(command.IsSuccess, "command create failed");

            if (command.Content[2] == 0x00 &&
                command.Content[3] == 0x00 &&
                command.Content[4] == 0x00 &&
                command.Content[5] == 0x06 &&
                command.Content[6] == 0x02 &&
                command.Content[7] == 0x04 &&
                command.Content[8] == 0x00 &&
                command.Content[9] == 0x7B &&
                command.Content[10] == 0x00 &&
                command.Content[11] == 0x0A)
            {
            }
            else
            {
                Assert.Fail("command check failed : " + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(command.Content, ' '));
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show("端口输入不正确!");
                return;
            }

            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("站号输入不正确!");
                return;
            }

            busTcpClient        = new ModbusTcpNet(textBox1.Text, port, station);
            busTcpClient.LogNet = logNet;
            busTcpClient.AddressStartWithZero = checkBox1.Checked;

            try
            {
                busTcpClient.ConnectionId = textBox1.Text; // 设置唯一的ID
                NetworkAlienStart(port);
                busTcpClient.ConnectServer(null);          // 切换为异形客户端,并等待服务器的连接。


                MessageBox.Show("等待服务器的连接!");
                button1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#4
0
        public void BuildWriteRegisterCommandTest( )
        {
            ModbusTcpNet modbusTcp = new ModbusTcpNet("127.0.0.1");

            OperateResult <byte[]> command = modbusTcp.BuildWriteRegisterCommand("s=2;123", new byte[] { 0x12, 0x34, 0x56, 0x78 });

            Assert.IsTrue(command.IsSuccess, "command create failed");

            if (command.Content[2] == 0x00 &&
                command.Content[3] == 0x00 &&
                command.Content[4] == 0x00 &&
                command.Content[5] == 0x0B &&
                command.Content[6] == 0x02 &&
                command.Content[7] == 0x10 &&
                command.Content[8] == 0x00 &&
                command.Content[9] == 0x7B &&
                command.Content[10] == 0x00 &&
                command.Content[11] == 0x02 &&
                command.Content[12] == 0x04 &&
                command.Content[13] == 0x12 &&
                command.Content[14] == 0x34 &&
                command.Content[15] == 0x56 &&
                command.Content[16] == 0x78)
            {
            }
            else
            {
                Assert.Fail("command check failed : " + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(command.Content, ' '));
            }
        }
示例#5
0
        public void BuildWriteCoilCommandTest( )
        {
            ModbusTcpNet modbusTcp = new ModbusTcpNet("127.0.0.1");

            OperateResult <byte[]> command = modbusTcp.BuildWriteCoilCommand("s=2;123", new bool[] { true, false, false, true });

            Assert.IsTrue(command.IsSuccess, "command create failed");

            if (command.Content[2] == 0x00 &&
                command.Content[3] == 0x00 &&
                command.Content[4] == 0x00 &&
                command.Content[5] == 0x08 &&
                command.Content[6] == 0x02 &&
                command.Content[7] == 0x0F &&
                command.Content[8] == 0x00 &&
                command.Content[9] == 0x7B &&
                command.Content[10] == 0x00 &&
                command.Content[11] == 0x04 &&
                command.Content[12] == 0x01 &&
                command.Content[13] == 0x09)
            {
            }
            else
            {
                Assert.Fail("command check failed : " + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(command.Content, ' '));
            }
        }
示例#6
0
        private void PressureTest()
        {
            DateTime dateStart = DateTime.Now;

            ModbusTcpNet[] clients = new ModbusTcpNet[1000];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = new ModbusTcpNet(textBox1.Text, int.Parse(textBox2.Text));
                clients[i].ConnectServer( );
            }

            while (true)
            {
                if ((DateTime.Now - dateStart).TotalSeconds > 60)
                {
                    break;
                }

                for (int i = 0; i < clients.Length; i++)
                {
                    clients[i].Write((i * 4).ToString( ), (i * 4));
                }

                System.Threading.Thread.Sleep(100);
            }
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i].ConnectClose( );
            }

            BeginInvoke(new Action(() =>
            {
                button3.Enabled = true;
            }));
        }
        public void Connection()
        {
            try
            {
                busTcpClient?.ConnectClose();
                busTcpClient = new ModbusTcpNet(IP, Port, Station);
                busTcpClient.AddressStartWithZero = true;


                busTcpClient.IsStringReverse = false;

                try
                {
                    OperateResult connect = busTcpClient.ConnectServer();
                    if (connect.IsSuccess)
                    {
                        IsConnected = true;
                    }
                    else
                    {
                        IsConnected = false;
                    }
                }
                catch (Exception ex)
                {
                    EventscadaException?.Invoke(this.GetType().Name, ex.Message);
                }
            }
            catch (SocketException ex)
            {
                EventscadaException?.Invoke(this.GetType().Name, ex.Message);
            }
        }
        public void BuildWriteOneCoilCommandTest1( )
        {
            ModbusTcpNet modbusTcp = new ModbusTcpNet("127.0.0.1");

            OperateResult <byte[]> command = modbusTcp.BuildWriteOneCoilCommand("123", true);

            Assert.IsTrue(command.IsSuccess, "command create failed");

            if (command.Content[2] == 0x00 &&
                command.Content[3] == 0x00 &&
                command.Content[4] == 0x00 &&
                command.Content[5] == 0x06 &&
                command.Content[6] == 0x01 &&
                command.Content[7] == 0x05 &&
                command.Content[8] == 0x00 &&
                command.Content[9] == 0x7B &&
                command.Content[10] == 0xFF &&
                command.Content[11] == 0x00)
            {
            }
            else
            {
                Assert.Fail("command check failed : " + Communication.BasicFramework.SoftBasic.ByteToHexString(command.Content, ' '));
            }
        }
示例#9
0
        private void WriteExample( )
        {
            #region WriteExample1

            ModbusTcpNet modbus = new ModbusTcpNet("192.168.0.1");     // 实例化

            // 假设100存储了short的报警,101,102存储了float的温度,103,104存储了int的产量
            byte[] buffer = new byte[10];
            modbus.ByteTransform.TransByte((short)1).CopyTo(buffer, 0);
            modbus.ByteTransform.TransByte(123.456f).CopyTo(buffer, 2);
            modbus.ByteTransform.TransByte(45678922).CopyTo(buffer, 6);

            OperateResult write = modbus.Write("100", buffer);
            if (write.IsSuccess)
            {
                // success
            }
            else
            {
                // failed
            }


            #endregion
        }
示例#10
0
        public ModbusTest( )
        {
            tcpServer = new ModbusTcpServer( );
            tcpServer.ServerStart(502);

            modbusTcp = new ModbusTcpNet("127.0.0.1");
            modbusTcp.ConnectServer( );
        }
示例#11
0
 private void ReadRequest(ModbusTcpNet modbusTcp, List <DeviceRequest> requests)
 {
     for (int i = 0; i < requests.Count; i++)
     {
         OperateResult <byte[]> read = modbusTcp.Read(requests[i].Address, requests[i].Length);
         if (read.IsSuccess)
         {
         }
     }
 }
示例#12
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show(DemoUtils.IpAddressInputWrong);
                return;
            }


            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("Station input is wrong!");
                return;
            }

            busTcpClient?.ConnectClose( );
            busTcpClient = new ModbusTcpNet(textBox1.Text, port, station);
            busTcpClient.AddressStartWithZero = checkBox1.Checked;

            busTcpClient.SetLoginAccount(textBox14.Text, textBox12.Text);

            ComboBox1_SelectedIndexChanged(null, new EventArgs( ));                // 设置数据服务
            busTcpClient.IsStringReverse = checkBox3.Checked;

            try
            {
                OperateResult connect = busTcpClient.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;

                    userControlReadWriteOp1.SetReadWriteNet(busTcpClient, "100", false);
                }
                else
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message + Environment.NewLine +
                                    "Error: " + connect.ErrorCode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#13
0
文件: DevicePlc.cs 项目: Gz1d/Gz
        /// <summary>
        /// 停止
        /// </summary>
        public override void DoStop()
        {
            try
            {
                this.isClose = true;
                foreach (LD.Config.PlcTypeItem item in this.config.PlcTypeItems)
                {
                    if (!item.IsActive || !item.IsConnected)
                    {
                        continue;
                    }

                    switch (item.DevType)
                    {
                    case Common.DeviceType.S1200:
                    case Common.DeviceType.S1500:
                    case Common.DeviceType.S200:
                    case Common.DeviceType.S200Smart:
                    case Common.DeviceType.S300:
                    case Common.DeviceType.S400:
                        SiemensS7Net plc = (SiemensS7Net)item.Tag;
                        plc.ConnectClose();
                        break;

                    case Common.DeviceType.ModbusTcp:
                        ModbusTcpNet mdTcp = (ModbusTcpNet)item.Tag;
                        mdTcp.ConnectClose();
                        break;

                    case Common.DeviceType.ModbusRtu:
                        ModbusRtu mdRtu = (ModbusRtu)item.Tag;
                        mdRtu.Close();
                        break;

                    default:
                        break;
                    }

                    item.IsConnected = false;
                }

                foreach (LD.Config.PlcDataItem plc in this.config.PlcDataItems)
                {
                    plc.PropertyChanged -= new PropertyChangedEventHandler(plc_PropertyChanged);
                    plc.PropertyChanged -= new PropertyChangedEventHandler(Ui.frmSiemens.Instance.plc_PropertyChanged);
                }

                this.PlcValueChanged -= new LD.Config.PlcValueChangeEventHandler(PlcClient_PlcValueChanged);
            }
            catch (Exception ex)
            {
                throw new StopException(this.ToString(), ex.ToString());
            }
        }
示例#14
0
 public ModbusTcpSystem(string name, string ipAddress, int port = 502, byte station = 1) : base(name)
 {
     modusTcp          = new ModbusTcpNet(ipAddress, port, station);
     startInputsAdress = new ModbusAdress()
     {
         SignalAdress = "100"
     };
     startInputsAdress = new ModbusAdress()
     {
         SignalAdress = "100"
     };
 }
示例#15
0
        public bool Connection()
        {
            if (!System.Net.IPAddress.TryParse(IP, out System.Net.IPAddress address))
            {
                EventscadaException?.Invoke(GetType().Name, DemoUtils.IpAddressInputWrong);
                return(false);
            }

            if (!int.TryParse($"{Port}", out int port))
            {
                EventscadaException?.Invoke(GetType().Name, DemoUtils.PortInputWrong);
                return(false);
            }



            try
            {
                busTcpClient?.ConnectClose();
                busTcpClient = new ModbusTcpNet(IP, Port, Station)
                {
                    AddressStartWithZero = true,


                    IsStringReverse = false
                };

                try
                {
                    OperateResult connect = busTcpClient.ConnectServer();
                    if (connect.IsSuccess)
                    {
                        EventscadaException?.Invoke(GetType().Name, StringResources.Language.ConnectedSuccess);
                        IsConnected = true;
                    }
                    else
                    {
                        EventscadaException?.Invoke(GetType().Name, StringResources.Language.ConnectedFailed);
                    }
                    return(IsConnected);
                }
                catch (Exception ex)
                {
                    EventscadaException?.Invoke(GetType().Name, ex.Message);
                    return(IsConnected);
                }
            }
            catch (Exception ex)
            {
                EventscadaException?.Invoke(GetType().Name, ex.Message);
                return(IsConnected);
            }
        }
        public void IpAddressExample( )
        {
            #region IpAddressExample

            ModbusTcpNet modbus = new ModbusTcpNet("192.168.0.100");
            // 读取线圈100的值
            bool coil_ip100 = modbus.ReadCoil("100").Content;
            // 切换ip地址
            modbus.IpAddress = "192.168.0.101";
            bool coil_ip101 = modbus.ReadCoil("100").Content;

            #endregion
        }
示例#17
0
        // 写设备的值
        public bool  WriteValue(Common.PlcDevice plcDevice, object value)
        {
            bool IsOk = false;

            try
            {
                Config.PlcDataItem devData = this.GetPlcDataItem(plcDevice);
                Config.PlcTypeItem devType = this.GetPlcTypeItem(devData.DeviceName);

                switch (devType.DevType)
                {
                //Keyence
                case Common.DeviceType.Keyence:
                    KeyenceMcNet kPlcClient = (KeyenceMcNet)devType.Tag;
                    IsOk = this.WriteSieTcpValue(devType, devData, kPlcClient, value);
                    break;

                //Melsec
                case Common.DeviceType.Qseries:
                    MelsecMcNet mPlcClient = (MelsecMcNet)devType.Tag;
                    IsOk = this.WriteSieTcpValue(devType, devData, mPlcClient, value);
                    break;


                case Common.DeviceType.S1200:
                case Common.DeviceType.S300:
                case Common.DeviceType.S400:
                case Common.DeviceType.S1500:
                case Common.DeviceType.S200Smart:
                case Common.DeviceType.S200:
                    SiemensS7Net sPlcClient = (SiemensS7Net)devType.Tag;
                    IsOk = this.WriteSieTcpValue(devType, devData, sPlcClient, value);
                    break;

                case Common.DeviceType.ModbusTcp:
                    ModbusTcpNet mTcpClient = (ModbusTcpNet)devType.Tag;
                    IsOk = this.WriteModTcpValue(devType, devData, mTcpClient, value);
                    break;

                case Common.DeviceType.ModbusRtu:
                    ModbusRtu mRtuClinet = (ModbusRtu)devType.Tag;
                    IsOk = this.WriteModRtuValue(devType, devData, mRtuClinet, value);
                    break;

                default:
                    break;
                }
            }
            catch { }
            return(IsOk);
        }
示例#18
0
        private void connect_btn_Click(object sender, EventArgs e)//modbustcp连接
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(IpAddress.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show(DemoUtils.IpAddressInputWrong);
                return;
            }


            if (!int.TryParse(PortNum.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }


            if (!byte.TryParse(stationNum.Text, out byte station))
            {
                MessageBox.Show("Station input is wrong!");
                return;
            }

            DELTARobotClient?.ConnectClose();
            DELTARobotClient = new ModbusTcpNet(IpAddress.Text, port, station);
            DELTARobotClient.AddressStartWithZero = checkBox1.Checked;



            comboBox1_SelectedIndexChanged(null, new EventArgs());  // 设置数据服务
            DELTARobotClient.IsStringReverse = checkBox3.Checked;

            try
            {
                OperateResult connect = DELTARobotClient.ConnectServer();
                if (connect.IsSuccess)
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                    disconnect_btn.Enabled = true;
                    connect_btn.Enabled    = false;
                }
                else
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#19
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show("Ip地址输入不正确!");
                return;
            }


            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show("端口输入不正确!");
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("站号输入不正确!");
                return;
            }

            busTcpClient?.ConnectClose( );
            busTcpClient = new ModbusTcpNet(textBox1.Text, port, station);
            busTcpClient.AddressStartWithZero = checkBox1.Checked;

            ComboBox1_SelectedIndexChanged(null, new EventArgs( ));    // 设置数据服务
            busTcpClient.IsStringReverse = checkBox3.Checked;

            try
            {
                OperateResult connect = busTcpClient.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show("连接成功!");
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;
                }
                else
                {
                    MessageBox.Show("连接失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#20
0
        public bool Connection()
        {
            busTcpClient?.ConnectClose();
            busTcpClient = new ModbusTcpNet(adds, port, 1);
            busTcpClient.AddressStartWithZero = false;
            OperateResult connect = busTcpClient.ConnectServer();

            if (connect.IsSuccess)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#21
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show("Ip地址输入不正确!");
                return;
            }


            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show("端口输入不正确!");
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("站号输入不正确!");
                return;
            }

            busTcpClient?.ConnectClose( );
            busTcpClient = new ModbusTcpNet(textBox1.Text, port, station);

            try
            {
                OperateResult connect = busTcpClient.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show("连接成功!");
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;
                }
                else
                {
                    MessageBox.Show("连接失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#22
0
        public ApcmDeviceApi(string serviceName, string ipAddress, int port)
        {
            _serviceName = serviceName;
            _ipAddress   = ipAddress;
            _port        = port;

            _deviceApi = new ModbusTcpNet(_ipAddress, _port);
            if ((_deviceApi != null) && (_deviceApi.ConnectServer().IsSuccess))
            {
                Logger.Main.Info($"[{_serviceName}]初始化设备成功: {_ipAddress}:{_port}");
                ConnectStatus = true;
            }
            else
            {
                Logger.Main.Info($"[{_serviceName}]初始化设备失败: {_ipAddress}:{_port}");
            }
        }
示例#23
0
        /// <summary>
        /// 默认的构造方法
        /// </summary>
        /// <param name="element"></param>
        public DeviceModbusTcp(XElement element)
        {
            NodeModbusTcpClient modbusTcpClient = new NodeModbusTcpClient( );

            modbusTcpClient.LoadByXmlElement(element);

            LoadRequest(element);

            modbusTcp = new ModbusTcpNet(modbusTcpClient.IpAddress, modbusTcpClient.Port, modbusTcpClient.Station);
            modbusTcp.AddressStartWithZero = modbusTcpClient.IsAddressStartWithZero;
            modbusTcp.ConnectTimeOut       = modbusTcpClient.ConnectTimeOut;

            ByteTransform = modbusTcp.ByteTransform;
            UniqueId      = modbusTcp.ConnectionId;

            TypeName = "Modbus-Tcp设备";
        }
示例#24
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //MelsecTest( );

            ModbusTcpNet modbusTcp = new ModbusTcpNet("127.0.0.1");

            //modbusTcp.ConnectServer( );

            Console.WriteLine(modbusTcp.ReadInt16("100").Content);

            Console.WriteLine(HslCommunication.BasicFramework.SoftBasic.ByteToHexString(modbusTcp.Read("100", 200).Content, ' '));

            //modbusTcp.ConnectClose( );
            Console.ReadLine( );
        }
示例#25
0
        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <param name="IPAddress"></param>
        public bool connectPLC(string IPAddress)
        {
            busTcpClient = new ModbusTcpNet(IPAddress);   // 端口号502,站号1
            OperateResult write = busTcpClient.ConnectServer();

            if (write.IsSuccess)
            {
                MessageBox.Show("连接PLC成功!!!");
                //NetLog.WriteTextLog("连接PLC成功!!!");
                return(true);
            }
            else
            {
                MessageBox.Show(write.ToMessageShowString());
                //NetLog.WriteTextLog("连接PLC失败!!!" + write.ToMessageShowString());
                return(false);
            }
        }
        /// <summary>
        /// 默认的构造方法
        /// </summary>
        /// <param name="element"></param>
        public DeviceModbusTcpAlien(XElement element)
        {
            NodeModbusTcpAline modbusTcpAline = new NodeModbusTcpAline( );

            modbusTcpAline.LoadByXmlElement(element);

            LoadRequest(element);

            modbusTcp = new ModbusTcpNet(string.Empty, 502, modbusTcpAline.Station);
            modbusTcp.AddressStartWithZero = modbusTcpAline.IsAddressStartWithZero;
            modbusTcp.ConnectionId         = modbusTcpAline.DTU;


            ByteTransform = modbusTcp.ByteTransform;
            UniqueId      = modbusTcp.ConnectionId;

            TypeName = "Modbus-Tcp异形设备";
        }
示例#27
0
        /// <summary>
        /// 实例化一个Modbus-Tcp的设备对象,从配置信息创建
        /// </summary>
        /// <param name="element">配置信息</param>
        public DeviceModbusTcp(XElement element)
        {
            NodeModbusTcpClient nodeModbus = new NodeModbusTcpClient( );

            LoadRequest(element);

            modbusTcp = new ModbusTcpNet(nodeModbus.IpAddress, nodeModbus.Port, nodeModbus.Station);
            modbusTcp.AddressStartWithZero = nodeModbus.IsAddressStartWithZero;
            modbusTcp.ConnectTimeOut       = nodeModbus.ConnectTimeOut;
            modbusTcp.IsMultiWordReverse   = nodeModbus.IsWordReverse;
            modbusTcp.IsStringReverse      = nodeModbus.IsStringReverse;

            ByteTransform   = modbusTcp.ByteTransform;
            UniqueId        = modbusTcp.ConnectionId;
            ReadWriteDevice = modbusTcp;

            TypeName = "Modbus-Tcp设备";
        }
示例#28
0
文件: DevicePlc.cs 项目: Gz1d/Gz
        //MudbusTCP设备初始化
        void DoInitMudbusTcp(LD.Config.PlcTypeItem item)
        {
            // 连接
            byte station;

            if (!byte.TryParse(item.Station, out station))
            {
                MessageBox.Show(string.Format("station {0} input is wrong!", item.Station));
                return;
            }
            System.Net.IPAddress address;
            if (!System.Net.IPAddress.TryParse(item.IP, out address))
            {
                MessageBox.Show(string.Format("IP {0} input wrong!", item.IP));
                return;
            }
            int port;

            if (!int.TryParse(item.Port.ToString(), out port))
            {
                MessageBox.Show(string.Format("port {0} input wrong!", item.Port));
                return;
            }
            ModbusTcpNet busTcpClient = new ModbusTcpNet(item.IP, port, station);

            busTcpClient.AddressStartWithZero = item.AddressStartWithZero;
            if (busTcpClient != null)
            {
                busTcpClient.DataFormat = item.DataFormat;
            }
            busTcpClient.IsStringReverse = item.IsStringReverse;
            item.Tag = busTcpClient;
            OperateResult connect = busTcpClient.ConnectServer();

            if (connect.IsSuccess)
            {
                item.IsConnected = true;
                //Plcs.Add(item.DevName, busTcpClient);
            }
            else
            {
                item.IsConnected = false;
            }
        }
示例#29
0
        /// <summary>
        /// 实例化一个Modbus-Tcp的设备对象,从配置信息创建
        /// </summary>
        /// <param name="element">配置信息</param>
        public DeviceModbusTcp(XElement element)
        {
            NodeModbusTcpClient nodeModbus = new NodeModbusTcpClient( );

            nodeModbus.LoadByXmlElement(element);
            LoadRequest(element);

            modbusTcp = new ModbusTcpNet(nodeModbus.IpAddress, nodeModbus.Port, nodeModbus.Station);
            modbusTcp.AddressStartWithZero = nodeModbus.IsAddressStartWithZero;
            modbusTcp.ConnectTimeOut       = nodeModbus.ConnectTimeOut;
            modbusTcp.DataFormat           = (HslCommunication.Core.DataFormat)nodeModbus.DataFormat;
            modbusTcp.IsStringReverse      = nodeModbus.IsStringReverse;

            ByteTransform   = modbusTcp.ByteTransform;
            UniqueId        = modbusTcp.ConnectionId;
            ReadWriteDevice = modbusTcp;

            TypeName = "Modbus-Tcp设备";
        }
示例#30
0
        // 读设备的值
        public object ReadValue(string itemName)
        {
            //try
            //{
            Config.PlcDataItem devData = this.GetPlcDataItem(itemName);
            Config.PlcTypeItem devType = this.GetPlcTypeItem(devData.DeviceName);

            switch (devType.DevType)
            {
            case Common.DeviceType.S1200:
            case Common.DeviceType.S300:
            case Common.DeviceType.S400:
            case Common.DeviceType.S1500:
            case Common.DeviceType.S200Smart:
            case Common.DeviceType.S200:
                SiemensS7Net sPlcClient = (SiemensS7Net)devType.Tag;
                return(this.ReadSieTcpValue(devType, devData, sPlcClient));

            //break;

            case Common.DeviceType.ModbusTcp:
                ModbusTcpNet mTcpClient = (ModbusTcpNet)devType.Tag;
                return(this.ReadModTcpValue(devType, devData, mTcpClient));

            //break;

            case Common.DeviceType.ModbusRtu:
                ModbusRtu mRtuClinet = (ModbusRtu)devType.Tag;
                return(this.ReadModRtuValue(devType, devData, mRtuClinet));

            //break;

            default:
                return(null);
                //break;
            }
            //}
            //catch (Exception ex)
            //{

            //}
        }