473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

System.Io.Serialport: Sometimes SerialDataReceivedEventHandler is not fired

I have a transport layer class using:
_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
  1. _scannerTransport = new TransportRs232ReadSynchronized(commPort, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One, false);
  2.  
On that class there is an init method:
Expand|Select|Wrap|Line Numbers
  1. void init()
  2.         {
  3.             lock (lckObject)
  4.             {
  5.                 try
  6.                 {
  7.                     // SerialPortFixer.Execute("COM" + port.ToString());
  8.                     _serialPort = new SerialPort("COM" + port.ToString(), baudRate, parity, dataBits, stopBits);
  9.                     _serialPort.Open();
  10.                     if (rtsdtsEnabled)
  11.                     {
  12.                         _serialPort.RtsEnable = true;
  13.                         _serialPort.DtrEnable = true;
  14.  
  15.                         _serialPort.Handshake = Handshake.RequestToSend;
  16.                     }
  17.                 }
  18.                 catch (Io.IOException ioEx)
  19.                 {
  20.                     //Get the error code
  21.                     Logger.LogIt(String.Format("Exception: RS232-->SendWithCharDelay Error code {0} - {1}", "", ioEx.Message), Logger.LogLevel.Error);
  22.                     Logger.LogIt("Exception: RS232-->SendWithCharDelay " + ioEx.StackTrace, Logger.LogLevel.Error);
  23.                 }
  24.                 catch (Exception ex)
  25.                 {
  26.                     Logger.LogIt("Exception: RS232-->init " + ex.Message, Logger.LogLevel.Error);
  27.                     Logger.LogIt("Exception: RS232-->init " + ex.StackTrace, Logger.LogLevel.Error);
  28.                 }
  29.             }
  30.         }
  31.  


That is the receive handler:

Expand|Select|Wrap|Line Numbers
  1. protected virtual void _serialPort_dataReceived(object sender, SerialDataReceivedEventArgs e)
  2.         {
  3.             byte[] receiveData = null;
  4.             int bytesToRead = 0;
  5.             lock (lckObject)
  6.             {
  7.                 try
  8.                 {
  9.                     if (_serialPort != null)
  10.                     {
  11.                         bytesToRead = _serialPort.BytesToRead;
  12.                         if (bytesToRead > 0)
  13.                         {
  14.                             receiveData = new byte[_serialPort.BytesToRead];
  15.                             _serialPort.Read(receiveData, 0, receiveData.Length);
  16.                         }
  17.                     }
  18.                 }
  19.                 catch (Exception ex)
  20.                 {
  21.                     Logger.LogIt("Exception: RS232-->Data Received " + ex.Message, Logger.LogLevel.Error);
  22.                     Logger.LogIt("Exception: RS232-->Data Received " + ex.StackTrace, Logger.LogLevel.Error);
  23.                 }
  24.             }
  25.             if (receiveData != null)
  26.             {
  27.                 OnReceivedEvent(receiveData);
  28.             }
  29.         }
  30.  

Again thanks for any hint: and the data is going on the line that is sure.
Aug 31 '17 #1
0 2143

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nathan Ie | last post by:
Hello all! I was wondering if any of you could shed some light on this problem I cant seem to figure out... I try to add an event handler to my SerialPort variable by doing: ...
13
by: Jean Paul Mertens | last post by:
Hello, Someone can tell me why I dont get serial port events in a Service, I created a separate Thread to open the port but no events are coming up (the same happens when I use the timer...
21
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
8
by: Kevin.M.Jonas | last post by:
I have the following method that handles the data received event: private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { string data = _port.ReadExisting();...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.