B4J Question How to send a string to a board with Astream and Serial

FreeWolF

Active Member
Licensed User
Longtime User
Hello!

I have a board connected though a serial cable to the COM1 port of my PC.
The board want an hexadecimal string to respond.

I have wrote that simply program:

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

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
    Private TextField2 As TextField
    Private Serial1 As Serial
    Private ListView1 As ListView
    Private astream As AsyncStreams
    Private lista As List

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.RootPane.LoadLayout("Main")
    MainForm.Show
    lista.Initialize()
 
    Serial1.Initialize("Seriale") 'PUT ON A LIST ALL THE AVAILABLE PORTS
    For Each item In Serial1.ListPorts
        ListView1.Items.Add(item)
 
    Next
 
 
    astream.Initialize(Serial1.GetInputStream,Serial1.GetOutputStream,"Dati")
     
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
 
    Dim dati() As Byte
 
    dati = Array As Byte(0xF5, 0x01, 0x00, 0x01, 0x01, 0x00, 0x08, 0xFC) 'Write the array on the board
    astream.Write(dati)
 
End Sub

Sub Astream_NewData (Buffer() As Byte)
    lista.Add(Buffer)
     
    If TextField1.Text = "" Then
        TextField1.Text = lista.Get(0) 'bring the received buffer on a TextField
    Else
        TextField1.Text = TextField1.Text & lista.Get(0)
    End If
 
    lista.Clear 'list clear
 
 
End Sub

Now, I have 2 problem (for now):

1) The library "byteconverter" don't run in B4X, but only in B4A
2) Obvioulsy there's some errors, where I'm wrong? The string is not sended, or is not correctly converted to bytes?


If I use another application to communicate with the board, is ok with that parameters:

 

Knoppi

Active Member
Licensed User
Longtime User
try this
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
    Private TextField2 As TextField
    Private Serial1 As Serial
    Private ListView1 As ListView
    Private astream As AsyncStreams
    Private lista As List
    
    Private PortName As String = "COM1"
    Private BaudRate As Int = 9600
    Private DataBits As Int = 8
    Private StopBits As Int = 1
    Private Parity As Int = 0

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.RootPane.LoadLayout("Main")
    MainForm.Show
    lista.Initialize()
 
    Serial1.Initialize("Seriale") 'PUT ON A LIST ALL THE AVAILABLE PORTS
    For Each item In Serial1.ListPorts
        ListView1.Items.Add(item)
    Next
 
    Serial1.Open( PortName)
    Serial1.SetParams(  BaudRate, DataBits, StopBits, Parity)     'Baud normally 9600
    astream.Initialize( Serial1.GetInputStream, Serial1.GetOutputStream, "astream")        'Dati changed to astream

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
 
    Dim dati() As Byte
 
    dati = Array As Byte(0xF5, 0x01, 0x00, 0x01, 0x01, 0x00, 0x08, 0xFC) 'Write the array on the board
    astream.Write(dati)
 
End Sub

Sub Astream_NewData (Buffer() As Byte)
    lista.Add(Buffer)
    
    If TextField1.Text = "" Then
        TextField1.Text = lista.Get(0) 'bring the received buffer on a TextField
    Else
        TextField1.Text = TextField1.Text & lista.Get(0)
    End If
 
    lista.Clear 'list clear
 
End Sub

i used ByteConverter v1.1 with B4J 6.00 and it works
be sure that you copy the jar and xml file to external library of B4J
Unbenannt.PNG
 
Upvote 0

FreeWolF

Active Member
Licensed User
Longtime User
try this
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
    Private TextField2 As TextField
    Private Serial1 As Serial
    Private ListView1 As ListView
    Private astream As AsyncStreams
    Private lista As List
   
    Private PortName As String = "COM1"
    Private BaudRate As Int = 9600
    Private DataBits As Int = 8
    Private StopBits As Int = 1
    Private Parity As Int = 0

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.RootPane.LoadLayout("Main")
    MainForm.Show
    lista.Initialize()
 
    Serial1.Initialize("Seriale") 'PUT ON A LIST ALL THE AVAILABLE PORTS
    For Each item In Serial1.ListPorts
        ListView1.Items.Add(item)
    Next
 
    Serial1.Open( PortName)
    Serial1.SetParams(  BaudRate, DataBits, StopBits, Parity)     'Baud normally 9600
    astream.Initialize( Serial1.GetInputStream, Serial1.GetOutputStream, "astream")        'Dati changed to astream

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
 
    Dim dati() As Byte
 
    dati = Array As Byte(0xF5, 0x01, 0x00, 0x01, 0x01, 0x00, 0x08, 0xFC) 'Write the array on the board
    astream.Write(dati)
 
End Sub

Sub Astream_NewData (Buffer() As Byte)
    lista.Add(Buffer)
   
    If TextField1.Text = "" Then
        TextField1.Text = lista.Get(0) 'bring the received buffer on a TextField
    Else
        TextField1.Text = TextField1.Text & lista.Get(0)
    End If
 
    lista.Clear 'list clear
 
End Sub

i used ByteConverter v1.1 with B4J 6.00 and it works
be sure that you copy the jar and xml file to external library of B4J
View attachment 62447

Thanks a lot for the reply! I have inserted the ByteConverter library, but is unused with that code.
The board still won't respond to me, but now the data I SENT to the board is arrived (I see the rx led on the board on when I click the button)...

@Erel: thanks a lot to you too, now I have to correct my code.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Make sure you declare the event exactly the same as the sub

astream.Initialize( Serial1.GetInputStream, Serial1.GetOutputStream, "astream") 'Dati changed to astream
Sub Astream_NewData (Buffer() As Byte)
Log( "Astream_NewData")
End Sub
 
Upvote 0

FreeWolF

Active Member
Licensed User
Longtime User
Thanks again, but nothing to do, the board won't respond. Maybe can be the parameter of the parity of the port that must be set to "pair", but I think, with that line:

B4X:
Private Parity As Int = 0

Is set to PAIR (btw I had tried with Int = 1 but without success)
 
Upvote 0
Top