private ModbusAscii modbus = new ModbusAscii( );   // 实例化



        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 });


            // 离散输入的数据读取同理
        }
Beispiel #2
0
 private void button_read_bool_Click(object sender, EventArgs e)
 {
     // 读取bool变量
     if (textBox1.Text == "1")
     {
         DemoUtils.ReadResultRender(busAsciiClient.ReadCoil(textBox3.Text), textBox3.Text, textBox4);
     }
     else
     {
         DemoUtils.ReadResultRender(busAsciiClient.ReadCoil(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)));
        }
Beispiel #4
0
        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)));
        }
Beispiel #5
0
 private void button_read_bool_Click(object sender, EventArgs e)
 {
     // 读取bool变量
     DemoUtils.ReadResultRender(busAsciiClient.ReadCoil(textBox3.Text), textBox3.Text, textBox4);
 }