API method to detect serial de-connection?

My photons all have some debugging/diag code that logs hints and info to a connected terminal. I wrote a nodeJs debugging tool that auto-connects to my usb serial port and receives event notifications and can ask for status updates to find out things like ram usage and internal system and variable states.

Telling the Photon that the debugger is present is easy–once the debugger connects it just sends a command. But is there a simple, low-level way for the Photon to tell if the debugger serial connection went away (other than asking it repeatedly or timing out if it doesn’t ping–i’m looking for a socket level status). On the linux or mac end I can easily see if the chip closes the connection, but on the chip, is there a system call to tell me if the connection closed?

Hi,

  • On 0.5.x Serial.availableForWrite() returns -1 when USB Serial port is not open on the host;
  • Starting with 0.6.0, there is a Serial.isConnected() function that reports true when the host has USB Serial port open and false otherwise.
1 Like

Testing this on particle version 0.5.2, Serial.availableForWrite() does start at -1 and changes to 0 when I connect, but it does not change back when I disconnect. It stays at 0.

What OS are you using?

MacOs El Capitan.

But you do not need a further signal to do this? Like the DTR control signal in the RS-232 standard. In my opinion the photon can not handle directly this scenario. Some time ago I solved the problem by using the commands on the serial. (photon - host/pc).

You are correct. This is a bit broken on 0.5.2. There was a major rewrite of USB subsystem for 0.6.0 and the SET_CONTROL_LINE_STATE (DTR/RTS) support is only correctly implemented in there. On 0.5.2, the state only changes when the device attempts to send something over Serial:

  1. Host closes USB Serial port
  2. Device attempts to send something over Serial
  3. Device detects that host closed USB Serial port