none
Error opening COM4 on my HC05 Bluetooth Module RRS feed

  • Question

  • Good day!
    Would please somebody help me. COM4 returns "Not Connected" everytime I ask a connection request to my HC05 via BT connection, but the module led's indicate that the module has been paired!

    Thank you.

    Visual BaSic Code:
    ' arduinoVBserial
    ' Arduino and Visual Basic: Receiving Data From the Arduino
    ' A simple example of sending and receiving data to and from an Arduino
    ' See martyncurrey.com
    '
    
    Imports System
    Imports System.IO.Ports
    
    
    
    Public Class Form2
        ' Global variables
        Dim comPORT As String
        Dim receivedData As String = ""
        Dim connected As Boolean = False
        Dim count = 0
    
        ' When the program starts; make sure the timer if off (not really needed) and add the available COM ports to the COMport drop down list
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = False
            populateCOMport()
        End Sub
    
        'The refresh button updates the COMport list
        Private Sub refreshCOM_BTN_Click(sender As Object, e As EventArgs) Handles refreshCOM_CB_BTN.Click
            SerialPort1.Close()
            populateCOMport()
        End Sub
    
        Private Sub populateCOMport()
            comPORT = ""
            comPort_ComboBox.Items.Clear()
            For Each sp As String In My.Computer.Ports.SerialPortNames
                comPort_ComboBox.Items.Add(sp)
            Next
        End Sub
    
        Private Sub comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
            If (comPort_ComboBox.SelectedItem <> "") Then
                comPORT = comPort_ComboBox.SelectedItem
            End If
        End Sub
    
        ' When the Connect button is clicked; if a COM port has been selected, connect and send out a HELLO message.
        ' Then wait for the Arduino to respond with its own HELLO.
        ' When the HELLO is received we are connected; change the button text to Dis-connect.
        Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
            comPORT = comPort_ComboBox.SelectedItem
            If (connect_BTN.Text = "Connect") Then
                If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 9600
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
    
                    SerialPort1.Open()
    
                    'See if the Arduino is there
                    count = 0
                    SerialPort1.WriteLine("<HELLO>")
                    connect_BTN.Text = "Connecting..."
                    connecting_Timer.Enabled = True
                Else
                    MsgBox("Select a COM port first")
                End If
            Else
                'connect_BTN.Text = "Dis-connect"
                'close the connection a reset the button and timer label
                Timer1.Enabled = False
                Timer_LBL.Text = "Timer: OFF"
                SerialPort1.Close()
                connected = False
                connect_BTN.Text = "Connect"
                populateCOMport()
            End If
    
    
        End Sub
    
        'The connecting_Timer waits for the Arduino to say HELLO.
        ' If HELLO is not received in 2 seconds display an error message.
        ' The connecting_Timer is only used for connecting
        Private Sub connecting_Timer_Tick(sender As Object, e As EventArgs) Handles connecting_Timer.Tick
            connecting_Timer.Enabled = False
            count = count + 1
    
            If (count <= 8) Then
                receivedData = receivedData & ReceiveSerialData()
    
                If (Microsoft.VisualBasic.Left(receivedData, 5) = "HELLO") Then
                    'if we get an HELLO from the Arduino then we are connected
                    connected = True
                    connect_BTN.Text = "Dis-connect"
                    Timer1.Enabled = True
                    Timer_LBL.Text = "Timer: ON"
                    receivedData = ReceiveSerialData()
                    receivedData = ""
                    SerialPort1.WriteLine("<START>")
    
                Else
                    'start the timer again and keep waiting for a signal from the Arduino
                    connecting_Timer.Enabled = True
                End If
    
    
            Else
                'time out (8 * 250 = 2 seconds)
                RichTextBox1.Text &= vbCrLf & "ERROR" & vbCrLf & "Can not connect" & vbCrLf
                connect_BTN.Text = "Connect"
                populateCOMport()
            End If
    
    
    
        End Sub
    
    
    
    
    
        'After a connection is made the main timer waits for data from the Arduino
        'Only OK1, OK2 or OK3 is available.
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            receivedData = ReceiveSerialData()
            RichTextBox1.Text &= receivedData
    
            Dim tmp As String
            tmp = Microsoft.VisualBasic.Left(receivedData, 3)
    
        End Sub
    
        Function ReceiveSerialData() As String
            Dim Incoming As String
            Try
                Incoming = SerialPort1.ReadExisting()
                If Incoming Is Nothing Then
                    Return "nothing" & vbCrLf
                Else
                    Return Incoming
                End If
            Catch ex As TimeoutException
                Return "Error: Serial Port read timed out."
            End Try
    
        End Function
    
    
        'Clear the RecievedDtaa test box
        Private Sub clear_BTN_Click(sender As Object, e As EventArgs) Handles clear_BTN.Click
            RichTextBox1.Text = ""
        End Sub
    
    
    
    
    
    
        'The OK button acts as soon as the button is clicked. It does not wait for the user to release the button
        'There is no of for the OK button
        Private Sub ok_BTN_MouseDown(sender As Object, e As MouseEventArgs) Handles ok_BTN.MouseDown
            If (connected) Then
                SerialPort1.WriteLine("<NKOK>")
            End If
    
        End Sub
    
    
    
        'Add the start and end markers to the contents of the text box and then send to the Arduino.
        'Does not clear the contents
        Private Sub send_BTN_Click(sender As Object, e As EventArgs) Handles send_BTN.Click
            If (connected) Then
                Dim tmp As String
                tmp = "<" & send_TB.Text & ">"
                SerialPort1.WriteLine(tmp)
            End If
        End Sub
    
        Private Sub up_BTN_Click(sender As Object, e As EventArgs)
    
        End Sub
    
        Private Sub down_BTN_Click(sender As Object, e As EventArgs)
            If (connected) Then
                SerialPort1.WriteLine("<NUON>")
            End If
        End Sub
    
        Private Sub ok_BTN_Click(sender As Object, e As EventArgs) Handles ok_BTN.Click
    
        End Sub
    End Class
    Thank You! :)
    • Edited by DwainDCP Thursday, March 29, 2018 6:32 AM It is HC05 that I am using, not HC06
    Thursday, March 29, 2018 6:10 AM

All replies

  • COM4 returns "Not Connected" everytime I ask a connection request to my HC06 via BT connection, but the module led's indicate that the module has been paired!

    You have to do this in small steps.  Start by writing a routine that shows every byte (eg, in HEX format) that is received from the device after the connection is attempted.  Only when you have seen this data as it actually occurs can you write the code to test the result.

    Thursday, March 29, 2018 6:32 AM
  • Thank you! ButI could not check any data received at the serial because i cannot connect to my module by using that VBasic App. but "Bluetooth Serial Terminal" app from the market could transmit. I wonder why I could not connect to my HC05.
    Thursday, March 29, 2018 6:37 AM
  • Thank you! ButI could not check any data received at the serial because i cannot connect to my module by using that VBasic App.
    How do you know that you can't connect?   The code you have depends on a particular response from the device.  You need to start by seeing whether there is any response, including data that might not be displayable as characters in a text box.
    Thursday, March 29, 2018 7:04 AM
  • Thank you! ButI could not check any data received at the serial because i cannot connect to my module by using that VBasic App. but "Bluetooth Serial Terminal" app from the market could transmit. I wonder why I could not connect to my HC05.

    I agree with Acamar, baby steps. I can't see in your code where there is a not connected statement.

    One thing to try is immediately after you open the port, SerialPort1.Open,  try setting its SerialPort1.DtrEnable property to true.  Put a breakpoint on that line which will tell us if you have opened the port.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Thursday, March 29, 2018 3:45 PM
  • Thank you! ButI could not check any data received at the serial because i cannot connect to my module by using that VBasic App. but "Bluetooth Serial Terminal" app from the market could transmit. I wonder why I could not connect to my HC05.

    But you didn't write the "Bluetooth Serial Terminal" app from the market so why would you believe what you wrote is exactly how the "Bluetooth Serial Terminal" app from the market is written?

    La vida loca

    Saturday, March 31, 2018 2:21 AM