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
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Have you looked at the jSerial library and Asyncstreams?
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Not yet, so Thx for the Hint :)
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
So i gt that lib and installed it properly.
All is working well, instaed of sending datas through the port to the RS485 Relais Board :confused:


Here is the code i am using (the COM-Port is fixed to COM14), the code is the same as from the B4J Chat example:

B4X:
DIM sp as Serial
....
sp.Initialize("")
    Try
        sp.Open("COM14")
    Catch
        Log(LastException)
    End Try
...
Sub txtInput_Action
    astream.Write(txtInput.Text.GetBytes("UTF8"))
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Gesendet ", txtInput.Text)
End Sub

Before firing the txtInput_Action the txt.Input.Text is cleared and the filled with i.e.

B4X:
txtInput.Text = ""
txtInput.Text = "FF 04 00"
txtInput_Action
txtInput.Text = ""

The card works with the relais tester software on that port, but i i send it with B4J nothing happened.
Also the code is the same that i write through the servo tester software of the manufacturer
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
You are not sending the correct values.
B4X:
astream.Write(Array As Byte(0xFF, 0x04, 0))
Or:
B4X:
astream.Write(bc.HexToBytes("FF0400")) 'bc - ByteConverter
When txtInput.Text = "FF, 04, 01"
and i use
astream.Write(Array As Byte(txtInput))
an error occurs.
The hex's are stored in txtInput.Text

Stupid me....of course it breaks:
Will try the bc-story...
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
When txtInput.Text = "FF, 04, 01"
and i use
astream.Write(Array As Byte(txtInput))
an error occurs.
The hex's are stored in txtInput.Text

Stupid me....of course it breaks:
Will try the bc-story...

OK, that works (in code) BUT: no relay is clicking :oops:

Is the initialization and opening of the port correct ?


B4X:
    sp.Initialize("")
    Try
        sp.Open("COM14")
    Catch
        Log(LastException)
    End Try

The relay-board is set to 9600,8,N,1 and i think, this are the standard parameters.
or have i to specify those, too ?

The code for sending now looks like this:

B4X:
Sub txtInput_Action
    astream.Write(bc.HexToBytes(txtInput.text))
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Gesendet ", txtInput.Text)
End Sub

txtInput is set to i.e.
B4X:
txtInput.Text = ("FF0100")
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx Erel...so is this the equivalent to "Windows-Style" 9600,8,N,1 ?
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx Erel, but no rela is clicking.
Where can the error be ?
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Is this order correct ?
B4X:
sp.Initialize("")
    Try
        sp.Open("COM14")
        sp.SetParams (9600, 8, 1, 0)

    Catch
        Log(LastException)
    End Try

Have i to wrote
B4X:
sp.Initialize("sp")

or is the given code OK ?
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
So what can be the problem ?
The board works with VB, why not with B4J ?
Normally its not such a great think to send datas through COM-Ports...the COM-Port is opened.
I tried it with a terminal proggi which didnt allow to use the port 'cause its open.

Any suggestions ?
Also i tried the code in the format xx xx xx which is used in the TestSoftware from .

Here is the site with diverse Software for that :
http://www.kmtronic.com/usb-rs232-rs485-relays.html

And here are the native commands & adresses i am using:
http://kmtronic.com/kmtronic-rs485-relays-commands.html

There are also samples in DOS-Formatted .bat-Files :cool: which are working well
Also the TestSoftware works - all works, instead of B4J ?!
 
Upvote 0

Mark Read

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.
 
Upvote 0

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.
Looks good ;-), will give it a try.
Erel preferred this one:
B4X:
astream.Write(bc.HexToBytes(txtInput.text))

THis is my code for sending, port always initalized with params and opened:
B4X:
Sub Send_Action
    txtInput.RequestFocus
    txtInput.SelectAll
    astream.Initialize(sp.GetInputStream,sp.GetOutputStream,"astream")
    astream.Write(txtInput.Text.GetBytes("UTF8"))
    'astream.Write(bc.HexToBytes(txtInput.text))
    LogMessage("Gesendet ", txtInput.Text)
    astream.Close
End Sub
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
That needs still HEX-Data in Format "xx xx xx" (so the testsoftware uses)
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Tried all variants - nothing, nada.
Is there a knd of echo-test to see how the datas came out ?
B4X:
sp.Initialize("")
    Try
        sp.Open("COM14")
        sp.SetParams (9600, 8, 1, 0)
        'sp.close

    Catch
        Log(LastException)
       
    End Try

B4X:
Sub txtInput_Action
    txtInput.RequestFocus
    txtInput.SelectAll
    astream.Initialize(sp.GetInputStream,sp.GetOutputStream,"astream")
    'astream.Write(txtInput.Text.GetBytes("UTF8"))
    astream.Write(bc.HexToBytes(txtInput.text))
    LogMessage("Gesendet ", txtInput.Text)
    astream.Close
End Sub
 
Upvote 0
Top