B4J Question Controlling COM-Ports

GMan

Well-Known Member
Licensed User
Longtime User
Ahoi,
how can i control the COM-Ports (also virtuals) through B4J ?
I want to send commands to an RS485-Relaiscard which is connected through USB (-> Virtual COM-Port).

In VB its no problem for me, but want to use B4J instead ;-)

Thx in @Vance
GMan
 

GMan

Well-Known Member
Licensed User
Longtime User
I am not an expert but maybe your COM port is not open?

Here is some code which I tried and worked:

B4X:
Sub Process_Globals
  Private myserial As Serial
  Private astream As AsyncStreams
  Private MyPort As String
  Private BaudRate As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
  myserial.Initialize("")

End sub

Sub btn_Connect_MouseClicked (EventData As MouseEvent)
   Log("Com Port: " & MyPort)
   Log("Baud: " & BaudRate)
   If btn_Connect.Text="Connect" Then
     btn_Connect.Text="Disconnect"  
     myserial.Open(MyPort)
     myserial.SetParams(BaudRate,8,1,0)     'Baud normally 9600
     astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
   Else  
     btn_Connect.Text="Connect"
     astream.Close
     myserial.Close
   End If
End Sub

'Then I use the following to send data,MyText is a string containing anything.

astream.Write(MyText.GetBytes("UTF8"))

Maybe this helps.

Sorry, NO - no click ;-)
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
THIS Code is working now:

B4X:
Sub Process_Globals
  Private myserial As Serial
  Private astream As AsyncStreams
  Private MyPort As String
  Private BaudRate As Int
  Private btn_Connect As Button
    Private Button1 As Button
    Private MyText As String
    Private Button2 As Button
    Private bc As ByteConverter
        Private fx As JFX
    Private MainForm As Form
End Sub

'Sub Activity_Create(FirstTime As Boolean)
' 
'  myserial.Initialize("")
' 
'
'End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("mainklein") 'Load the layout file.
    MainForm.Show
    myserial.Initialize("")
End Sub

Sub btn_Connect_MouseClicked (EventData As MouseEvent)
   Log("Com Port: " & MyPort)
   Log("Baud: " & BaudRate)
   If btn_Connect.Text="Connect" Then
     btn_Connect.Text="Disconnect"  
     myserial.Open("COM14")
     myserial.SetParams(9600,8,1,0)     'Baud normally 9600
     astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
   Else  
     btn_Connect.Text="Connect"
     astream.Close
     myserial.Close
   End If
End Sub

Sub Button1_Action
MyText = "FF0101"   
astream.Write(bc.HexToBytes(MyText))
'astream.Write(MyText.GetBytes("UTF8"))
End Sub

Sub Button2_Action
MyText = "FF0100"   
astream.Write(bc.HexToBytes(MyText))
'astream.Write(MyText.GetBytes("UTF8"))   
End Sub

So Erel was right with the bc-enchilada.

But why doesnt it work in the other code ?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Nice to know that it is now working. You can delete these lines though - you are not using them anyway.

B4X:
Private MyPort As String
Private BaudRate As Int
Log("Com Port: " & MyPort)
Log("Baud: " & BaudRate)

The code I posted was by no means complete but I hoped that it was enough to help a little.

Also, I had assumed that you can send the data as a string ( I could) - sorry.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Works not without errors.
Relays are switched on, but can switch off.
Here is the error code when clicking ON / OFF (Button has status and switches from green to red)

B4X:
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more
jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:170)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:154)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AOUT.run(AsyncStreams.java:317)
    at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - COM14; Method name - writeBytes(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.writeBytes(SerialPort.java:346)
    at anywheresoftware.b4j.serial.Serial$2.write(Serial.java:166)
    ... 3 more

But the port IS open:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1 
    MainForm.SetFormStyle ("UNIFIED")
    MainForm.RootPane.LoadLayout("MAINNEW") 'Load the layout file.
    MainForm.Show
    dartport="COM14"
    sp.Initialize("")
    Try
        sp.Open("COM14")
        sp.SetParams (9600, 8, 1, 0)
        'sp.close

    Catch
        Log(LastException)
       
    End Try
End Sub

I already set the InputStream to NULL, is the Syntax correct Erel ?

B4X:
Sub Senden_Action
    astream.Initialize(Null,sp.GetOutputStream,"astream")
    astream.Write(bc.HexToBytes(MyText))
    LogMessage("Gesendet ", MyText)
    'astream.Close
End Sub
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
How do you know that the port is open when the error log says the opposite? Can you try a loop-back test?
What is the value of MyText?

Maybe you can post a bit more code so that we can try and help.

I already set the InputStream to NULL, is the Syntax correct Erel ?
The syntax looks correct to me.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
I tested it while opening a terminal proggi and try to open the port.
When the software is running, the port is BUSY, otherwise when i close the software the port is accessible.

BUT: solved it (stupid me).
I declared in MY Codeversion the txtInput as a TextField :mad:, not as a string as necessary.

So, all good :) and Many thanks for helping :)
 
Upvote 0
Top