我有我的設備在主/從配置,我正在開發一個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
的所有信息,並處理配置從屬狀態的邏輯。