我有我的设备在主/从配置,我正在开发一个WPF/MVVM应用程序。主人真的知道他们的奴隶是什么?
我有一个COM对象(所有这些都实现IDevice
)代表外部设备/ Modbus网络的状态,并附加到SerialPort
或Socket
之类的东西上。这意味着,在通过Version
和Revision
识别设备后,我会拨打IDevice device = DeviceManager.GetDevice(version, revision);
来获取表示出厂默认设备状态的对象。
现在我有一个IDevice
,我可以调用它的API来获取诸如List<ushort> GetCoreRegisters()
和List<ushort> GetAllRegisters()
之类的东西。据说,在读取寄存器值之后,我必须调用IDevice
的API来设置值:device.SetItemValue(registerAddress, registerValue)
以便更新网络另一端的设备状态。
我创建了Master
类型,它处理通信层(Socket
与SerialPort
)。
在我的应用程序的当前状态,我呼吁像我认为的车型之一下面的伪代码(单击一个按钮后):
IDevice device = null;
profile = SelectedProfile
master = MasterFactory.GetMaster(profile.Name)
master.Open() //Connects or Opens SerialPort/Socket
if(master.DeviceCheck(profile.SlaveAddress))
{
KeyValuePair<ushort, ushort> info = await master.IdentifyDeviceAsync(profile.SlaveAddress);
device = DeviceManager.GetDevice(info.Key, info.Value)
initList = device.GetInitializationRegisters()
initValues = await master.ReadRegisters(profile.SlaveAddress, initList)
for(int i = 0; i < initList; i++)
device.SetRegisterValue(initList[i], initValues[i]);
allRegisters = device.GetAllRegisters();
allValues = await master.ReadRegisters(profileSlaveAddress, allRegisters)
for ... repeat
}
if device != null, DevicesInViewModel.Add(device)
master.Close()
我的问题是,这是正确的设计,或者我应该在Master
和识别装置(S)后有List<IDevice> Devices
,我会做更多的东西一样:
device = DeviceManager.GetDevice(info.Key, info.Value);
master.Add(device);
// possibly add more devices if needed
List<IDevice> devices = master.ReadDevices()
if devices != null, DevicesInViewModel.AddRange(devices);
,所有的GetRegister
和SetRegisterValue
逻辑里面Master
- 意思大师知道关于IDevice
的所有信息,并处理配置从属状态的逻辑。