private void RegisterExample( ) { // 读取寄存器100的值 short register100 = modbus.ReadInt16("100").Content; // 批量读取寄存器100-109的值 short[] register100_109 = modbus.ReadInt16("100", 10).Content; // 写入寄存器100的值,注意,一定要强制转换short类型 modbus.Write("100", (short)123); // 批量写 modbus.Write("100", new short[] { 123, -123, 4244 }); // ============================================================================================== // 以下是一些常规的操作,不再对是否成功的结果进行判断 // 读取操作 bool coil100 = modbus.ReadCoil("100").Content; // 读取线圈100的通断 short short100 = modbus.ReadInt16("100").Content; // 读取寄存器100的short值 ushort ushort100 = modbus.ReadUInt16("100").Content; // 读取寄存器100的ushort值 int int100 = modbus.ReadInt32("100").Content; // 读取寄存器100-101的int值 uint uint100 = modbus.ReadUInt32("100").Content; // 读取寄存器100-101的uint值 float float100 = modbus.ReadFloat("100").Content; // 读取寄存器100-101的float值 long long100 = modbus.ReadInt64("100").Content; // 读取寄存器100-103的long值 ulong ulong100 = modbus.ReadUInt64("100").Content; // 读取寄存器100-103的ulong值 double double100 = modbus.ReadDouble("100").Content; // 读取寄存器100-103的double值 string str100 = modbus.ReadString("100", 5).Content; // 读取100到104共10个字符的字符串 // 写入操作 modbus.WriteCoil("100", true); // 写入线圈100为通 modbus.Write("100", (short)12345); // 写入寄存器100为12345 modbus.Write("100", (ushort)45678); // 写入寄存器100为45678 modbus.Write("100", 123456789); // 写入寄存器100-101为123456789 modbus.Write("100", (uint)123456778); // 写入寄存器100-101为123456778 modbus.Write("100", 123.456); // 写入寄存器100-101为123.456 modbus.Write("100", 12312312312414L); //写入寄存器100-103为一个大数据 modbus.Write("100", 12634534534543656UL); // 写入寄存器100-103为一个大数据 modbus.Write("100", 123.456d); // 写入寄存器100-103为一个双精度的数据 modbus.Write("100", "K123456789"); // =============================================================================================== // 读取输入寄存器 short input_short100 = modbus.ReadInt16("x=4;100").Content; // 读取寄存器100的short值 ushort input_ushort100 = modbus.ReadUInt16("x=4;100").Content; // 读取寄存器100的ushort值 int input_int100 = modbus.ReadInt32("x=4;100").Content; // 读取寄存器100-101的int值 uint input_uint100 = modbus.ReadUInt32("x=4;100").Content; // 读取寄存器100-101的uint值 float input_float100 = modbus.ReadFloat("x=4;100").Content; // 读取寄存器100-101的float值 long input_long100 = modbus.ReadInt64("x=4;100").Content; // 读取寄存器100-103的long值 ulong input_ulong100 = modbus.ReadUInt64("x=4;100").Content; // 读取寄存器100-103的ulong值 double input_double100 = modbus.ReadDouble("x=4;100").Content; // 读取寄存器100-103的double值 string input_str100 = modbus.ReadString("x=4;100", 5).Content; // 读取100到104共10个字符的字符串 }
private void button_read_ushort_Click(object sender, EventArgs e) { // 读取ushort变量 if (textBox1.Text == "1") { DemoUtils.ReadResultRender(busRtuClient.ReadUInt16(textBox3.Text), textBox3.Text, textBox4); } else { DemoUtils.ReadResultRender(busRtuClient.ReadUInt16(textBox3.Text, ushort.Parse(textBox1.Text)), textBox3.Text, textBox4); } }
public TValue[] Read <TValue>(string address, ushort length) { int Address = DMT.DevToAddrW("DVP", address, Station); if (typeof(TValue) == typeof(bool)) { bool[] b = busRtuClient.ReadCoil($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ushort)) { ushort[] b = busRtuClient.ReadUInt16($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(int)) { int[] b = busRtuClient.ReadInt32($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(uint)) { uint[] b = busRtuClient.ReadUInt32($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(long)) { long[] b = busRtuClient.ReadInt64($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ulong)) { ulong[] b = busRtuClient.ReadUInt64($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(short)) { short[] b = busRtuClient.ReadInt16($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(double)) { double[] b = busRtuClient.ReadDouble($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(float)) { float[] b = busRtuClient.ReadFloat($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(string)) { string b = busRtuClient.ReadString($"{Address}", length).Content; return((TValue[])(object)b); } throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue))); }
public TValue[] Read <TValue>(string address, ushort length) { if (typeof(TValue) == typeof(bool)) { var b = busRtuClient.ReadCoil(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ushort)) { var b = busRtuClient.ReadUInt16(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(int)) { var b = busRtuClient.ReadInt32(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(uint)) { var b = busRtuClient.ReadUInt32(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(long)) { var b = busRtuClient.ReadInt64(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ulong)) { var b = busRtuClient.ReadUInt64(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(short)) { var b = busRtuClient.ReadInt16(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(double)) { var b = busRtuClient.ReadDouble(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(float)) { var b = busRtuClient.ReadFloat(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(string)) { var b = busRtuClient.ReadString(address, length).Content; return((TValue[])(object)b); } throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue))); }
private void button_read_ushort_Click(object sender, EventArgs e) { // 读取ushort变量 readResultRender(busRtuClient.ReadUInt16(textBox3.Text), textBox3.Text, textBox4); }
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( ); }
//modbusRtu read private object ReadModRtuValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, ModbusRtu mod) { 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(null); } switch (plcdata.DataType) { case Common.DataTypes.Bool: //Coil plcdata.ValueNew = mod.ReadCoil(plcdata.Address).Content; break; case Common.DataTypes.Discrete: //Discrete plcdata.ValueNew = mod.ReadDiscrete(plcdata.Address).Content; break; case Common.DataTypes.Short: plcdata.ValueNew = mod.ReadInt16(plcdata.Address).Content; break; case Common.DataTypes.Ushort: plcdata.ValueNew = mod.ReadUInt16(plcdata.Address).Content; break; case Common.DataTypes.Int: plcdata.ValueNew = mod.ReadInt32(plcdata.Address).Content; break; case Common.DataTypes.UInt: plcdata.ValueNew = mod.ReadUInt32(plcdata.Address).Content; break; case Common.DataTypes.Long: plcdata.ValueNew = mod.ReadInt64(plcdata.Address).Content; break; case Common.DataTypes.ULong: plcdata.ValueNew = mod.ReadUInt64(plcdata.Address).Content; break; case Common.DataTypes.Float: plcdata.ValueNew = mod.ReadFloat(plcdata.Address).Content; break; case Common.DataTypes.Double: plcdata.ValueNew = mod.ReadDouble(plcdata.Address).Content; break; case Common.DataTypes.String: plcdata.ValueNew = mod.ReadString(plcdata.Address, 10).Content; break; default: break; } } catch { //MessageBox.Show(ex.Message); } return(plcdata.ValueNew); }