B4J Question JSerial question

rosippc64a

Active Member
Licensed User
Longtime User
Hi All,
I use the Jserial lib (v. 1.31) to read from serial port. I did a small class to do this. Nothing special, but I can't read anything, except if I place a brakpoint at some lines.
form code:
B4X:
Sub Process_Globals
    ...
    Private ser As MerlegClass
    ...
End Sub

Public Sub Show
    ...
    ser.Initialize(Me)
End Sub
Private Sub btnMeresTeszt_Click
    ser.setparam2(cmbComPort.Value,cmbComSpeed.Value,cmbComPar.Value,cmbProtokoll.Value)
    If ser.openport() Then
        Sleep(1)
        ser.startmeres
        wait for serial_result (receivedstr As String)
        xui.MsgboxAsync("Data received" & receivedstr,"INFO")
        Wait For Msgbox_Result (SResult As Int)
        ser.closeport
    Else
        xui.MsgboxAsync("Can't open.","HIBA")
    End If
End Sub
Here is the relevant class code. If I put a breakpoint one of signed line, the program can only read from serial port!
If I put a short delay without breakpoint, then can't read. No error message, just simple no read.
B4X:
public Sub openport As Boolean
    If com.ListPorts.IndexOf(comparam.port) > -1 Then
        If opened Then
            closeport
        End If
        Try
           'Breakpoint here
            com.Open(comparam.port)
            com.SetParams(comparam.speed,comparam.databit,comparam.stopbit,comparam.parity)
            astream.Initialize(com.GetInputStream, com.GetOutputStream, "astream")
            opened = True
            Log($"${comparam.port} opened"$)
            Return True
        Catch
            Log(LastException)
            Return False
        End Try
    Else
        Return False
    End If
End Sub
public Sub startmeres
'Breakpoint here
    Log($"${comparam.port} data sending"$)
    Select comparam.protokoll
        Case "F501"       
            Dim b() As Byte = Array As Byte (2,5,3)   
            astream.Write(b)
        Case "Dollar"
            astream.Write("$".GetBytes("ASCII"))           
    End Select
End Sub
'----------------------------------------------------------
'can't happen AStream_NewData without a brekpoint
Sub AStream_NewData (Buffer() As Byte)
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    Log($"${comparam.port} data arrived ${s}"$)
    If SubExists(resobj,"serial_result") Then
        CallSubDelayed2(resobj,"serial_result",s)
    Else
        Log($"serial_result not found!"$)
    End If
End Sub
Can somebody experieced problem like this?
 

rosippc64a

Active Member
Licensed User
Longtime User
I have to wait for at least 2sec to work well. Weird. Why?
B4X:
    If ser.openport() Then
        Sleep(2000)
        ser.startmeres
 
Last edited:
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
There is no events to check if port is opened, so I can log if I open the port, but not when it is done.
B4X:
COM17 opened 08:23:28 <--- there is the 2seconds needed to work well
COM17 data sending 08:23:30
COM17 data arrived   594.0 08:23:30
This is an arduino uno connected to win long before of starting the test.
 
Upvote 0
Top