VB.net Serialport Response

14 Jun 2012

I've written a connection/rejection code for my windows application which should try to send my mbed a string and respond with a string but i'm running into a problem!

The mbed receives the string and sends its value back but my application does not wanna have it?

Here is my code for vb

While (portnumber < 256)
            SerialPort1.ReadTimeout = 200
            Try
                SerialPort1.PortName = com + portnumber.ToString 'COM1 COM2 ect
                SerialPort1.Open()
                SerialPort1.WriteLine("DRCmbed")
                Do
                    Try
                        If SerialPort1.ReadLine() = "mbedhere" Then
                            MsgBox("Connected to mbed: " + com + portnumber.ToString, MsgBoxStyle.Information, "Connected")
                            Exit While
                        End If
                    Catch ex As Exception
                        Exit Do
                    End Try
                Loop
            Catch ex1 As Exception
                Try
                    SerialPort1.Close()
                Catch ex As Exception
                End Try
            End Try

            portnumber += 1
        End While

Can anyone see what's wrong? because i really dont >.<

14 Jun 2012

Not familiar with VB, but some observations:

You have

SerialPort1.ReadLine() = "mbedhere"

That probably means the VB applications expects a newline/carriage return at the end of the string. Is mbed sending that?

Is

SerialPort1.ReadLine() = "mbedhere"

a valid way of doing string compares in VB? May have to use something like

String.compare(SerialPort1.ReadLine(),"mbedhere")
16 Jun 2012

Sorry for answering my own thread (DUH) LOL I've just figured it out. There is a char at the end of the received readline so i've got the functionality i need now :) thanks for replying.

For anyone who will have this problem here is the code i've created, I've also fully commented it so you can see in your mind what happens.

Functionality: Search for response on every comport and keep comport open if correct response is received

mbed C++ Example of the automated response This code is in my repository bellow

//Connection to device atempted
if (strcmp("DRCmbed", pFields[0]) == 0) {
    pc.printf("mbeddevice\r\n");//Validate correct port query (For Ext Application Port Chcking)
}

VB.NET Example of serialport scanning for automated response

Dim com As String = "COM" //Serialport names are written in strings so all we need to do is add an integer to it
Dim validport As Boolean //Allows us to stop scanning as soon as we retreive a valid response on that port
Dim check As System.Threading.Thread //Threading allows us to prevent lock up of an interface and also provides a quicker scan

//Function at load up to scan for response from serialport
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    CheckForIllegalCrossThreadCalls = False //We will be making illegal thread calls. At this point it does not matter
    check = New System.Threading.Thread(AddressOf readthread)  //Lets configure a sub to run the thread
    check.Start() //Lets start the thread before anything else
    Dim idle_wait as integer = 50 //Wait idlers help in slowing things down for the mbed device or any other device for that matter
    SerialPort1.ReadTimeout = idle_wait //Give time for the mbed to respond
    For i = 0 To 256 //Every com port accounted for
        Try //Lets try the port to check if it exists or if it gives us the correct response
            If validport = True Then //This will stop scanning once the correct response is received
                msgbox("Serial Port Correct Response from: " + SerialPort1.PortName) //Visual feedback of correct port found
                Exit For //We will no longer need to be in this loop as we've found the correct com port so lets exit this routine
            End If
            SerialPort1.PortName = com + i.ToString //Change the com number through every fail of the try event
            SerialPort1.Open() //Lets try to open the port on every try event if it exists or not
            SerialPort1.WriteLine("DRCmbed") //Writes this to the com port and waits for a response
            System.Threading.Thread.Sleep(idle_wait) //allow time for thread to read reply
        Catch ex As Exception //Lets catch the exception of a faild response or no connection and handle the exception
            Try //Lets try to close the port if we are connected to a device but gives us an incorrect response
                SerialPort1.Close() //Closes the serial com port for the next iteration
            Catch ex1 As Exception //We dont need to handle this exception at all!
            End Try
        End Try
    Next //Loop to the next comport
Exit Sub

//Constantly read the open serialport for response (Responses are length -1)
Public Sub readthread()
    Dim message //Lets create a dynamic response storage variable
    Do Until (validport = True) //If we have found the correct port then we dont need to do anything else!
        Try //Lets try this if the current com port is open
            message = SerialPort1.ReadLine.ToString //Store the response of the device
            If message.Substring(0, message.ToString.Length - 1) = "mbeddevice" Then //Check if the response equals: mbeddevice
                validport = True //If the response is correct then we've successfully validated which comport we should connect to
            End If
        Catch ex As Exception //No need to handle this exception at all!
        End Try
    Loop //Keep trying no matter what comport is open or closed
End Sub

Import programKeyboardMouseSerialV2

Updated many functions and bugs from the previous version. This program can emulate the keyboard and mouse on another computer using serial input [PC1-USB-MBED-USB-PC2] Control using serial input to keyboard/mouse output like a regular keyboard and mouse.