49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using Common.Attributes;
|
|
using DeviceCommand.Base;
|
|
using NModbus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DeviceCommand.Device
|
|
{
|
|
[ADPCommand]
|
|
public class IOBoard : ModbusTcp
|
|
{
|
|
|
|
public IOBoard(string Ip地址, int 端口, int 发送超时, int 接收超时)
|
|
{
|
|
ConfigureDevice(Ip地址, 端口, 发送超时, 接收超时);
|
|
}
|
|
|
|
|
|
public async Task 写输出开关(byte 站号, ushort 开始地址, bool data)
|
|
{
|
|
await Modbus.WriteSingleCoilAsync(站号, 开始地址, data);
|
|
}
|
|
public async Task 批量写输出开关(byte 站号, ushort 开始地址, bool[] datas)
|
|
{
|
|
var 批量写入 = await Modbus.ReadCoilsAsync(1, 0, 16);
|
|
for (int i = 0; i < datas.Length; i++)
|
|
{
|
|
if (开始地址 + i < 批量写入.Length)
|
|
{
|
|
批量写入[开始地址 + i] = datas[i];
|
|
}
|
|
}
|
|
await Modbus.WriteMultipleCoilsAsync(站号, 0, 批量写入);
|
|
}
|
|
public async Task 读输出开关(byte 站号, ushort 开始地址)
|
|
{
|
|
await Modbus.ReadCoilsAsync(站号, 开始地址, 1);
|
|
}
|
|
public async Task<bool[]> 批量读输出开关(byte 站号, ushort 开始地址,ushort 数量)
|
|
{
|
|
return await Modbus.ReadCoilsAsync(站号, 开始地址, 数量);
|
|
}
|
|
}
|
|
}
|