none
How can I stop the SerialPort in SerialPort.DataReceived Event? RRS feed

  • Question

  • I connect to a handle scaner by SerialPort.

    I need to stop the SerialPort while scaned a code which is repeated.

    Whereas, I found that when I stop the SerialPort in SerialPort.DataReceived event. The visual studio will reports thread exit in Ouput window but without threws an error. What's more, the code will stop while the thread exited also.

    I googled and found that it seems I can't stop the SerialPort in SerialPort.DataReceived Event or it will crash like above.

    I wanna if there is any way for me to stop the SerialPort while scaned a code which is repeated? Thank you.

    Monday, July 15, 2019 1:10 PM

Answers

  • Finally I found a strange way by create a new Task to solve this:

    Task.Run(() =>
                            {
                                ///the code to close the Serial Port.
                            });

    • Marked as answer by mywatermelon Wednesday, July 17, 2019 3:00 AM
    Wednesday, July 17, 2019 3:00 AM

All replies

  • There should be no need to "stop" the data received event handler what you can do instead is insert a boolean variable that prevents data being read when a certain condition exists. If you need to you can also clear the receive buffer while not using the data.

    Another option is to close the port in between data reads, this may not be what you want especially if the scanner is in constant use.

    private bool enable_read=true;
    
    private void _port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    
            {
    if(enable_read)
    		{
    			//read the data here
    		}
    else            
                    {
    			_port.ReadExisting(); // discard the receive buffer
    		}

    Monday, July 15, 2019 3:39 PM
  • There should be no need to "stop" the data received event handler what you can do instead is insert a boolean variable that prevents data being read when a certain condition exists. If you need to you can also clear the receive buffer while not using the data.

    Another option is to close the port in between data reads, this may not be what you want especially if the scanner is in constant use.

    private bool enable_read=true;
    
    private void _port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    
            {
    if(enable_read)
    		{
    			//read the data here
    		}
    else            
                    {
    			_port.ReadExisting(); // discard the receive buffer
    		}

    The users will not always notice the screen, when they scan a repeat code they can hardly know and always continue scaning.

    What's more, the scaner will warn with sound while not connect to the SerialPort.

    I wanna disconnect the SerialPort so that make the scaner alerts the users.

    Tuesday, July 16, 2019 12:16 AM
  • There should be no need for the user to know or even care about the screen or if it is a repeat code, the process should be automatic.

    Where the code snippet says "//read data here" it should recognize a valid scan and disable the boolean variable enable_read for a period of time or create a sound to alert the user the scan was recognized.

    You have two posts and not really described how your process works, the communications must have some kind of protocol which might help in determining how to handle the incoming data.

    Tuesday, July 16, 2019 2:02 AM
  • Finally I found a strange way by create a new Task to solve this:

    Task.Run(() =>
                            {
                                ///the code to close the Serial Port.
                            });

    • Marked as answer by mywatermelon Wednesday, July 17, 2019 3:00 AM
    Wednesday, July 17, 2019 3:00 AM