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:

upload_2016-1-12_8-20-13.png


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

Roycefer

Well-Known Member
Licensed User
Longtime User
Use AsyncStreamsText instead of just AsyncStreams, it will make your life a lot easier. The way you're doing it now, you're basically throwing up random ASCII characters, some of which aren't valid.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Sorry Roycefer but I don't understand. Using the timer (commented out in line 37), I simulate what the sensor actually delivers. The output shown above IS from the sensor itself. If I use the "debug" timer, the data is also garbled.

I am not familiar with AsyncStreamsText.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Put this in your astream_NewData() sub and see if the output is any more intelligible.
B4X:
For j = 0 To Buffer.Length-1
    Log(j & " " & Buffer(j) & " " & Chr(Buffer(j)))
Next
For each Byte in the Buffer, it will Log the Byte's index, the raw Byte value (from -128 to 127) and the Unicode representation of the Byte.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Okay this is the result.

upload_2016-1-12_11-15-45.png
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I checked the sensor docu, it will only run at 9600 baud, 8 data bits, no parity, 1 stop bit and no handshaking! I set 9600,8,1,0 in line 34.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
If the TTL rx, tx is connected to RS232 that will not work. A RS232 transceiver level shifts and inverts, hence garbled data. The RS232 levels may damage TTL and related cable dongle circuitry. Just guessing based on your posts.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
@KitCarlson : That was the whole point of using the ttl usb adapter. I read that the RS232 levels were too high for the RPi and therefore an adapter must be used.
@Erel: Tried a simple Python script to read the port. Something is received but there are no characters visible.

I think the problem is the cable. If I run the B4J app on a windows XP PC, there are no problems.

Any other ideas???
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Mark, sorry I was unsure of your hardware connections.

If you have a scope or logic analyzer, they can be used to measure and interpret characters, baud ...

This url might be help. Sounds like garbled chars my happen due to lack of pull-up settings on early boards. Also levels 3.3V may not play well with 5V levels. If USB adapter is FTDI, I know there are small mods for 3.3V use. http://elinux.org/RPi_Serial_Connection
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
No I don't unfortunately.

I added a new sub with a timer. Every second I send a text string to the port (ttyUSB0). The TTL-cable is attatched and the lines Tx and Rx are shorted together. The data coming back is correct.

Now I am really stuck!
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Loop back works, if the baud rate is off, because rx, tx bauds match on same system. While it is set to 9600, has it been verified on "both" sides. It is possible to use a sound recorder as a cheap scope, to capture characters, and view if you have time reference for acquired data.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
So, I managed to install Putty on the RPi. I can login to the USB0 port and get data but it is still garbled.

I am wondering if this is the correct cable??? I am using the USB to Serial TTL from FTDI (TTL-232R-3V3-WE), similar to this:
TTL232RG.jpg

The USB side is connected to a RPi USB port and the bare wires have a 9 Pole sub D connector (self made). My sensor has a 9 Pole sub D connector. I have crossed the RX/TX lines.

Can someone confirm that this is correct?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

to share = have been testing as well with USB to Serial (look here) between PC and Raspberry Pi.
The biggest hazzle was to get the wiring correct for the 3 lines GND, TX, RX from the USB to the RS232 pins. There seems to be not a standard which color is what, so had to explore. Then after having the wires correct (data been flowing), find out the line speed as thought it was 9600 but turned out to be 115200. Did use Putty to checkout.
BTW: The TinkerForge Bricklet I have used is (just) routing the lines.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks for the info but I am not connecting to a PC. I am connecting to a wind sensor which is driven by a 9 volt battery and has a 3 wire RS232 output (Rx, Tx, GND).

Loop back works like a dream but my sensor data is meaningless. I have tried two of the above cables and cannot fix the problem.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
What data are you expecting to see?
From your first post it looks like you expect stx,Q,270,0.56, M, 00, etx 16
stx will be 0x02 (2 decimal) - an unprintable character
etx will be 0x03 (3 decimal) - unprintable again
Q would be 0x71 (113 decimal)
M would be 0x6D (109 decimal)
Is the data it sends between Q and M bytes? (you have 0.56) so it could be a float/double)
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
You are correct, the data should be <stx>,Q,270,0.56, M, 00,<etx> 16. The whole thing is sent as text, not numbers in that sense.
<stx> as decimal 2 - start of string
<etx> as decimal 3 - end of string
16 is the checksum of the data between stx and etx.
Q is the code for the sensor
270 is sent as text - wind direction
0.56 also as text with up to three leading zeros (???)
M is the mode - normally continuous
00 is a text error code
In total there should be about 26 bytes of data each second.

With the sensor connected to a windows PC and using hyperterminal, the data received is exactly as above. When conncting to the RPi with a usb -TTL adapter, the data is garbled.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
So, I managed to install Putty on the RPi. I can login to the USB0 port and get data but it is still garbled.

I am wondering if this is the correct cable??? I am using the USB to Serial TTL from FTDI (TTL-232R-3V3-WE), similar to this:
TTL232RG.jpg

The USB side is connected to a RPi USB port and the bare wires have a 9 Pole sub D connector (self made). My sensor has a 9 Pole sub D connector. I have crossed the RX/TX lines.

Can someone confirm that this is correct?

I looked up the FTDI cable on digikey, the instruction manual shows on pages 7, and 8 the color codes and pinouts. Orange is TX, Yellow RX, BLACK ground. On DB9 pin 5 is ground, pins 2,3 are used for tx,rx, but vary depending on dce, dce, male, female connector. Best to consult wind sensor manual.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Just a thought. Could it be that it makes a difference which way the cable is connected, ie. the USB connector is not meant to be used on the RPi side?
 
Upvote 0
Top