我正在尝试重构/更新一些旧的串行通信代码。我有这个:
private SerialPort cereal;
private String receivedData;
private FileXferLegacy()
{
cereal = new SerialPort("COM1", 9600);
cereal.PortName = "7727";
cereal.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
// Is this just as well, as the elided part is grayed out above?: cereal.DataReceived += port_DataReceived;
}
...但是当我尝试连接并发送 ping时收到错误消息“端口 '7727:' 不存在”:
public void SendDataContentsAsXML(string destinationPath, string XMLData)
{
byte[] stuff;
ExceptionLoggingService.Instance.WriteLog("Reached FileXferLegacy.SendDataContentsAsXML"); // <= This is written to the log file
cereal.Open();
stuff = System.Text.UTF8Encoding.UTF8.GetBytes("PING" + "\n");
cereal.Write(stuff, 0, stuff.Length);
stuff = System.Text.UTF8Encoding.UTF8.GetBytes(XMLData + "\n");
cereal.Write(stuff, 0, stuff.Length);
}
7727 与旧版应用程序中成功使用的端口相同。
我确实看到附加了一个冒号,并想知道这是否是问题所在 - 为什么它看到“7727:”而不是普通的旧“7727”,如果那样的话,我该如何消除附加冒号的概念确实有问题吗?