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
 

qle

Member
Licensed User
Longtime User
Thank you for your reply Erel,
I get no error with File.OpenOutput("/dev/eser0", "", false)
and f=File.OpenInput("/dev/eser0", "") with no errors.
so it may be possible i can communicate this way.
but have had little success so far, will keep trying, thank you for the tip.
i have the full demo app in java but unfortunately my skill at this level is nill.

Lee.
 

qle

Member
Licensed User
Longtime User
Thank you for your reply and time.
The compiled apk sample works fine.
I have only just installed eclipse ide to try and play with the source, but im not very good at this level, but chipping away slowly. (thats why i love b4a)
 

qle

Member
Licensed User
Longtime User
Good news! :sign0060:

I can now successfully read the laser scanner using f=File.OpenInput("/dev/eser0", "")

So i assume from this i will also be able to write to the device (for setting config parameters)

Now i just have to work out how to check for the scan key being pressed.

Thanks for your time and help.

edit: almost successful, i need to pass the baud rate
mSerialPort = new SerialPort("/dev/eser0",9600)

:(
 
Last edited:

banditanos

New Member
Licensed User
Longtime User
Great code !

:sign0098: Hi Qle,

I will try to catch with my tablet RS232 caracteres with a USB.serial adapters, it would be very great if you send me your code about serial port (open, port setting, send string, read string).

Thanks for your answer,

best regards.
 

Ferraz71

Member
Licensed User
Longtime User
Hi Qle...

Do you have a solution in b4a to reply the java code mSerialPort = new SerialPort("/dev/xxxxx",9600) ??. My tablet have a native RS232 serial in the /dev/ttymxc0. and i can read and write in this port with Asyncstreams, but i cant configure its baudrate or parity.... Can you help me??? sorry for my english....
 

Ferraz71

Member
Licensed User
Longtime User
Hi Qle...

Do you have a solution in b4a to reply the java code mSerialPort = new SerialPort("/dev/xxxxx",9600) ??. My tablet have a native RS232 serial in the /dev/ttymxc0. and i can read and write in this port with Asyncstreams, but i cant configure its baudrate or parity.... Can you help me??? sorry for my english....

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.
 

Attachments

  • SerialUART.zip
    12.2 KB · Views: 1,811
Last edited:

benji

Active 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.

How can i send info to the port?
 

Ferraz71

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
 

DrewG

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.
 

festez

Member
Licensed User
Longtime User
Hello to all.
I have a dual Wandboard with i.MX6 and UART1 on the board.
Using the library SerialLib provided by Ferraz71, I can determine the number and the names of the various ports of the card.
The code is as follows:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim c As PortFinder
    Dim d As SerialPort
    Dim Label1 As Label
    Dim Spinner1 As Spinner
    Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    Label1.text = "Number Devices:" & c.AllDevicesPath.Length '(number of devices)
    Spinner1.AddAll(c.AllDevicespath) '(device Path List)
End Sub

Sub Button1_Click
   d.SetPort(Spinner1.SelectedItem,115200,0) 'set serial port..
End Sub


Unfortunately, when I try to open the resource /dev/ttymxc0 I get the error java.io.IOException.


I also tried to directly open the resource /dev/ttymxc0 with the command
B4X:
File.OpenInput("/dev/ttymxc0", "")
.
In this case I got the error java.io.FileNotFoundException: /dev/ttymxc0: open failed: EACCES (Permission denied)


Is there anyone who can help me?
Thanks
 

festez

Member
Licensed User
Longtime User
Hello Erel and thank you for answer.
If I connect a null modem cable to my pc, I can read all the information via the console (with putty.exe).
I seem to be already logged in as root.
I attach a file containing the information that I read through the console.

I also thought that access to the resource /dev/ttymxc0 is prevented as is already being used by the console.
In fact, I stopped U-Boot and I typed the following commands:

B4X:
setenv bootargs console= init=/init video=mxcfb0:dev=hdmi,1280x720M@60,if=RGB24,bpp=32 video=mxcfb1:off video=mxcfb2:off fbmem=28M vmalloc=400M androidboot.console= androidboot.hardware=freescale
saveenv
boot

After doing this, the console appears to be disabled (putty does not read any more information), but always reflected the following errors: java.io.FileNotFoundException and java.io.IOException

You have any other suggestions?

Unfortunately I do not know the Java programming (otherwise I would not be here in this forum). Is there any way (even with native Java programs) to test if it works the resource?

Thank you so much in advance for your help.

Sorry for my english.
 

Attachments

  • boot.txt
    20 KB · Views: 505

festez

Member
Licensed User
Longtime User
Hello.

Also you think that the reason why the resource is that it is not open already used by the console?
Otherwise, what do you think could be the reason? (because when I connect the cable to the PC I read all the information with the console).
 

festez

Member
Licensed User
Longtime User
I'm sorry, I wanted to know if you've ever used the command File.OpenInput ("/dev/ttymxc0", "") (or other commands) to open a serial port on Android.

My goal is to use my Wandboard for building automation.
But I have not understood if the problem is my board, or the library that I am using.
 
Last edited:
Top