【发布时间】:2015-02-25 05:27:01
【问题描述】:
我的设备处于主/从配置中,并且正在开发 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 中有一个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 内部——意思是 Master 知道有关 IDevice 的一切,并处理配置从属状态的逻辑。
【问题讨论】:
标签: c# communication network-protocols