0
votes

So basically I have a console application running in background listening to a specified serial port (device: barcode-scanner connected via USB, mapped as serial port). The attached SerialDataReceivedEventHandler works as supposed. However, as soon as the computer goes into stand-by/hibernate and re-activates, apparently no data is sent anymore (SerialDataReceivedEventHandler does not trigger). If I check the SerialPort object, everything seems to be fine (same attributes, same event handler, connection still open). I also noticed that the connection seems to timeout (after several hours) anyway.

Connection

...
{
  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();

  ...
}
1
Throw away the USB adapter and get another one.Hans Passant

1 Answers

1
votes

I added logic to my application that listened to when the power mode changed.

Microsoft.Win32.SystemEvents.PowerModeChanged += OnPowerModeChanged;

On Suspend I closed my SerialPort and cleaned it up and on Restore I re-established the connection.