I've been looking for a better event model quite long, and this is my
contribution:
The Idea is that after the DataStore has been updated, an event is
fired. You can probably include the updated data in the event, but i
haven't tried that yet.
Changes below:
in ModbusDataCollection.cs
........
namespace Modbus.Data
{
//new Delegate
public delegate void DataChangedDelegate(int address, int count);
/// <summary>
/// Modbus Data sections support addresses 1-n so we always pad the 0
element with a default value in a ModbusDataCollection.
/// </summary>
public class ModbusDataCollection<TData> : Collection<TData>
{
private bool _allowZeroElement = true;
//New event
public event DataChangedDelegate DataChanged;
//New helper method
internal void FireDataChanged(int address, int count)
{
if (this.DataChanged != null)
DataChanged(address, count);
}
/// <summary>
/// Initializes a new instance of the <see
cref="ModbusDataCollection<TData>"/> class.
/// </summary>
public ModbusDataCollection()
.......
in DataStore.cs
.......
internal static void WriteData<TData>(Collection<TData> items,
ModbusDataCollection<TData> destination, ushort startAddress)
{
int startIndex = startAddress + 1;
if (startIndex < 0 || startIndex >= destination.Count)
throw new ArgumentOutOfRangeException("Start address was out of
range. Must be non-negative and <= the size of the collection.");
if (destination.Count < startIndex + items.Count)
throw new ArgumentOutOfRangeException("Items collection is too
large to write at specified start index.");
CollectionUtility.Update(items, destination, startIndex);
//new call to event
helper.
destination.FireDataChanged(startIndex, items.Count);
}
............
This gives the possibility to do something like:
private void StartSlave(int port)
{
m_modBusSlaveListener = new
System.Net.Sockets.TcpListener(port);
m_modBusSlaveListener.Start();
m_modBusSlave = ModbusTcpSlave.CreateTcp(1,
m_modBusSlaveListener);
m_modBusSlave.DataStore =
Modbus.Data.DataStoreFactory.CreateDefaultDataStore();
m_modBusSlave.DataStore.HoldingRegisters.DataChanged +=
new Modbus.Data.DataChangedDelegate(HoldingRegisters_DataChanged);
m_modBusSlave.Listen();
}
void HoldingRegisters_DataChanged(int address, int count)
{
for (int i = address; i < address + count; i++)
{
DoSomeThing(i);
> > > submitting a patch or something.- Dölj citerad text -
>
> - Visa citerad text -