【发布时间】:2012-06-20 09:34:55
【问题描述】:
所以基本上我有一个控制台应用程序在后台运行,监听指定的串行端口(设备:通过 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();
...
}
【问题讨论】:
-
扔掉 USB 适配器,换一个。
标签: c# windows serial-port console-application .net