所以基本上我有一个控制台应用程序在后台运行,监听指定的串行端口(设备:通过 USB 连接的条形码扫描仪,映射为串行端口)。附加的 SerialDataReceivedEventHandler 按预期工作。但是,一旦计算机进入待机/休眠状态并重新激活,显然不再发送数据(SerialDataReceivedEventHandler 不会触发)。如果我检查 SerialPort 对象,一切似乎都很好(相同的属性、相同的事件处理程序、连接仍然打开)。我还注意到无论如何连接似乎超时(几个小时后)。
联系
...
{
SerialPort serialPort = new SerialPort(port);
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.Handshake = Handshake.None; // tried RequestToSend as well, no difference
serialPort.DataReceived += new SerialDataReceivedEventHandler(MySerialPortHandler);
serialPort.Open();
...
}
static void MySerialPortHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serialPort = (SerialPort)sender;
string inData = serialPort.ReadExisting();
...
}