I'm delving into the world of serial communication for the first time and wanted to use b4j as a base.
Looking around I see that I should be able to use jSerial lib to send / receive data through USB port to a USB to RS232 lead.
Just as a quick and dirty test I chucked this together
At the end of the 'Log()' line I have put the logged value. As you can see the Adaptor lead is indeed detected (it is Com5) but when I try to open the port I get an error that the port is not found..... Funny, you saw it a second ago
Any Clues?
Looking around I see that I should be able to use jSerial lib to send / receive data through USB port to a USB to RS232 lead.
Just as a quick and dirty test I chucked this together
B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
USB.Initialize("USB")
Log(USB.ListPorts) '(ArrayList) [COM5]
Log(USB.ListPorts.Get(0)) 'COM5
USB.Open(USB.ListPorts.Get(0))
astream.Initializeprefix(USB.GetInputStream, True, USB.GetOutputStream, "astream")
'USB.SetParams(9600, 8, 1, 0)
End Sub
Sub MainForm_Closed
USB.Close
End Sub
Sub astream_NewData (Buffer() As Byte)
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log(s)
End Sub
At the end of the 'Log()' line I have put the logged value. As you can see the Adaptor lead is indeed detected (it is Com5) but when I try to open the port I get an error that the port is not found..... Funny, you saw it a second ago
Waiting for debugger to connect...
Program started.
(ArrayList) [COM5]
COM5
Error occurred on line: 21 (Main)
jssc.SerialPortException: Port name - COM5; Method name - openPort(); Exception type - Port not found.
at jssc.SerialPort.openPort(SerialPort.java:167)
at anywheresoftware.b4j.serial.Serial.Open(Serial.java:80)
at b4j.example.main._appstart(main.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.start(main.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Any Clues?