B4J Question SOLVED - Serial Comms with FTDI based GPIB-USB Controller

Kevin L. Johnson

Member
Licensed User
Longtime User
Hello All,

I am having difficulty using Windows 10 and B4J to communicate with a FTDI Chip based General Purpose Interface Bus otherwise known as a GPIB-USB Controller. I can use Windows 10 HyperTerminal to talk to it with no problem.

Just a bit of background … The Win10 Hyperterm combination only requires me to open the Virtual Serial Port without specifying the parameters of (baudrate, databits, parity, and stop bit) etc., which is pretty convenient and apparently a feature of virtual comm ports. I do have to select the options to both “echo typed characters locally” and “send line-ends with line-feeds”in order to see my typed commands and to see the response on a new line. After this minimal configuration, I can (from HyperTerminal) …

send a “++ver” string and receive a string like “Prologix GPIB-USB Controller version 6.107”

Now I wish to use a B4J project, however, when I use Erel’s B4J_Chat example here…

https://www.b4x.com/android/forum/threads/jserial-library.34762/

I do not receive anything ... I have tried both Prefix …

astream.InitializePrefix(sp.GetInputStream, True, sp.GetOutputStream, "astream")

and no-prefix methods…

astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")

Somehow I got the idea that the Jserial lib under Windows10 with the latest version of Java has problems with the FTDI usb serial chip. If so, is there a work around?

Other relevant threads include ...
https://www.b4x.com/android/forum/threads/getbytes-encoding.64604/#post-605995
https://www.b4x.com/android/forum/threads/jserial-and-java-9.89325/#content

Any Ideas?
Thanks KLJ
 

techknight

Well-Known Member
Licensed User
Longtime User
That is a tricky peice of hardware. The User Manual clearly states that the serial port/baud rate settings, parity, flow control, etc do not matter, so unless the on-board microcontroller of the GPIB box has auto-timing and negotiation, I dunno.

If it were me, I would setup the serial port at 115,200 8-N-1 and see what happens.

You CANNOT use the Prefix mode of asyncstreams, its an internal proprietary thing that ads a header to the packet to make sure you got it all without any transmission errors.

You have to run it in standard asyncstreams mode. Also, you need to specify the correct COM port to open, and a baud rate, parity, etc setting. The Chat app doesnt list one.

So an example would be this:
B4X:
    Dim Serial1 as Serial
    Dim Astreams as AsyncStreams
    Serial1.Open(port)
    Serial1.SetParams(115200, 8, 1, 0)
    Astreams.Initialize(Serial1.GetInputStream, Serial1.GetOutputStream, "Astreams")

Then of course dont forget your Sub AStreams_NewData (Buffer() As Byte) or you wont get anything back.
 
Last edited:
Upvote 0

Kevin L. Johnson

Member
Licensed User
Longtime User
Thanks techknight,
I tried your suggestion(s) above with no change in behavior … still no response from the Prologix box. I've also had a log("I've got data") placed in the SubAStreams_NewData(….. ) event.

I may need to hook up two laptop's to send and receive via serial cable, with the first one running b4J and then swap out the with the 2nd laptop with the Prologix Controller. AND Thanks for going the extra mile and reading the Prologix's user manual too! Much appreciated!

NOTE: I should have mentioned that the hyperterm program defaults to "2400, 8, n, 1, h/w handshaking" parameters

Thanks
KLJ
 
Upvote 0

Kevin L. Johnson

Member
Licensed User
Longtime User
Here is the message sending code …

B4X:
 Sub txtInput_Action
    astream.Write(txtInput.Text.GetBytes("UTF8"))
    'astream.Write(txtInput.Text.GetBytes("ASCII"))
    'astream.Write(txtInput.Text.GetBytes("UTF-8"))
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Me", txtInput.Text)
End Sub

Here is what it looks like from HyperTerminal …

HyperTerminal Interface.png



Here is what it looks like from B4J_Chat example …

B4J Chat Interface.png


The code is from the B4J_Chat example. Here it is in its entirety ...

B4X:
#Region  Project Attributes
  #MainFormWidth: 600
  #MainFormHeight: 400
#End Region

Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim btnOpen As Button
 Dim cmbPort As ComboBox
 Dim txtInput As TextField
 Dim txtLog As TextArea
 Dim lblStatus As Label
 Private sp As Serial
 Private astream As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.RootPane.LoadLayout("1") 'Load the layout file.
 MainForm.Show
 MainForm.Title = "Bluetooth Chat"
 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
End Sub

Sub btnOpen_Action
 sp.Open(cmbPort.Value)
 sp.SetParams(115200, 8, 1, 0)
 astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
 txtInput.Enabled = True
 btnOpen.Enabled = False
 lblStatus.Text = "Status: Open"
End Sub

Sub MainForm_Closed
 sp.Close
End Sub

Sub AStream_NewData (Buffer() As Byte)
 LogDebug("Hey I'm in NewData!")
 Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
 'Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
 'Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
 
 LogMessage("You", s)
End Sub

Sub txtInput_Action
 astream.Write(txtInput.Text.GetBytes("UTF8"))
 'astream.Write(txtInput.Text.GetBytes("ASCII"))
 'astream.Write(txtInput.Text.GetBytes("UTF-8"))
 txtInput.SelectAll
 txtInput.RequestFocus
 LogMessage("Me", txtInput.Text)
End Sub

Sub LogMessage(From As String, Msg As String)
 txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
 txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length)
End Sub

Sub AStream_Error
 Log("Error: " & LastException)
 astream.Close
 AStream_Terminated
End Sub

Sub AStream_Terminated
 Log("Connection is broken.")
 lblStatus.Text = "Status: Close"
 txtInput.Enabled = False
 btnOpen.Enabled = True
End Sub
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
B4X:
 astream.Write((txtInput.Text & Chr(13) & Chr(10)).GetBytes("UTF8"))
Have you tried to add CHR (13) or / and CHR (10) at the end of the sent string?
 
Upvote 0

Kevin L. Johnson

Member
Licensed User
Longtime User
THANKS besoft! It Worked! … I was thinking of how I might do this, but couldn't get past the syntax. I should have known

Who would have thought that …

B4X:
astream.Write((txtInput.Text & Chr(13) & Chr(10)).GetBytes("UTF8"))

would work.

Working B4J interface.png


I love this product and the people who frequent these forums!

Awesome just Awesome

Thanks again besoft! and others too!
KLJ
 
Upvote 0
Top