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(busAsciiClient.ReadUInt16(textBox3.Text), textBox3.Text, textBox4); } else { DemoUtils.ReadResultRender(busAsciiClient.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 = busAsciiClient.ReadCoil($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ushort)) { ushort[] b = busAsciiClient.ReadUInt16($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(int)) { int[] b = busAsciiClient.ReadInt32($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(uint)) { uint[] b = busAsciiClient.ReadUInt32($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(long)) { long[] b = busAsciiClient.ReadInt64($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ulong)) { ulong[] b = busAsciiClient.ReadUInt64($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(short)) { short[] b = busAsciiClient.ReadInt16($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(double)) { double[] b = busAsciiClient.ReadDouble($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(float)) { float[] b = busAsciiClient.ReadFloat($"{Address}", length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(string)) { string b = busAsciiClient.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 = busAsciiClient.ReadCoil(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ushort)) { var b = busAsciiClient.ReadUInt16(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(int)) { var b = busAsciiClient.ReadInt32(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(uint)) { var b = busAsciiClient.ReadUInt32(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(long)) { var b = busAsciiClient.ReadInt64(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(ulong)) { var b = busAsciiClient.ReadUInt64(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(short)) { var b = busAsciiClient.ReadInt16(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(double)) { var b = busAsciiClient.ReadDouble(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(float)) { var b = busAsciiClient.ReadFloat(address, length).Content; return((TValue[])(object)b); } if (typeof(TValue) == typeof(string)) { var b = busAsciiClient.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变量 DemoUtils.ReadResultRender(busAsciiClient.ReadUInt16(textBox3.Text), textBox3.Text, textBox4); }