1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question Open COM Port after Program Termination

Discussion in 'B4J Questions' started by Amin Ismail, Jul 30, 2019.

  1. Amin Ismail

    Amin Ismail 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!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Example of adding an event that fires when the VM is being terminated:

    Code:
    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.
     
    DonManfred likes this.
  3. Amin Ismail

    Amin Ismail Member Licensed User

    It works... kind of ….. but I get the following exception when I press CTL-C:

    Code:
    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:

    Code:
    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!
     
  4. DonManfred

    DonManfred Expert Licensed User

    try remove the StopMessageLoop
    The loop is probably stopped anyway because of the shutdown.

    Or maybe move the StopMessageLoop to the line before the LOG (after serial, AST and mqtt close)
     
  5. Amin Ismail

    Amin Ismail 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!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You should ignore this exception. It will do no harm.
     
  7. Amin Ismail

    Amin Ismail Member Licensed User

    Difficult thing to do, but I did :)
    Thanks Erel and DonManfred!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice