views:

335

answers:

2

Using VB.Net I've added a CTRL-C handler:

 AddHandler Console.CancelKeyPress, AddressOf QuitHandler

Which does the following:

Private Sub QuitHandler(ByVal sender As Object, ByVal args As ConsoleCancelEventArgs)

        Console.WriteLine("Quitting...")
        args.Cancel = True
        Quit = True
    End Sub

I then have a main loop which just runs until Quit=True.

This all works until I start reading from the serial port:

 Private Sub port_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Port.DataReceived

at which point the CTRL-C handler gets ignored for about 30secs at which point the console app just terminates without going through the cleanup code.

Why?

A: 

I don't know VB, but my guess would be that you're attempting to read data from the serial port that isn't there (err, the port is there, the data isn't); as a result, your program is blocking ("hanging") until the read attempt times out after 30 seconds.

If I'm correct, you need some way to poll your serial input without blocking, or (better) to get an asynchronous sub called when data actually appears.

Carl Smotricz
port_DataReceived is a handler in itself so runs on a different thread when data appears so it shouldn't be blocking the main thread. CTRL-C works up until the point the first set of data is read i.e if you press CTRL-C before any data hits the serial port everything is fine.
Nicky
Data is hitting the port every 10 seconds or so, so three or four lots is received and displayed before the app quits.
Nicky
OK, time for me to give up then. My guesses haven't helped you. Sorry!
Carl Smotricz
A: 

You should make sure to understand how Timeouts work, since you are reading serial port on pooling. Your serial thread will always be running and trying to read something.

A best approuch would be to read data just when its availiable, then your serial thread would have time to breath.

You can also try to use DoEvents.

Douglas Gemignani