B4R Tutorial [tool] External Serial Connector

Erel

Administrator
Staff member
Licensed User


This is a small B4J program that uses jSerial library to connect to an Arduino board.

To see it working start with this simple B4R program:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
End Sub

Sub Astream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
End Sub

Sub AStream_Error
   Log("error")
End Sub



In this mode the IDE will send a message to the serial connector to close the serial port during compilation and to open it afterward.

You can modify the code to send binary data instead of strings.

Serial connector requires jSerial v1.30+: https://www.b4x.com/android/forum/threads/jserial-library.34762/page-1
 

Attachments

Last edited:

Moch Firmansyah

Member
Licensed User
is like hyperterminal for direct serial communication, gateway only or virtual communication port as VSPD(virtual serial port driver)?
 

Beja

Expert
Licensed User
Thanks Erel, but sorry not following.. I see only one B4J app in which it sends eg4g from one editbox and receives it in another editbox in the same B4J app (unless I am missing something)
Can we have a block diagram of the system so we can see the different components?
Thanks.
 

Erel

Administrator
Staff member
Licensed User
There are two components:

1. The B4R program that uses AsyncStreams to read and write to the serial port.
2. The B4J desktop program that allows sending data over the serial port.

Try it. Run the B4R program and the B4J program.
 

Erel

Administrator
Staff member
Licensed User
Version 1.10 was uploaded to the first post. Starting from B4R v1.00 beta 8 the IDE can control the serial connector so it is no longer required to stop the serial connector before compilation.
 

jarda

Member
Licensed User
What run this
External Serial Connector
on B4J 4.20

reflector - not found
and
udpListener.Initialize2 - not found
 

positrom2

Active Member
Licensed User
Edit: Solved:
I had copied j... library files to the B4R libraries. Unchecking them in the library manager the program compiles.
By the way, I have several j... lib files in the B4R folder. Must they all be removed?

Original post:
Error message when compiling the code from first post. I have followed the two links at the end of the first post:
B4R version: 1.00
Parsing code. (0.00s)
Compiling code. Error
Error compiling program.
Error description: Cannot cast type: {Type=Stream,Rank=0, RemoteObject=True} to: {Type=java.io.InputStream,Rank=0, RemoteObject=True}
Occurred on line: 10
astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
Word: stream
 
Last edited:

Erel

Administrator
Staff member
Licensed User
All of B4R libraries start with r. Any other XML file should be deleted.
 

mrred128

Active Member
Licensed User
I removed all refs to network objects and removed jNetwork. It worked fine. Was this console made for something else initially?
 

Erel

Administrator
Staff member
Licensed User
The UDPSocket allows the IDE to control the serial connector. See the first post.
 
Top