B4J Question Serial port problem

saunwin

Active Member
Licensed User
Longtime User
Morning,
Q. - I have a serial port connected to a Bluetooth adapter (one of those cheap SPP-C modules). I send it a couple of AT commands but the last character of the string is always missing. I can use teraterm and manually enter the AT commands aall is correct, when I log the sending strings all look correct, but the response always has the last character of the string missing. Sending the AT command from my app then reading back in teraterm confirms that the BT module has "dropped" the final character. so I must conclude there is something amiss in the sending routine.

Log also seems to be receiving some random CRLF's in the module replies. Any ideas ? TIA

Sending code

B4X:
Sub setbutton_MouseClicked (EventData As MouseEvent)
    If astream.IsInitialized = False Then Return
    Dim s As String
    Dim buffer() As Byte
    If btname.Text.Length > 0 Then
        s ="AT+NAME"&btname.Text&CRLF
        buffer = s.GetBytes("UTF8")
        astream.Write(buffer)
        Log("Sending: " & s)
    End If
    If Pinfield.Text.Length = 4 Then
        s ="AT+PIN"&Pinfield.Text&CRLF
        buffer = s.GetBytes("UTF8")
        astream.Write(buffer)
        Log("Sending: " & s)
    End If
End Sub


Reading code

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

saunwin

Active Member
Licensed User
Longtime User
OK - It's taken me all day and a poke about with my logic analizer but it's now fixed.
The SPP-C (Hc-06 copy) requires AT command termination with \r\n
This MAY help someone.

the line: -
s ="AT+NAME"&btname.Text&CRLF

should be :-
s ="AT+NAME"&btname.Text& Chr(13) & Chr(10)

&CRLF does not = \r\n - (only an \n is sent). Chr(13) is \r and Chr(10) is \n
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Thanks for that link DonManfred, I also found that although I was getting the correct response from the BT module, the AT commands were flakey and often not working at all. there needs to be a delay of about 2 seconds between AT commands. All seems good now.
 
Upvote 0
Top