B4J Library jSerial library

Status
Not open for further replies.
The jSerial library allows you to open and communicate with other devices through the computer COM ports.

It can also be used to communicate with Bluetooth devices over a virtual com port.
(jBluetooth library is now available: https://www.b4x.com/android/forum/threads/jbluetooth-library.60184/)

SS-2013-11-21_15.46.10.png


The attached examples implement an Android <-> PC chat app.

In order to run the example you need to first pair the PC and the device and make sure that the Bluetooth is mapped to a virtual com port.

jSerial works with AsyncStreams. Both standard mode and prefix mode are supported. Note that prefix mode can only work if both sides of the connection follow the protocol.
Change AStream.InitializePrefix to AStream.Initialize to disable prefix mode.

jSerial depends on an open source project named JSSC: https://github.com/java-native/jssc

Updates:
v1.32 - Based on jssc v2.9.4.
v1.31 - Adds missing BAUDRATE, DATABITS, STOPBITS and PARITY constants.
v1.30 - Adds support for Java 11 and 64 bit computers based on: https://github.com/java-native/jssc

Instructions related to jSerial and B4J Packager 11: https://www.b4x.com/android/forum/threads/jserial-library.34762/post-735742
 

Attachments

  • B4J_Chat.zip
    1.7 KB · Views: 4,898
  • B4A_Chat.zip
    9.2 KB · Views: 2,918
  • jSerial.zip
    500.3 KB · Views: 1,257
Last edited:

vcelak

Member
Licensed User
Longtime User
Hello, writing to astream here is buggy, becausy physicaly it outputs zero beginning data to serial port - here are 00 00 00 0b and then my data, so I thing it send out as first 4 bytes length of array or something wrong.

bug_b4j_astream_jserial.png
bug_b4j_astream_jserial.png
 

vcelak

Member
Licensed User
Longtime User
You Are right, my error. I changed it to normal mode and everything is working well now - so if you like to "noramlly" communicate with device and not with b4a or b4j, then you must use normal initialization, not prefix mode.

[16/12/2013 11:34:07] Written data
53 46 46 30 30 0d 0a SFF00..
[16/12/2013 11:34:07] Read data
53 46 46 30 30 32 45 45 32 33 31 35 34 33 37 33 SFF002EE23154373
32 33 33 32 30 0d 0a 23320..
 

vcelak

Member
Licensed User
Longtime User
I have tested this lib under Ubuntu linux and it is working only, when you run app as root, otherwise it cannot find any ports ;)
 

vcelak

Member
Licensed User
Longtime User
Note that if you are sending text and cannot use prefix mode then you can still use AsyncStreamsText class. It will collect and split the message based on the end of line characters.

Yes, but there is our embedded ascii based communication protocol, where CRLF is used to msg end indication and as I found, in b4a it is not identical when working only with text - like line ends. So I prefer this way and to manually add CR and LF chars to end of message.
 

LucaMs

Expert
Licensed User
Longtime User
When I try to open the only port it lists, COM3, I get the following message:
jssc.SerialPortException: Port name - COM3; Method name - openPort(); Exception type - Port busy.

I do not know what the COM3 match, or what is connected to it. In Windows 7 there isn't a list of the ports, differently to older versions of Windows. (There is some old DOS command to get a list of the ports?)

I do not know if this may be due to the fact that I do not have the ideal BlueTooth driver for my SmartPhone (damn Italian brand, on their website do not exist drivers!); although I think it is a different problem (because, oddly enough, I can upload files from PC to SmartPhone using Windows services).

Anyone can do miracles? :)

Thanks in advance

P.S. Now I can see "port (COM & LPT)" in "Gestione dispositivi" (in italian, Devices Manager, in english, perhaps), but there is only a COM4 that i "made" to obtain this section!
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
This means that this port is already opened by a different process / driver.

Thanks, but i don't know how to discover wich process/driver (and how to use other ports)

P.S. I believe that COM3 is used right from my smartphone (dial-up - DUN). There is no solution, without having the appropriate driver
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
COM3 is often reserved for modem connection (old telephone line, RJ11 port). Maybe you will find it in your Device Manager > Modems.

Have you created a new COM port under Bluetooth settings ?
You can have a look at this page http://www.flightprep.com/rootpage.php?page=w7btsetup (see step 10).


It is strange ... thanks to your suggestion, I have found a:
"Standard Modem over BlueTooth connection" ... Bah, I know the old modem, this thing I do not understand, but it was on COM3 (changed to 256, but I'll disable it soon).

Anyway, I think the main problem is the lack of a suitable driver. and the fact that PC to smartphone features something ... Probably, I have installed a Russian driver, a long time ago, which is why, from PC to SmartPhone something works, but there is always the yellow icon that indicates problems.

Thank you, Raphael
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi, trying to control a modem, just getting back the command I sent, have set the asyc stream to no prefix....any suggestions. the command I am issuing is AT+CGSN, return the serial number of the modem.
 
Hello Erel, I want to start off by saying that this is an excellent project you have created. I have used it as a template for my own project. I want to communicate with my PIC microcontroller by connecting the serial port on my laptop to the USART on the microcontroller. I have been able to successfully transmit bytes to the PIC microcontroller using the jSerial library and your example code. However, when I try to receive bytes from the PIC microcontroller, an AStream Error occurs (laptop end) and I am unable to receive data. It is my understanding that the following subroutine will be executed when new data is received by the laptop serial port:

Sub AStream_NewData (Buffer() As Byte)
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
recieved_data.Text=s
End Sub

I want the received ASCII character to be received and displayed by the label "recieved_data".

Any suggestions would be appreciated.
 

rdkartono

Member
Licensed User
Longtime User
Hi Erel,

I use B4J for programming with Raspberry Pi. Need to communicate with GPS receiver module.
I use jserial 1.05 with GPS receiver module.
For several seconds work well, can receive GPS data. After that always terminated with message :
terminate called after throwing an instance of std::bad_alloc.

My GPS Port initialization :
...
Private GPS_PORT As Serial
.......
.......
GPS_PORT.Initialize("")
GPS_PORT.Open("/dev/ttyAMA0")
GPS_PORT.SetParams(4800,8,1,0)
GPS_PORT.PurgePort(Bit.OR(GPS_PORT.PURGE_RXABORT,GPS_PORT.PURGE_RXCLEAR))
ASP.Initialize(GPS_PORT.GetInputStream,Null,"GPSPORT")
...........
...........
Sub GPSPORT_NewData(datanya() As Byte)
Dim ss As String = BytesToString(datanya,0,datanya.Length,"UTF8")
Log(ss)
End Sub
...........
...........
I make simple the function GPSPORT_NewData for this question, just to show you that LOG will output some serial data. After few seconds, program always break with std::bad_alloc.
I have tested using Java 1.7 (original Raspberian) and Java 1.8, resulting the same.

Any advice ?

Regards,

Rudy Darmawan
 
Status
Not open for further replies.
Top