Android Question RN41 and a PIC Microcontroller

Gavin O'Connor

Member
Licensed User
Hi All,

I have a RN41 Bluetooth Module connected to a PIC Microcontroller and I have it set-up at a baud rate of 9600.

I have everything working well for the exception I am reading certain parts on the incoming string "not all connected", for example I trying to read a value of 8.27mpy and sometimes I get 8.27mpy but most of the time I get:

8 (first)
and then only only .29mpy

I have tried everything as I took Erel's Bluetooth example (BTW which is brilliant) and tried to use Asyncstreams without a prefix and a timer delay and even tried it with AsyncstreamsText (of which does not show any data from the incoming string, somehow I have perhaps confused myself trying a hundred different things,

please see below, perhaps there is something I have missed?, any help would be greatly appreciated,

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
   
    Private ast As AsyncStreamsText
    Dim timerstreams As Timer
    Dim AStream As AsyncStreams
   
End Sub

Sub Globals
   
    Dim myBuffer As String
    Dim txtInput As EditText
    Dim txtLog As EditText
    Dim btnSend As Button
    Dim ticksOfInactivity As String
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    'timerstreams.Initialize("timerStreams", 100)
   
    Activity.LoadLayout("2")
   
    'If AStream.IsInitialized = False Then
    If ast.IsInitialized = False Then
        'AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStream")
        ast.Initialize(Me,"ast", Main.serial1.InputStream, Main.serial1.OutputStream)
    End If
    txtLog.Width = 100%x
End Sub



Sub AStream_NewData (Buffer() As Byte)
   
    'myBuffer = myBuffer & LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
    'ticksOfInactivity=0
   ' timerstreams.enabled=True
   
     LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))

     'If Buffer.Length >= 6 Then
    'myBuffer =  BytesToString(Buffer, 0, Buffer.Length, "UTF8")

    'LogMessage("You",myBuffer)

    'End If

End Sub

Sub AStream_Error
   
    ToastMessageShow("Connection is broken.", True)
    btnSend.Enabled = False
    txtInput.Enabled = False
   
End Sub

Sub AStream_Terminated
   
    AStream_Error
   
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    If UserClosed Then
        AStream.Close
    End If
   
End Sub

Sub txtInput_EnterPressed
   
    If btnSend.Enabled = True Then btnSend_Click
   
End Sub

Sub btnSend_Click
   
    AStream.Write(txtInput.Text.GetBytes("UTF8"))
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Me", txtInput.Text)
   
End Sub

Sub LogMessage(From As String, Msg As String)
   
    txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
    txtLog.SelectionStart = txtLog.Text.Length
   
End Sub
 

JordiCP

Expert
Licensed User
Longtime User
It is normal that messages can arrive broken.
If the sender ends messages with \n , \r or \r\n then asyncstreamsText should work. Even if the messages end with any other delimiter, the class could be customized for that
If it does not exist, (i.e., the sender just writes text messages with no end of line nor other special chars), the only way is to "join" them while receiving and make your own criteria based on text length, parsing its content (if based on message start you can know its length, etc.)

Relying only on timing will give you headaches, since they will not be necessarily the same
 
Upvote 0

Gavin O'Connor

Member
Licensed User
Hi Jordi,

Thanks for the help, currently I have the 8.29mpy terminated with the <CR> and <LF> so I assume that should work?, in the code above I am not seeing any New Data coming in, do I have that Set Up Correctly?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Strange. What I would do

Check (if you have an oscilloscope/logical analyzer) that there is data coming through the serial port
Also check that the micro can receive data by sending some text through asyncstreamsText

Take into account that the event signature changes in case of asyncstreamtext
B4X:
Sub ast_NewText(Text As String)


I have an RN42 module with me, I think they are compatible. Let me check if it works...
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
This has worked for me. It is based on your project but all in one module and with only a button. Check the logs to see if it is receiving



--EDIT--
My code is assuming that there is only one device visible. You will have to change it if there is more than one
 

Attachments

  • testRN41.zip
    7.4 KB · Views: 334
Last edited:
Upvote 0

Gavin O'Connor

Member
Licensed User
Hi Jordi,

Thanks for all the Help, I have it working very nicely, code below:

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
   
    Dim ast As AsyncStreamsText
    Dim timerstreams As Timer
    Dim AStream As AsyncStreams
   
   
End Sub

Sub Globals
    Dim myBuffer As String
    Dim txtInput As EditText
    Dim txtLog As EditText
    Dim btnSend As Button
    Dim ticksOfInactivity As String
    Dim serial1 As Serial
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   
    Activity.LoadLayout("2")
   
    If ast.IsInitialized = False Then
    ast.Initialize(Me,"Astream", Main.serial1.InputStream, Main.serial1.OutputStream)
    End If
    txtLog.Width = 100%x
       
End Sub

Sub AStream_NewText (received As String)
         
          LogMessage ("Received:", received) 
       
    End Sub


Sub Astream_Error
   
    ToastMessageShow("Connection is broken.", True)
    btnSend.Enabled = False
    txtInput.Enabled = False
   
End Sub

Sub Astream_Terminated
   
    Astream_Error
   
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    If UserClosed Then
        ast.Close
    End If
   
End Sub

Sub txtInput_EnterPressed
    If btnSend.Enabled = True Then btnSend_Click
End Sub

Sub btnSend_Click
   
    AStream.Write(txtInput.Text.GetBytes("UTF8"))
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Me", txtInput.Text)
   
End Sub

Sub LogMessage(From As String, Msg As String)
   
    txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
    txtLog.SelectionStart = txtLog.Text.Length
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…