Android Question How do I get setup port in (((HC_05)))

Achilles

Member
B4X:
Private Function SetupPort(ByRef objCom As MSComm)
   With objCom
       .Break = False
       .DTREnable = True
       .EOFEnable = False
       .Handshaking = comNone
       .InBufferCount = 0
       .InBufferSize = 1024
       .InputLen = 1024
       .InputMode = comInputModeText ' or comInputModeBinary
       .NullDiscard = False
       .OutBufferCount = 0
       .OutBufferSize = 1024
       .ParityReplace = Chr(0)
       .RThreshold = 1
       .SThreshold = 0
       .Settings = "9600,N,8,1" 'default settings
   End With
End Function
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Can you provide some context here? What are you trying to do? If you are trying to communicate between an Android device and an HC-05 module then this is not the place where you would normally start. But maybe you are trying something different.
 
Upvote 0

Achilles

Member
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have looked at both of your threads. This is what I think that you are trying to do ...

- You have eight LEDs controlled by an Arduino.
- A data file defines a sequence to switch the LEDs on and off.
- You want an app to read the file and transmit the data to the Arduino via Bluetooth.

You appear to have two problems ...

A - The code that reads the data file is not working as you expect (first thread).
B - The Bluetooth link between Android and Arduino is not working (this thread).

Your code to read the file is extremely complicated but it seems to almost do the job - it misses the final pattern (problem 'A'). Let us leave that for now.

In this thread we need to look at problem 'B'. The �� symbols that you have seen indicate data errors, so the Bluetooth link is not working properly. I am attaching a Bluetooth Testbed app. Write a new Arduino sketch (I do not use Arduino myself) that waits for a single incoming character from the Bluetooth serial link and responds by sending a short ASCII string back over Bluetooth. The ASCII string must end with a chr(10) - a Line Feed character. Open the Testbed app on your Android device, search for and connect to your HC-05, then press any of the app buttons. You should see your ASCII message from the Arduino displayed in the app. Be sure that you are driving the HC-05 Rx input with 3.3v, not 5v. If you see the error "Link error : java.io.IOException: Broken pipe" in the B4A log then stop and restart the Testbed.

If this works look at the Testbed code and compare it with your own Android code - it might show where you are going wrong. If it does not work then post your Arduino sketch - there might be an error there. We do need to do this. I spent several months trying to debug a Bluetooth connection before I discovered that the problem was in the HC-05 device code. Not all HC-05s work.
 

Attachments

  • Testbed.zip
    11.9 KB · Views: 58
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
This code example might help you sort out your other problem (Problem "A" above) ...

B4X:
Sub convert                     ' Build bytes from "bit-string"
    Dim input As String = "01000001010000100100001101000100"
    Dim i, n As Int = 0
    Dim ch As Char
    Do While (i < input.length)            ' Test each character in turn
        n = Bit.ShiftLeft(n, 1)                ' Prepare to add next bit
        ch = input.CharAt(i)             ' Get next input character
        If (ch = "1") Then n = n + 1    ' Set next bit in output
        If (i Mod 8) = 7 Then           ' Check for 8-bit boundary
            Log(Chr(n))          ' This is the decoded byte
            n = 0                  ' Reset for next byte
        End If
        i = i + 1         ' Move to next input character
    Loop
End Sub
 
Upvote 0
Top