B4J Question Open/Close port to the library jSerial

red30

Well-Known Member
Licensed User
Longtime User
I tried to write the program resembling yours. But I want the COMport be opened just after choosing the item in Combobox. When I push the button btnClose,the COMport closes, but there is an error.
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:126)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:118)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:193)
    at java.lang.Thread.run(Thread.java:745)
What is wrong?
When I connect the new device, COMport doesnt occur in the list Combobox. I added the button - btnRefresh. When I push it, I refresh the list Combobox. I also have an error. How to correct?
 

Attachments

  • MyChat.zip
    501.6 KB · Views: 299

red30

Well-Known Member
Licensed User
Longtime User
How can I update the list of COM ports? That there was no mistake, as in the attached program?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private cmbPort As ComboBox
    Private sp As Serial
    Private astream As AsyncStreams
    Private btnClose As Button
    Private lblStatus As Label
    Private btnRefresh As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("123") 'Load the layout file.
    MainForm.Show
    MainForm.Title = "Test"
    MainForm.BackColor = fx.Colors.White
    sp.Initialize("")
    cmbPort.Items.AddAll(sp.ListPorts)
End Sub

Sub cmbPort_SelectedIndexChanged(Index As Int, Value As Object)
    'btnOpen.Enabled = Index > -1 'enable the button if there is a selected item
    sp.Open(cmbPort.Value)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    'astream.InitializePrefix(sp.GetInputStream, True, sp.GetOutputStream, "astream")
    btnRefresh.Enabled = False
    cmbPort.Enabled = False
    lblStatus.Text = "Status: Open"
End Sub

Sub btnClose_Action
    astream.Close
    sp.Close
    btnRefresh.Enabled = True
    cmbPort.Enabled = True
    lblStatus.Text = "Status: Close"
End Sub

Sub btnRefresh_Action
    sp.Initialize("")
    cmbPort.Items.Clear
    cmbPort.Items.AddAll(sp.ListPorts)
End Sub

When I select COMport (cmbPort) from the combo box, I immediately open it. And by the btnClose button I close the COMport to open another if there are several. But when the program is already running and I'm connecting a new device, it is not defined in the list. I added the btnRefresh button to add it to the list. It is Enabled only when the port is closed. When I click it, I clean the port list (cmbPort.Items.Clear) to add a new one (cmbPort.Items.AddAll (sp.ListPorts)). Then I automatically switch to the subprogram (cmbPort_SelectedIndexChanged), which gives an error:
B4X:
jssc.SerialPortException: Port name - null; Method name - openPort(); Exception type - Port not found.
    at jssc.SerialPort.openPort(SerialPort.java:167)
    at anywheresoftware.b4j.serial.Serial.Open(Serial.java:80)
    at b4j.example.main._cmbport_selectedindexchanged(main.java:129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:79)
    at anywheresoftware.b4a.BA$1.run(BA.java:188)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
How to solve this problem?
 
Upvote 0
Top