B4A Library Open/Read/Write - UART Serial device

I need to open/read/write a serial (UART) device - "/dev/eser0"
(a laser scanner)

Im unsure if b4a can do this, or if th IOIO lib can achieve this?

I do have a "libserial_port.so" but am unsure how it can be used in b4a.
Ive spent the day searching and reading the forum, but im none the wiser!

Best Regards
Lee
 

Ferraz71

Member
Licensed User
Longtime User
this library just work in devices where the serial port is unlocked like in the mxt models, where is not necessary an external driver.
 

Ferraz71

Member
Licensed User
Longtime User
Thank you for this, it works on a Boundary Devices iMX6 N6x board on the ttymxc0 hardware serial port.

Only thing to note is that you need to use flag '2' and not flag '0' if you want read and write access to the port, with flag '0' I could only read.
it depends your device model, in my case my imx device can read and write using the flag set to zero
 

mrodriguez

Member
Licensed User
Longtime User
Hi, I'm using the usblibrary that supports FTDI chip but the problem is that this library doesn't supports 2 FTDI chip on the same android.
Can I use this library instead usblibrary?
Does it work with FTDI chip?
I need UART instead RS232 signal, what adapter do you recommend me?

I need to connect 2 uart device to my android to send serial command.
I have tried usblibrary+FTDI chip and IOIO board. It is a good solution for connecting 1 serial device.
Thanks
 

EvgenyB4A

Active Member
Licensed User
Longtime User
Benji..

use
.........................................
Dim c As PortFinder
Dim d As SerialPort
Dim x As AsyncStreams
.........................................

c.AllDevicespath.Length //(number of devices)
c.AllDevicespath// (device path list)
d.SetPort("string adress",int baud rate, byte flags) //set serial port..
x.Initialize(d.InputStream,d.OutputStream,"Astream ") // connect with asyncstream

where Astream is a function that receive the input buffer
likes this:
Sub Astream_NewData (Buffer() As Byte)
your commands here
end sub

and for write to the port use

x.write(buffer) 'where buffer is a byte array

or

x.write2(buffer,initial byte, lenght) 'to write some bytes inside buffer array

Hi
I try to use your library. I can send from Android device to PC the strings, but can't receive from PC.
And the second problem is that the program is stacked on the line d.SetPort... in debugger very often. It is need to turn off and turn on the device.
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

Would you tell me how to using the parameter flags of the function setport ? 0 is what ? 2 is what ,,,,,,, :p
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
B4X:
.........................................
Dim c As PortFinder
Dim d As SerialPort
Dim x As AsyncStreams
.........................................
Label1.text=c.AllDevicespath.Length //(number of devices)
Spinner1.AddAll(c.AllDevicespath)// (device path list)
d.SetPort("/dev/ttymxc0",9600,0) //set serial port..
x.Initialize(d.InputStream,d.OutputStream,"Astream") // connect with asyncstream

Hi

How did i know d.setPort Ok or NG ? Because d hasn't IsInitialized !
 

vdudukov

Member
Licensed User
Longtime User
Hi

I have laser barcode scanner with uart port. My virtual folder is /dev/ttyMT2.

I tried to connect it with:
B4X:
Dim d As SerialPort
Dim x As AsyncStreams

d.SetPort("/dev/ttyMT2",9600,0)
x.Initialize(d.InputStream,d.OutputStream,"Astream")

Sub  Astream_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log(msg)
End Sub

But with no success :-(

Everytime i must initialize port, or set TIMER.

I downloaded serial port app from GooglePlay, and everything works fine.

How can i do it with B4A ?
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Do you have read/write right for "/dev/ttyMT2" ? chmod 777 /dev/ttyMT2
 

vdudukov

Member
Licensed User
Longtime User
Do you see any error message?

No, i do not see any message. Just when i create timer, and everytime tick, message from ttyMT2 file came to log. In this case EAN Code.

Sub timer1_Tick
d.SetPort("/dev/ttyMT2",9600,0)
x.Initialize(d.InputStream,d.OutputStream,"Astream")
End Sub


Do you have read/write right for "/dev/ttyMT2" ? chmod 777 /dev/ttyMT2


I put chmod 777, but then i got message java.io.IOException
 

jarda

Member
Licensed User
Longtime User
Hi.... :sign0060::sign0060::sign0060:

Erel,

i did make a small change in the serial_port_api and now i can list, read and write in all serial devices in my tablet ( in my case... the port /dev/ttymxc0 is unblocked). with this library now i can set the baudrate, port adress and flags in all devices.

there are two types created. "PortFinder" and "SerialPort"

.........................................
Dim c As PortFinder
Dim d As SerialPort
Dim x As AsyncStreams
.........................................

Label1.text=c.AllDevicespath.Length //(number of devices)
Spinner1.AddAll(c.AllDevicespath)// (device path list)
d.SetPort("/dev/ttymxc0",9600,0) //set serial port..
x.Initialize(d.InputStream,d.OutputStream,"Astream") // connect with asyncstream





this is the compiled library with the. jar and .xml... for android 2.3 and higher
enjoy it!!


thanks a lot for your help...i am very very happy with de b4a.


Hi

We downloaded a library UART.

Unfortunately for me B4A ver 4.3 fails Declaration (Android 4.0.4)

e.g.
dim ser as serialport

when you start (on HW with 2xUART) getting the message

"Unfortunat App has stopped"

--------------------------- code
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.


Dim ser As SerialPort

End Sub

Sub Activity_Create(FirstTime AsBoolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed AsBoolean)
End Sub

Jarek


------------- Log
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
main_globals (java line: 401)
java.lang.NoSuchMethodError: android_serialport_api.SerialPort.<init>
at b4a.example.main._globals(main.java:401)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at b4a.example.main.initializeGlobals(main.java:306)
at b4a.example.main.afterFirstLayout(main.java:97)
at b4a.example.main.access$100(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Last edited:

vdudukov

Member
Licensed User
Longtime User
Can somebody reply to my previous post, please.
I am trying to understand where is the problem with my code and serial lib...

Thank You.
 

vdudukov

Member
Licensed User
Longtime User
I tried Serial Port Api sample, and it is working very good.
Can we implement this solution in b4a?
 

vincentehsu

Member
Licensed User
Longtime User
Sirs:

I wanna communcate with SPI interface via USB OTG
When i try to use
B4X:
d.SetPort("/dev/ttyUSB0",9600,0)

i get the errors

B4X:
java.io.IOException
    at android_serialport_api.SerialPort.SetPort(SerialPort.java:72)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:747)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example1.main.afterFirstLayout(main.java:102)
    at b4a.example1.main.access$000(main.java:17)
    at b4a.example1.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7237)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 

androh

Member
Licensed User
Longtime User
B4X:
d.SetPort("/dev/ttyUSB0", baudrate, flags)
Is there any detailed documantation about "flags" parameter?
 
Top