BaH.Serial -- detecting disconnection?

BlitzMax Forums/Brucey's Modules/BaH.Serial -- detecting disconnection?

BlitzSupport(Posted 2014) [#1]
Hi all,

I'm using BaH.Serial to control an Arduino LoLShield via the standard USB connection, as shown here in a Windows-based CPU monitor. (See video in thread for coolness!)

I just successfully added code to repeatedly attempt serial connection if the Arduino isn't yet plugged in, but can't figure out how to handle unplugging the thing!



The key section is highlighted with "****" near the end of the code. Is there any way to handle this safely?

This is typical output from first plugging in to unplugging:

Executing:whatsthepcduino.exe

"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...

OK, connected!

    [Displays ongoing CPU usage on LoLShield... and now we unplug... ]

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

terminate called after throwing an instance of 'serial::IOException'
  what():  IO Exception: Error while writing to the serial port: 5, file D:/DevTools/Blitz/BlitzMax/mod/bah.mod/serial.mod/serial/src/impl/win.cc, line 314.

Process complete


I've tried changing the obj:TSerialException to obj:IOException, based on the above output, but it gives the same error. Also tried obj:TPortNotOpenedException and tried checking if serial:TSerial is null (don't think this is expected to work), all to no avail.

As you can probably tell, I'm new to this!


Brucey(Posted 2014) [#2]
terminate called after throwing an instance of ...

Generally when you see this, it means the C++ is raising an exception that I have not caught and Thrown as a BlitzMax exception... :-/

Apologies. I should have a fix for you this evening sometime.


BlitzSupport(Posted 2014) [#3]
Cheers, Brucey... no rush, probably won't get to test until Saturday now, anyway -- but I'll report ASAP once it's available!


BlitzSupport(Posted 2014) [#4]
Hi Brucey,

Only just noticed you'd updated this -- many thanks!

I've found that I can now detect a disconnection of the device without crashing -- woo! -- but I'm wondering if something's wrong still, or if I'm doing something wrong.

New code:



So... I have a SerialConnect function that just tries to repeatedly open the port and eventually returns a TSerial once the device is plugged in.

This works fine, then I disconnect the device (Arduino), detect the drop, plug in again, then attempt to reconnect, like so:

	Try
		serial.write Varptr cpubyte, 1
		
	Catch obj:TSerialException
	'Catch obj:TIOException
	
		Print "Disconnected! " + obj.ToString ()
		Print "Lost connection! Trying again..."

		serial = SerialConnect ()
		
		If serial = Null Then Print "NULL" Else Print "GOT SERIAL"
		
	End Try


So... disconnected, then I try to get a new handle from SerialConnect, which just keeps checking until plugged in again.

However, once disconnected, I can't re-open the port ("COM3"). I've tried calling serial.Close after disconnection (the handle is non-Null still), but to no avail, so I'm wondering if maybe the library ought to be doing something to free the port up after a disconnection, if that's even possible?

Even if I restart the program, I get told COM3 isn't found -- have to unplug it and plug in again.

I was hoping it would be possible to pick up the connection again while the program's running... would you expect that to be do-able without having to end the program and unplug/re-plug the device again?

Typical session goes like this:

Building whatsthepcduino
Executing:whatsthepcduino.debug.exe

"IO Exception: Specified port, COM3, does not exist."
Trying again...

	[... until plugged in... ]

OK, connected!

	[Working away, now unplug device... ]

Disconnected! IO Exception: Error while writing to the serial port: 5, file I:/Development/DevTools/Blitz/BlitzMax/mod/bah.mod/serial.mod/serial/src/impl/win.cc, line 314.
Lost connection! Trying again...

	[Fine... however, on re-plugging in... ]

"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...
"IO Exception: Specified port, COM3, does not exist."
Trying again...

Process terminated



BlitzSupport(Posted 2014) [#5]
Also, I'm not really clear on which exception I should be checking for at each stage (detecting initial connection versus disconnection), TSerialException or TIOException (or TPortNotOpenedException?!) -- what's the actual difference between these?

EDIT: Just tried catching TPortNotOpenedException at disconnection stage, but that actually crashes it.

Oh, one last thing! I can't get listPorts to return anything, whether I have the device connected or not. (Ignore: found listPorts is Linux only!)