B4J Question jSerial issue

Daren463

Member
Licensed User
Longtime User
I am setting up the port to receive 8 data bits. But the data is garbled unless I set the B4j program port to 7 data bits. If I send data from the B4J program with 8 data bits it is received properly. Am I setting the port wrong?

Private Sub btnOpen_Click
If lblStatus.Text = "Closed" Then
sp.Open(Port)
sp.SetParams(9600,8,1,0)
'sp.SetParams(BaudRate,DataBits,Parity,StopBits)
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
lblStatus.Text = "Open"
btnOpen.Text = "Close"


taSend.Text = DataBits
Else
sp.Close
lblStatus.Text = "Closed"
btnOpen.Text = "Open"
End If
End Sub
 

Daren463

Member
Licensed User
Longtime User
Sorry,
Here is the code

B4X:
Private Sub btnOpen_Click
    If lblStatus.Text = "Closed" Then
        sp.Open(Port)
        sp.SetParams(9600,8,1,0)
        astream.Initialize(sp.GetInputStream,  sp.GetOutputStream, "astream")
        lblStatus.Text = "Open"
        btnOpen.Text = "Close"
        
        
        taSend.Text = DataBits
    Else
        sp.Close
        lblStatus.Text = "Closed"
        btnOpen.Text = "Open"
    End If
End Sub
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
If is probably not the source of your problem given in the line above you define the port parameters explicitly, but I note in your original post

B4X:
'sp.SetParams(BaudRate,DataBits,Parity,StopBits)

however, the Parity and StopBits parameters should be swapped in their position.

I think to understand why your received data is garbled we would need to see your code which handles astream. Depending on the format of your data, using AsyncStreamsText may be preferred to AsyncStreams.
 
Upvote 0

Daren463

Member
Licensed User
Longtime User
Thanks for the reply.
Here is the code for the data handling.

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    LogMessage("You", s)
End Sub

Sub LogMessage(From As String, Msg As String)
    taRecieve.Text = taRecieve.Text & From & ": " & Msg & CRLF
    taRecieve.SetSelection(taRecieve.Text.Length, taRecieve.Text.Length)
End Sub
B4X:
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
I suggest you check out this post AsyncStreamsText - Useful when working with streams of text which commences by saying:

When a message (set of bytes) is sent over a network channel, of any type, there is no guarantee that the whole message will arrive as a single set of bytes.

The message might be split or merged together with other messages.

which may be why your received message is garbled and goes on to explain that:

AsyncStreamsText can help you if:
1. The data sent is text and not binary data.
2. Each message ends with an end of line character (both Unix and Windows formats will work).

I have certainly found use of AsyncStreamsText very convenient when sending text data over the serial port and that may be suitable for you too.
 
Upvote 0

Daren463

Member
Licensed User
Longtime User
I tried AsyncStreamsText and no joy.

The problem seem to be on the port setup side. When data in 8 bit form is sent to the b4j program set at 7 bits it works fine.
 
Upvote 0

Daren463

Member
Licensed User
Longtime User
I tried all the different paritys. I tried two different communication programs to send the data (As a test bed, I'm communicaing using two laptops). To test the hardware I used proven software on both laptops and the communication was perfect. Frankly I'm at a loss. The development machine is 64 bit running windows 10, B4j was just installed with the latest version, Java is jdk-11.0.1, and jSerial is version 1.31.

Is there another serial library to test with? I would like to convert all my old software tool from VB to B4J.
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
@Daren463 I've used jSerial in several projects with various speed and stop bit configurations, albeit only with 8 data bits and no parity (0), and haven't found any problems.

Are you willing to share the project or a cut-down version that exhibits the problem which we could test ourselves?
 
Upvote 0
Top