B4J Question Open COM Port after Program Termination

Amin Ismail

Active Member
Licensed User
I wonder if someone could shed some light onto this....

I'm running a Non-UI Application that opens a COM port on a PC. The Application works great. However, if I terminate the application with a CONTROL-C, the COM Port remains open and subsequently running the same program again (or any other program that opens the same port) will produce the error saying that the COM port is already open by another application.

I realize that I should close the COM port in my Application before I terminate the program. However, how do I trap a CONTROL-C in the program and close the COM Port before the program terminates?

Thanks for any advise!
 

Erel

Administrator
Staff member
Licensed User
Example of adding an event that fires when the VM is being terminated:

B4X:
Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
   Dim jo As JavaObject = Me
   jo.RunMethod("addShutdownHook", Null)
   StartMessageLoop
End Sub

Sub Shutdown_Hook
   Log("Hook event")
   File.WriteString(File.DirApp, "1.txt", "test")
End Sub


#if Java
public static void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                System.out.println("Shutdown hook");
               ba.raiseEventFromDifferentThread(null, null, 0, "shutdown_hook", true, null);
                Thread.sleep(500);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
}
#End If
Test it from the command line as the IDE kills the process without letting it run the shutdown hook.
 

Amin Ismail

Active Member
Licensed User
Example of adding an event that fires when the VM is being terminated:

B4X:
Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)
   Dim jo As JavaObject = Me
   jo.RunMethod("addShutdownHook", Null)
   StartMessageLoop
End Sub

Sub Shutdown_Hook
   Log("Hook event")
   File.WriteString(File.DirApp, "1.txt", "test")
End Sub


#if Java
public static void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                System.out.println("Shutdown hook");
               ba.raiseEventFromDifferentThread(null, null, 0, "shutdown_hook", true, null);
                Thread.sleep(500);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
}
#End If
Test it from the command line as the IDE kills the process without letting it run the shutdown hook.
It works... kind of ….. but I get the following exception when I press CTL-C:

B4X:
java.lang.NegativeArraySizeException
        at jssc.SerialNativeInterface.readBytes(Native Method)
        at jssc.SerialPort.readBytes(SerialPort.java:437)
        at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:131)
        at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:123)
        at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:199)
        at java.lang.Thread.run(Unknown Source)
java.lang.RuntimeException: java.lang.NegativeArraySizeException
        at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:138)
        at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:123)
        at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:199)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NegativeArraySizeException
        at jssc.SerialNativeInterface.readBytes(Native Method)
        at jssc.SerialPort.readBytes(SerialPort.java:437)
        at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:131)
        ... 3 more
Program Terminated - Ports Closed

This is what I have in the ShutDown_Hook sub:

B4X:
Sub Shutdown_Hook
 StopMessageLoop
 working=False
 'File.WriteString(File.DirApp, "1.txt", "test")
 serial.Close
 AST.Close
 mqtt.Close
 Log("Program Terminated - Ports Closed")
End Sub
Thanks!
 

Amin Ismail

Active Member
Licensed User
Actually I added "StopMessageLoop" after I started getting the exception. But I removed it now and tried it and still got the exception. Also tried moving it as you suggested but no change.
Anyway.... it's not a big deal since my main concern was leaving the COM port open after the application terminated. This seems to have corrected the issue. I was just curious as to why I was getting the exception.
Thanks for your help!
 

Erel

Administrator
Staff member
Licensed User
You should ignore this exception. It will do no harm.
 
Top