B4J Question [Solved] RPi - Message from serial port is garbled

Mark Read

Well-Known Member
Licensed User
Longtime User
Still working on my datalogger, I have a new problem. I am using a usb to serial converter kabel (TTL) for the RPi. My main app in B4J did not work so I made a barebones version. After changing the cable for a new one as the first was damaged, the app works but the data is garbled. Can anyone shed light on what is happening here?

My code:

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private btn_Quit As Button
    Private Timer1 As Timer
    Private astream As AsyncStreams
    Private myserial As Serial
    Private uart_list As List
    Private rcvStr As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Timer1.Initialize("Timer1",5000)
       
    If astream.IsInitialized Then
        astream.Close
    End If
       
    myserial.Initialize("")
    uart_list = myserial.ListPorts
    Log(uart_list)
    Log(" ")
   
    myserial.Open(uart_list.Get(1))
    myserial.SetParams(9600,8,1,0)
    astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
    Log("Com Port Initialized: " & uart_list.Get(0))
    'Timer1.Enabled=True
End Sub

Sub Timer1_Tick
    Dim s1, s2, s3 As String
    s1=Rnd(0,360)
    s2=Rnd(0,500)/100
    s3="<STX>Q," &  s1 & "," & s2 & ", M, 00, <ETX> 16"
    Log("Sending: " & s3)
    astream.Write(s3.GetBytes("UTF8"))
End Sub

' Called when stream gets new data
Sub astream_NewData (Buffer() As Byte)
    rcvStr = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log("Newdata received: " & rcvStr)
   
End Sub

Sub btn_Quit_MouseClicked (EventData As MouseEvent)
    astream.Close
    myserial.Close   
    ExitApplication
End Sub

And the log data:



The data shoud be similar to this: <STX>Q,270,0.56, M, 00, <ETX> 16
 

Mark Read

Well-Known Member
Licensed User
Longtime User
According to support at FTDI, this cable cannot work. A standard USB to serial will and does work! Found a cheap cable with the ftdi chipset which worked as soon as it was plugged in. Data is correct.

Note: I measured the voltage from the sensor Rx/Tx lines and got 5,7V. This doesn't seem to bother the RPi when connected via USB.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…