【问题标题】:What do masters really know about their slaves?主人对他们的奴隶到底了解多少?
【发布时间】:2015-02-25 05:27:01
【问题描述】:

我的设备处于主/从配置中,并且正在开发 WPF/MVVM 应用程序。

我有一个 COM 对象(所有这些对象都实现了 IDevice),它们代表外部设备/Modbus 网络的状态,并附加到 SerialPortSocket 之类的东西上。这意味着,在通过VersionRevision 识别设备后,我调用IDevice device = DeviceManager.GetDevice(version, revision); 来获取代表出厂默认设备状态的对象。

现在我有了IDevice,我可以调用它的API 来获取List<ushort> GetCoreRegisters()List<ushort> GetAllRegisters() 之类的东西。话虽如此,在读取寄存器值后,我必须调用IDevice 的API 来设置值:device.SetItemValue(registerAddress, registerValue) 以更新网络另一端设备的状态。

我创建了一个处理通信层的Master 类型(SocketSerialPort)。

在我的应用程序的当前状态下,我在我的一个视图模型中调用了类似以下的伪代码(单击按钮后):

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&lt;IDevice&gt; 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);

GetRegisterSetRegisterValue 的所有逻辑都在 Master 内部——意思是 Master 知道有关 IDevice 的一切,并处理配置从属状态的逻辑。

【问题讨论】:

    标签: c# communication network-protocols


    【解决方案1】:

    在理想情况下,查看模型代码非常简单。您当然不想在其中运行长时间的操作和循环。视图模型包含处理来自视图的命令的逻辑,就是这样。

    您的第一个示例似乎包含相当多的领域知识和业务逻辑。这应该在模型中某处。在我看来,您的 Master 课程似乎是一个合理的放置位置。

    回答名义上的问题:主人对他们的奴隶了解很多,当然足以“驱动”或“使用”他们。因此,它知道IDevice 中的所有内容是可以的。确保它是通用的,但主人不应该知道他正在处理的奴隶是什么类型的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多