脚本说明
在Modbus Slave Emulator中,可以使用自定义C#脚本。编写脚本时,你需要实现下面这个接口,当主站(客户端)发起请求时,执行脚本中对应的方法。
namespace ModbusSlave.Script
{
public interface IHandler
{
/// <summary>
/// 读线圈状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
bool[] ReadCoilDiscretes(ushort startAddress, ushort numberOfPoints);
/// <summary>
/// 写线圈状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="points">要写入的数据</param>
void WriteCoilDiscretes(ushort startAddress, bool[] points);
/// <summary>
/// 读离散输入状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
bool[] ReadCoilInputs(ushort startAddress, ushort numberOfPoints);
/// <summary>
/// 读保持寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
ushort[] ReadHoldingRegisters(ushort startAddress, ushort numberOfPoints);
/// <summary>
/// 写保持寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="points">要写入的数据</param>
void WriteHoldingRegisters(ushort startAddress, ushort[] points);
/// <summary>
/// 读输入寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
ushort[] ReadInputRegisters(ushort startAddress, ushort numberOfPoints);
}
}
IHandler
的实现类必须命名为Handler
:
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// IHandler实现示例
/// </summary>
public class Handler : ModbusSlave.Script.IHandler
{
/// <summary>
/// 读线圈状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
public bool[] ReadCoilDiscretes(ushort startAddress, ushort numberOfPoints)
{
bool[] array = new bool[numberOfPoints];
for (int i = 0; i < array.Length; i++)
{
array[i] = true;
}
return array;
}
/// <summary>
/// 写线圈状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="points">要写入的数据</param>
public void WriteCoilDiscretes(ushort startAddress, bool[] points)
{
//ignore
}
/// <summary>
/// 读离散输入状态
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
public bool[] ReadCoilInputs(ushort startAddress, ushort numberOfPoints)
{
bool[] array = new bool[numberOfPoints];
for (int i = 0; i < array.Length; i++)
{
array[i] = true;
}
return array;
}
/// <summary>
/// 读保持寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns>如果返回null,则返回用户配置的寄存器数据;如果不是null,则返回脚本提供的数据</returns>
public ushort[] ReadHoldingRegisters(ushort startAddress, ushort numberOfPoints)
{
ushort[] array = new ushort[numberOfPoints];
for (int i = 0; i < array.Length; i++)
{
array[i] = 1;
}
return array;
}
/// <summary>
/// 写保持寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="points">要写入的数据</param>
public void WriteHoldingRegisters(ushort startAddress, ushort[] points)
{
//ignore
}
/// <summary>
/// 读输入寄存器
/// </summary>
/// <param name="startAddress">寄存器起始地址(Protocol Address Base 0)</param>
/// <param name="numberOfPoints">读取的数量</param>
/// <returns></returns>
public ushort[] ReadInputRegisters(ushort startAddress, ushort numberOfPoints)
{
ushort[] array = new ushort[numberOfPoints];
for (int i = 0; i < array.Length; i++)
{
array[i] = 1;
}
return array;
}
}