我有我的设备在主/从配置,我正在开发
WPF / MVVM应用程序.
我有一个COM对象(所有这些都实现IDevice),它代表外部设备/ Modbus网络的状态,并附加到SerialPort或Socket之类的东西.这意味着,在通过其版本和修订版识别设备之后,我调用IDevice device = DeviceManager.GetDevice(version,revision);获取表示出厂默认设备状态的对象.
既然我有一个IDevice,我可以调用它的API来获取List< ushort>之类的内容. GetCoreRegisters()和List< ushort> GetAllRegisters().话虽如此,在读取寄存器值后,我必须调用IDevice的API来设置值:device.SetItemValue(registeraddress,registerValue),以便更新网络另一端的设备状态.
我创建了一个处理通信层(Socket与SerialPort)的Master类型.
在我的应用程序的当前状态中,我在我的一个视图模型中调用类似下面的伪代码(在单击按钮之后):
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()
我的问题是,这是正确的设计,还是我应该有一个List< IDevice> Master中的设备和识别设备后,我会做更多的事情:
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的所有内容并处理配置Slave状态的逻辑.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。