_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_dataRec eived);
I use the class to get data from a barcode scanner. Normally I get the data as expected. When I send a deactivate command to the scanner the scanner still scans but does not send anything. When I activate the scanner it sends the barcodes in buffer BUT the eventhandler is not called.
The good thing is: that is exactly the behavior I want but it makes me crazy to understand why because on a serial logger I see that the data is sent to my port.
Why Microsoft SerialPort class knwos to ignore that or is it a bug/problem which helps me luckily. I want to understand. Any help/hint appreciated.
Also I do not call something like:
_serialPort.DataReceived -=
Thanks
Here some code:
My own class transport layer is initiated:
Expand|Select|Wrap|Line Numbers
- _scannerTransport = new TransportRs232ReadSynchronized(commPort, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One, false);
Expand|Select|Wrap|Line Numbers
- void init()
- {
- lock (lckObject)
- {
- try
- {
- // SerialPortFixer.Execute("COM" + port.ToString());
- _serialPort = new SerialPort("COM" + port.ToString(), baudRate, parity, dataBits, stopBits);
- _serialPort.Open();
- if (rtsdtsEnabled)
- {
- _serialPort.RtsEnable = true;
- _serialPort.DtrEnable = true;
- _serialPort.Handshake = Handshake.RequestToSend;
- }
- }
- catch (Io.IOException ioEx)
- {
- //Get the error code
- Logger.LogIt(String.Format("Exception: RS232-->SendWithCharDelay Error code {0} - {1}", "", ioEx.Message), Logger.LogLevel.Error);
- Logger.LogIt("Exception: RS232-->SendWithCharDelay " + ioEx.StackTrace, Logger.LogLevel.Error);
- }
- catch (Exception ex)
- {
- Logger.LogIt("Exception: RS232-->init " + ex.Message, Logger.LogLevel.Error);
- Logger.LogIt("Exception: RS232-->init " + ex.StackTrace, Logger.LogLevel.Error);
- }
- }
- }
That is the receive handler:
Expand|Select|Wrap|Line Numbers
- protected virtual void _serialPort_dataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- byte[] receiveData = null;
- int bytesToRead = 0;
- lock (lckObject)
- {
- try
- {
- if (_serialPort != null)
- {
- bytesToRead = _serialPort.BytesToRead;
- if (bytesToRead > 0)
- {
- receiveData = new byte[_serialPort.BytesToRead];
- _serialPort.Read(receiveData, 0, receiveData.Length);
- }
- }
- }
- catch (Exception ex)
- {
- Logger.LogIt("Exception: RS232-->Data Received " + ex.Message, Logger.LogLevel.Error);
- Logger.LogIt("Exception: RS232-->Data Received " + ex.StackTrace, Logger.LogLevel.Error);
- }
- }
- if (receiveData != null)
- {
- OnReceivedEvent(receiveData);
- }
- }
Again thanks for any hint: and the data is going on the line that is sure.