B4R Tutorial [tool] External Serial Connector

SS-2016-04-12_15.23.57.png


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


SS-2016-04-25_09.48.44.png


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.

Updates

- v1.20 - Uses a small console app to get the ports description.

f5CT6pMMYn.png
 

Attachments

  • B4R_Serial_Connector.zip
    7.2 KB · Views: 1,487
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
When running under JDK9, Windows 10, B4J v5.9, after opening a COM port, the application crashes with error below.
Googled around and found this thread. Downloaded the mentioned updated jSCC.jar, copied to the additional B4J libraries ... but did not solve the issue.

Has anyone else experienced same issue under JDK9? Any workarounds?

BTW: Verified opening the COM port using the Arduino, is working fine.

B4X:
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000007110b5db, pid=11220, tid=10872
#
# JRE version: Java(TM) SE Runtime Environment (9.0+181) (build 9+181)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (9+181, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)
# Problematic frame:
# C  [jSSC-2.8_x86_64.dll+0xb5db]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Daten\b4\ztools\B4RSER~1\Objects\hs_err_pid11220.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks - indeed having multiple JDK versions installed and went back to JDK8.

Wondering if there are any advantages in using JDK9 vs JDK8 for B4J application development - but thats a different question...
 

raphael75

Active Member
Licensed User
Longtime User
In the AppStart Sub, there is one line "Wait For (GetPortsNames)" and one line "GetPortsNames".
Does it mean that the GetPortsNames Sub is run twice?

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1")
    MainForm.Show
    DataFolder = File.DirData("SerialConnector")
    serial.Initialize("")
    Wait For (GetPortsNames) Complete (unused As Boolean)
    cmbPort.Items.AddAll(PortsNames.Values)
    If File.Exists(File.DirApp, settingsFile) Then
        Dim m As Map = File.ReadMap(File.DirApp, settingsFile)
        If m.ContainsKey("port") Then
            Dim SavedPort As String = m.Get("port")
            If PortsNames.ContainsKey(SavedPort) Then
                cmbPort.SelectedIndex = cmbPort.Items.IndexOf(PortsNames.Get(SavedPort))
            End If
        End If
        chkReset.Checked = m.Get("reset")
    End If
    If cmbPort.SelectedIndex = -1 Then cmbPort.SelectedIndex = cmbPort.Items.Size - 1
    udpListener.Initialize2("udp", 51042, 8192, True, True)
    GetPortsNames
End Sub
 
Top