我有以下代码,想知道如何分解它?它工作得很好,但是是由一位大师写的,而且在我头上。预先感谢您的任何帮助。
该代码基本上将发送和接收的设备信息写入 GUI。似乎它具有基于设备创建的事件并且还调用 GUI 的东西。
- 为什么需要调用?
- 我想我想知道这是否过于复杂或合适?完成相同任务的更好方法是什么?
- 我也想知道“代表{};”是什么 做?
公共事件 EventHandler CommunicationPerformed = 委托 { };
SerialPort _port;
readonly int _delay = 100;
private string ReadAndUpdateStatus()
{
string read = _port.ReadExisting();
CommunicationPerformed?.Invoke(this, new LoaderReadWriteEventArgs(read, LoaderCommunicationDirection.Read));
return read;
}
private void WriteAndUpdateStatus(string data)
{
if (!data.StartsWith("//")) //ignore "Comment" lines but show them on the GUI to read
_port.Write(data);
CommunicationPerformed?.Invoke(this, new LoaderReadWriteEventArgs(data, LoaderCommunicationDirection.Write));
}
public class LoaderReadWriteEventArgs : EventArgs
{
public LoaderCommunicationDirection Direction { get; }
public string Value { get; }
public LoaderReadWriteEventArgs(string value, LoaderCommunicationDirection direction)
{
Value = value;
Direction = direction;
}
}
public enum LoaderCommunicationDirection
{
Read,
Write
}