Hi Everyone, I am using the Jserial library to communicate with a device, this device enumerates 3 different COM Ports but only one can be open to communicate with the device, the other two are for debugging purposes, so far everything works as expected, however I had the need to come up with a way to detect when the COM port has been unplugged and plugged back in.
I found this project here and decided to do a small wrapper for it, this seems to do exactly what I need as it raises two events, One when the COM Port is disconnected and another when the COM Port is connected back again, all this as I mentioned works perfect for my purposes.
The issue I am having now and I know this is probably an easy fix, is that I need to Re-establish the COM port communication after the USB COM Port has been disconnected and re-connected again.
I thought it would be as easy as re-initializing the Serial object and passing the COMPort String to re-open the port again, but I am seeing some issues where the com port communication will be established intermittently, where sometimes it will work and some others it wont.
Is there anything special that needs to be done as far as the JSerial library goes, I am using the Astreams Library as well to be able to capture the data being sent by the device and be able to send data back to the device as well. My question is, is there any special handling that needs to be done on the comport side when the cable is disconnected, i.e. flush any buffers etc.
Below is the functions that capture when the USB COM Port is disconnected and re-connected and how I am handling the reconnection of the serial port.
As you can see the it should be very straight forward, but for some reason when I try re-opening the COM Port again, I don't get any errors but I can tell that the communication is not working, if it was I would start seeing data right away from the device, I can't go into too much detail about the device itself because is highly confidential but is essentially a custom device running a custom Android Image.
Any thoughts on what may be needed, or what I may be doing wrong. I just can't seem to be able to re-open the COM Port back after being closed.
Thanks All.
Walter
I found this project here and decided to do a small wrapper for it, this seems to do exactly what I need as it raises two events, One when the COM Port is disconnected and another when the COM Port is connected back again, all this as I mentioned works perfect for my purposes.
The issue I am having now and I know this is probably an easy fix, is that I need to Re-establish the COM port communication after the USB COM Port has been disconnected and re-connected again.
I thought it would be as easy as re-initializing the Serial object and passing the COMPort String to re-open the port again, but I am seeing some issues where the com port communication will be established intermittently, where sometimes it will work and some others it wont.
Is there anything special that needs to be done as far as the JSerial library goes, I am using the Astreams Library as well to be able to capture the data being sent by the device and be able to send data back to the device as well. My question is, is there any special handling that needs to be done on the comport side when the cable is disconnected, i.e. flush any buffers etc.
Below is the functions that capture when the USB COM Port is disconnected and re-connected and how I am handling the reconnection of the serial port.
COM Port Disconnection:
Sub hid2_HidDeviceAttached (PID As Int, VID As Int)
Log($"new device attached with PID ${PID} and VID ${VID}"$)
If Not(Common.PortFound) Then
Dim rs As ResumableSub = FindComPorts
wait for (rs) complete (comport As String)
Log("comport: " & comport)
Common.ComPort = comport
OpenComPort(Common.ComPort)
End If
End Sub
Sub hid2_HidDeviceDetached (PID As Int, VID As Int)
Common.PortFound = False
Log($"new device Detached with PID ${PID} and VID ${VID}"$)
End Sub
Public Sub OpenComPort(comport As String)
Try
If rpc.IsInitialized Then
Log("opening or re-opening comport: " & comport)
Main.ser.Close
End If
Main.ser.Initialize("serial")
Main.ser.Open(comport)
Main.ser.SetParams(115200, 8, 1, 0)
Sleep(500)
rpc.Initialize(Me)
rpc.Flush
Catch
Log("error opening comport: " & LastException.Message)
End Try
End Sub
As you can see the it should be very straight forward, but for some reason when I try re-opening the COM Port again, I don't get any errors but I can tell that the communication is not working, if it was I would start seeing data right away from the device, I can't go into too much detail about the device itself because is highly confidential but is essentially a custom device running a custom Android Image.
Any thoughts on what may be needed, or what I may be doing wrong. I just can't seem to be able to re-open the COM Port back after being closed.
Thanks All.
Walter