コード例 #1
0
        private ModbusRtu modbus = new ModbusRtu( );   // 实例化



        private void CoilExample( )
        {
            // 读取线圈示例
            bool coil100 = modbus.ReadCoil("100").Content;

            // 判断是否读取成功
            OperateResult <bool> result_coil100 = modbus.ReadCoil("100");

            if (result_coil100.IsSuccess)
            {
                // success
                bool value = result_coil100.Content;
            }
            else
            {
                // failed
            }


            // 假设读取站号10的线圈100的值
            bool coil_station_ten_100 = modbus.ReadCoil("s=10;100").Content;



            // =============================================================================================
            // 写入也是同理,线圈100写通
            modbus.WriteCoil("100", true);

            // 站号10的线圈写通
            modbus.WriteCoil("s=10;100", true);

            // 想要判断是否写入成功
            if (modbus.WriteCoil("s=10;100", true).IsSuccess)
            {
                // success
            }
            else
            {
                // failed
            }



            // ===========================================================================================
            // 批量读写也是类似,批量的读取
            bool[] coil10_19 = modbus.ReadCoil("100", 10).Content;

            // 写入也是同理
            modbus.WriteCoil("100", new bool[] { true, false, true, false, false, false, true, false, false, false });


            // 离散输入的数据读取同理
        }
コード例 #2
0
 private void button24_Click(object sender, EventArgs e)
 {
     // bool写入
     try
     {
         writeResultRender(busRtuClient.WriteCoil(textBox8.Text, bool.Parse(textBox7.Text)), textBox8.Text);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
コード例 #3
0
ファイル: testNetRs232.cs プロジェクト: windygu/modbus
        /// <summary>
        /// D0置零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_D0Clear_Click(object sender, EventArgs e)
        {
            try
            {
                writeResultRender(busRtuClient.WriteCoil(txt_D0ClearAddress.Text, true), txt_D0ClearAddress.Text);

                writeResultRender(busRtuClient.WriteCoil(txt_D0ClearAddress.Text, false), txt_D0ClearAddress.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #4
0
        public override Result WriteCoil(string address, bool[] values)
        {
            var op = modbus.WriteCoil(address, values);

            return(new Result {
                IsSuccess = op.IsSuccess, Msg = op.Message
            });
        }
コード例 #5
0
        public void write_coil(string adreess, bool val, byte station)
        {
            busRtuClient.Station = station;
            //写入线圈
            OperateResult result = busRtuClient.WriteCoil(adreess, val);

            if (!result.IsSuccess)
            {
                MessageBox.Show("线圈写入失败");
            }
        }
コード例 #6
0
        public bool Write(string address, dynamic value)
        {
            if (value is bool)
            {
                busRtuClient.WriteCoil(address, value);
            }
            else
            {
                busRtuClient.Write(address, value);
            }

            return(true);
        }
コード例 #7
0
        public void ModbusRtuUnitTest( )
        {
            ModbusRtu modbus = new ModbusRtu(1);

            modbus.SerialPortInni("COM2", 9600);

            try
            {
                modbus.Open( );
            }
            catch
            {
                Console.WriteLine("无法连接modbus,将跳过单元测试。等待网络正常时,再进行测试");
                return;
            }

            // 开始单元测试,从coil类型开始测试
            string address = "1200";

            bool[] boolTmp = new bool[] { true, true, false, true, false, true, false };
            Assert.IsTrue(modbus.WriteCoil(address, true).IsSuccess);
            Assert.IsTrue(modbus.ReadCoil(address).Content == true);
            Assert.IsTrue(modbus.WriteCoil(address, boolTmp).IsSuccess);
            bool[] readBool = modbus.ReadCoil(address, (ushort)boolTmp.Length).Content;
            for (int i = 0; i < boolTmp.Length; i++)
            {
                Assert.IsTrue(readBool[i] == boolTmp[i]);
            }

            address = "300";
            // short类型
            Assert.IsTrue(modbus.Write(address, (short)12345).IsSuccess);
            Assert.IsTrue(modbus.ReadInt16(address).Content == 12345);
            short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 };
            Assert.IsTrue(modbus.Write(address, shortTmp).IsSuccess);
            short[] readShort = modbus.ReadInt16(address, (ushort)shortTmp.Length).Content;
            for (int i = 0; i < readShort.Length; i++)
            {
                Assert.IsTrue(readShort[i] == shortTmp[i]);
            }

            // ushort类型
            Assert.IsTrue(modbus.Write(address, (ushort)51234).IsSuccess);
            Assert.IsTrue(modbus.ReadUInt16(address).Content == 51234);
            ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 };
            Assert.IsTrue(modbus.Write(address, ushortTmp).IsSuccess);
            ushort[] readUShort = modbus.ReadUInt16(address, (ushort)ushortTmp.Length).Content;
            for (int i = 0; i < ushortTmp.Length; i++)
            {
                Assert.IsTrue(readUShort[i] == ushortTmp[i]);
            }

            // int类型
            Assert.IsTrue(modbus.Write(address, 12342323).IsSuccess);
            Assert.IsTrue(modbus.ReadInt32(address).Content == 12342323);
            int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 };
            Assert.IsTrue(modbus.Write(address, intTmp).IsSuccess);
            int[] readint = modbus.ReadInt32(address, (ushort)intTmp.Length).Content;
            for (int i = 0; i < intTmp.Length; i++)
            {
                Assert.IsTrue(readint[i] == intTmp[i]);
            }

            // uint类型
            Assert.IsTrue(modbus.Write(address, (uint)416123237).IsSuccess);
            Assert.IsTrue(modbus.ReadUInt32(address).Content == (uint)416123237);
            uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 };
            Assert.IsTrue(modbus.Write(address, uintTmp).IsSuccess);
            uint[] readuint = modbus.ReadUInt32(address, (ushort)uintTmp.Length).Content;
            for (int i = 0; i < uintTmp.Length; i++)
            {
                Assert.IsTrue(readuint[i] == uintTmp[i]);
            }

            // float类型
            Assert.IsTrue(modbus.Write(address, 123.45f).IsSuccess);
            Assert.IsTrue(modbus.ReadFloat(address).Content == 123.45f);
            float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f };
            Assert.IsTrue(modbus.Write(address, floatTmp).IsSuccess);
            float[] readFloat = modbus.ReadFloat(address, (ushort)floatTmp.Length).Content;
            for (int i = 0; i < readFloat.Length; i++)
            {
                Assert.IsTrue(floatTmp[i] == readFloat[i]);
            }

            // double类型
            Assert.IsTrue(modbus.Write(address, 1234.5434d).IsSuccess);
            Assert.IsTrue(modbus.ReadDouble(address).Content == 1234.5434d);
            double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d };
            Assert.IsTrue(modbus.Write(address, doubleTmp).IsSuccess);
            double[] readDouble = modbus.ReadDouble(address, (ushort)doubleTmp.Length).Content;
            for (int i = 0; i < doubleTmp.Length; i++)
            {
                Assert.IsTrue(readDouble[i] == doubleTmp[i]);
            }

            // long类型
            Assert.IsTrue(modbus.Write(address, 123617231235123L).IsSuccess);
            Assert.IsTrue(modbus.ReadInt64(address).Content == 123617231235123L);
            long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 };
            Assert.IsTrue(modbus.Write(address, longTmp).IsSuccess);
            long[] readLong = modbus.ReadInt64(address, (ushort)longTmp.Length).Content;
            for (int i = 0; i < longTmp.Length; i++)
            {
                Assert.IsTrue(readLong[i] == longTmp[i]);
            }

            // ulong类型
            Assert.IsTrue(modbus.Write(address, 1283823681236123UL).IsSuccess);
            Assert.IsTrue(modbus.ReadUInt64(address).Content == 1283823681236123UL);
            ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL };
            Assert.IsTrue(modbus.Write(address, ulongTmp).IsSuccess);
            ulong[] readULong = modbus.ReadUInt64(address, (ushort)ulongTmp.Length).Content;
            for (int i = 0; i < readULong.Length; i++)
            {
                Assert.IsTrue(readULong[i] == ulongTmp[i]);
            }

            // string类型
            Assert.IsTrue(modbus.Write(address, "123123").IsSuccess);
            Assert.IsTrue(modbus.ReadString(address, 3).Content == "123123");

            // byte类型
            byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 };
            Assert.IsTrue(modbus.Write(address, byteTmp).IsSuccess);
            Assert.IsTrue(SoftBasic.IsTwoBytesEquel(modbus.Read(address, 3).Content, byteTmp));

            modbus.Close( );
        }
コード例 #8
0
        public override bool WriteTagToRealDevice(Tag tag, object value)
        {
            lock (this)
            {
                OperateResult res = new OperateResult();
                try
                {
                    var address = tag.Address.ToLower();

                    if (address.Contains("i"))
                    {
                        throw new Exception("Error tag address.Can not write this tag");
                    }

                    string station = "";
                    if (address.StartsWith("s") && address.Contains(";"))
                    {
                        var ads = address.Split(';');
                        station = ads[0];
                        address = ads[1];
                    }


                    string reg = "";
                    if (address.StartsWith("di") || address.StartsWith("do") || address.StartsWith("ai") || address.StartsWith("ao"))
                    {
                        reg     = address.Substring(0, 2);
                        address = address.Replace(reg, "");
                    }
                    else
                    {
                        throw new Exception("Error tag address.");
                    }


                    if (!string.IsNullOrEmpty(station))
                    {
                        address = station + ";" + address;
                    }

                    switch (tag.TagType)
                    {
                    case "bool":
                        if (reg == "do")
                        {
                            res = _modbusDevice.WriteCoil(address, (bool)value);
                        }
                        break;

                    case "string":
                        res = _modbusDevice.Write(address.Split('.')[0], ConvertUtils.GetBytes(tag, value));
                        break;

                    default:
                        res = _modbusDevice.Write(address, ConvertUtils.GetBytes(tag, value).Reverse().ToArray());
                        break;
                    }
                    LOG.Info($"Datasource[{SourceName}] Write tag. Tag[{tag.TagName}] Address[{tag.Address}] IsSuccess[{res.IsSuccess}]");
                }
                catch (Exception ex)
                {
                    LOG.Error($"Datasource[{SourceName}] write error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]");
                }
                return(res.IsSuccess);
            }
        }
コード例 #9
0
        //modbusRtu write
        private bool  WriteModRtuValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, ModbusRtu mod, object value)
        {
            HslCommunication.OperateResult rlt = new HslCommunication.OperateResult();
            rlt.IsSuccess = false;
            try
            {
                //string[] strAdrss = plcdata.Address.Split('.');
                //string Address = strAdrss[0] + "." + Regex.Replace(strAdrss[1], @"^[A-Za-z]+", string.Empty);

                if (plctype == null || !plctype.IsConnected || plcdata == null || mod == null)
                {
                    return(rlt.IsSuccess);
                }

                switch (plcdata.DataType)
                {
                case Common.DataTypes.Bool:    //Bool
                    rlt = mod.WriteCoil(plcdata.Address, Convert.ToBoolean(value));
                    break;

                case Common.DataTypes.Short:
                    rlt = mod.Write(plcdata.Address, Convert.ToInt16(value));
                    break;

                case Common.DataTypes.Ushort:
                    rlt = mod.Write(plcdata.Address, Convert.ToUInt16(value));
                    break;

                case Common.DataTypes.Int:
                    rlt = mod.Write(plcdata.Address, Convert.ToInt32(value));
                    break;

                case Common.DataTypes.UInt:
                    rlt = mod.Write(plcdata.Address, Convert.ToUInt32(value));
                    break;

                case Common.DataTypes.Long:
                    rlt = mod.Write(plcdata.Address, Convert.ToInt64(value));
                    break;

                case Common.DataTypes.ULong:
                    rlt = mod.Write(plcdata.Address, Convert.ToUInt64(value));
                    break;

                case Common.DataTypes.Float:
                    rlt = mod.Write(plcdata.Address, float.Parse(value.ToString()));
                    break;

                case Common.DataTypes.Double:
                    rlt = mod.Write(plcdata.Address, Convert.ToDouble(value));
                    break;

                case Common.DataTypes.String:
                    rlt = mod.Write(plcdata.Address, Convert.ToString(value));
                    break;

                default: break;
                }
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
            return(rlt.IsSuccess);
        }