我试图找到这样的东西:
class SerialEquipment
{
private int baudrate;
......
public SerialEquipment(int baudrate)
{
this.baudrate= baudrate;
}
public int Baudrate{ get => baudrate; set => baudrate= value; }
......
public static void IdentifyEquipment<EquipmentType>()
{
int baudrate = (int)typeof(EquipmentType).GetProperty("Baudrate", BindingFlags.Static | BindingFlags.Public).GetValue(null, null);
........
}
}
class EquipmentTypeA: SerialEquipment
{
private static readonly int baudrate = 9600;
......
public EquipmentTypeA() : base(baudrate) { }
public static new int Baudrate{ get => baudrate}
......
}
class EquipmentTypeB : SerialEquipment
{
private static readonly int baudrate = 38400;
......
public EquipmentTypeB () : base(baudrate) { }
public static new int Baudrate { get => baudrate }
......
}
但继承的目的是为了避免冗余,我找不到更好的东西供我使用。我找不到只有一个声明的方法。那么有更好的方法来实现这个吗?
编辑03/01/2018:
使用静态识别设备的功能如下:
public static List<String> IdentifyEquipment<EquipmentType>()
{
List<String> wantedEquipmentList = new List<String>();
int baudrate = (int)typeof(EquipmentType).GetProperty("Baudrate", BindingFlags.Static | BindingFlags.Public).GetValue(null, null);
String identificationCommand = (int)typeof(EquipmentType).GetProperty("Baudrate", BindingFlags.Static | BindingFlags.Public).GetValue(null, null);
String correctIdentificationResponse = (String)typeof(EquipmentType).GetProperty("CorrectIdentificationResponse", BindingFlags.Static | BindingFlags.Public).GetValue(null, null);
foreach (String aSerialPort in SerialPort.GetPortNames())
{
SerialPort deviceInTest = new SerialPort(aSerialPort, baudrate);
deviceInTest.Open();
deviceInTest.WriteLine(identificationCommand);
System.Threading.Thread.Sleep(50);
string result = deviceInTest.ReadLine();
if (result.StartsWith(correctIdentificationResponse))
{
wantedEquipmentList.Add(aSerialPort);
}
}
return wantedEquipmentList;
}
这里有关于Bruno Belmonde解决方案的新实施:
class DeviceIdentifier
{
private List<DeviceFactory> factories = new List<DeviceFactory>();
public void AddDeviceType(DeviceFactory factory) => factories.Add(factory);
public ISerialDevice BuildDeviceByIdentification(String comPort, int baudrate, String identificationCommand, String identificationResponse) => factories.FirstOrDefault(deviceFactory => IdentifyEquipment(comPort, baudrate, identificationCommand, identificationResponse))?.Builder();
private bool IdentifyEquipment(String comPort, int baudrate, String identificationCommand, String identificationResponse)
{
bool foundIt = false;
SerialPort deviceInTest = new SerialPort(comPort, baudrate);
deviceInTest.ReadTimeout = 200;
deviceInTest.Open();
try
{
deviceInTest.WriteLine(identificationCommand);
System.Threading.Thread.Sleep(50);
string result = deviceInTest.ReadLine();
if (result.StartsWith(identificationResponse))
{
foundIt = true;
}
}
catch
{
//........
}
deviceInTest.Close();
return foundIt;
}
}
// MAIN
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
List<EquipmentTypeA> typeAList = new List<EquipmentTypeA>();
deviceIdentifier.AddDeviceType(new DeviceFactory
{
Baudrate = 9600,
IdentificationCommand = "You are ?",
IdentificationResponse = "Type A",
Builder = () => new EquipmentTypeA(9600)
});
foreach(String aSerialPort in SerialPort.GetPortNames())
{
ISerialDevice identifiedObject = deviceIdentifier.BuildDeviceByIdentification(aSerialPort, 9600, "You are ?", "Type A");
if (identifiedObject != null)
typeAList.Add((EquipmentTypeA)identifiedObject);
}
答案 0 :(得分:0)
将您的设计更改为以下内容:
public interface IDevice
{
void WhateverIWantToDoWIthDevice();
}
public class DeviceIdentifier
{
private List<DeviceFactory> factories = new List<DeviceFactory>();
public void AddDeviceType(DeviceFactory factory) => factories.Add(factory);
public IDevice BuildDeviceFromBaudrate(int baudrate) => factories.FirstOrDefault(f => f.BaudRate == baudrate)?.Builder();
}
public class DeviceFactory
{
public int BaudRate { get; set; }
public Func<IDevice> Builder { get; set; }
}
然后在你的主要部分,只需添加像这样的已知设备
deviceIdentifier.AddDeviceType(new DeviceFactory
{
BaudRate = 12,
Builder = () => new DeviceOfTypeA()
});
因此当连接某些东西时,你会测试波特率与设备标识符的关系,它会给你一个设备实例,假设它有一个工厂匹配。
您可以从这里详细说明识别所需的测试...
最后,请记住静电必须谨慎操作,过多的静电通常是难闻的气味。静态覆盖更糟糕