Serial Disconnect w/o hang

Has anyone found a reliable way to deal with losing a connection to a serial device, not have the program hang, and as a bonus, search for it to come back on? My remote arduino might get shut off while the OF app will keep going and I want to be able to deal with that.
Thanks for ideas.
Chris

If you’re connected to an Arduino device via USB, won’t the USB power it? If powered externally, perhaps it will auto-switch to USB power to keep the Arduino running?

If it does get completely disconnected, you could set up a directory listener (e.g. https://github.com/bakercp/ofxIO/blob/master/example_directory_watcher/src/ofApp.cpp#L40) to listen for items matching /dev/cu.usbserial* or /dev/tty.usbserial* (or the appropriate prefix). When a serial device disappears (e.g. the file handler is lost due to a disconnect), ofxSerial should throw an exception, which you could catch. If you keep a list of disconnected devices and watch the directory listener events for a matching reconnect. This could be a little tricky though as I don’t know that you are guaranteed by the device driver for the OS to give each device the exact same name each time. In that case, you could have a directory watcher set up and connect to any new device that shows up. If your Arduinos each are programmed with their own unique ID number, then you could quickly query the Arduino on connect to see what its ID is and add it back to the system.

That said, to avoid complexity I’d double check the power switch thing first to see if you can just make sure the Arduino stays on as long as the computer, that might solve most of your problems.