I am going to be doing some GPS work which will use a bluetooth serial COM Port. I expect that the COM Port interactions will be pretty simple at 38000 baud with ascii sentences terminated by a crlf.

I usually don't read the com port stuff. Where do I start Any particular advice

Re: Visual Basic Express Edition Bluetooth COM Port

Carsten Kanstrup


Very long story:

Read this: and please let me know if you find any errors or if you have any suggestions or anything else to contribute.

Short story:

Since each telegram is terminated with LF (the default NewLine character) you should only need a few lines of code to read from the GPS like this:

Code Block

Imports System.IO.Ports

Public Delegate Sub StringSubPointer(ByVal Buffer As String) ' A delegate to transfer the received string to the UI thread

Dim WithEvents COMPort As New SerialPort ' Make a new System.IO.Ports.SerialPort instance, which is able to send events

Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived ' Receiver routine. Note this subroutine is executed on a thread pool thread - not the UI thread.

Me.BeginInvoke(New StringSubPointer(AddressOf Display), COMPort.ReadLine)

End Sub

' Text display routine, which appends the received string to any text in the Received TextBox. Just an example. You can of course do what you like with the content of Buffer.

Private Sub Display(ByVal Buffer As String)


End Sub

' Somewhere in your program where you open the port (I do it when I select or change the COM port).

COMPort.PortName = "COM1" ' I use a Combobox with all possibilities instead.

COMPort.BaudRate = 38400

COMPort.ReadTimeout = 2000 ' 2 sec timeout to prevent blocking forever. ReadLine is a blocking call and you cannot close the port while it hangs in ReadLine.

' COMPort.NewLine = Chr(xx) in case your telegram is not terminated with LF.



Catch ex As Exception


End Try

' This subroutine is activated when the form is closed. It closes the COM port. Without such a close command, the garbage collector may close the COM port while it is still in use!!!

Private Sub MaxiTesterClosing(ByVal sender As Object, ByVal e As ComponentModel.CancelEventArgs) Handles MyBase.Closing

If MessageBox.Show("Do you really want to close the window", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then

e.Cancel = True


If COMPort.IsOpen Then COMPort.Close() ' Close COM port when the form is terminated with [X]

End If

End Sub

I hope that I have remembered everything. However, if this is not the case or you need something else, you can find the complete code example in the above link - or the easy way - just ask again .

Re: Visual Basic Express Edition Bluetooth COM Port


Thank you Carsten, that looks great.

I can't until the GPS gets here. We ordered it yesterday.

If you have both Vista and WIFI there's something i'd like to send you.

Re: Visual Basic Express Edition Bluetooth COM Port

Carsten Kanstrup


Unfortunately (somebody may think I am lucky), I have only XP and my rather old PC does not have the horsepowers for Vista. Are there actually any computers, which have that I have never seen a fluent Vista demos on any computer! I simply don't understand that Microsoft can bring themselves to show something like this, and I begin to understand why Apply has such a great success. Unless the software scientists at Microsoft learn the small word "efficiency", the company may commit suicide. This time they just cannot request faster hardware like they use to do. It is simply not available.

Re: Visual Basic Express Edition Bluetooth COM Port


I ahve been running Vista for the past year. I find it to be very stable and infrastructurally, it is vastly superior to XP in so very many ways.

Re: Visual Basic Express Edition Bluetooth COM Port


OK Carsten,

I've been playing with the little GPS and it's nive and it's cute. I'm using COM Portrs 4 (In) and 5(out) and I have never needed to issue a command.

I can connect and read from it. That's not a problem. I do notice that when I am in the port's received event, that thread is not the UI thread and if this were going to controls I'd have to use Begin - End Invoke.

But that's not what I want. What I'd really want is to simply copy the data into a string an start the UI thread so I cn process. I HATE this threading model. If that thread is the only writer to a textbox created by the UI thread SO WHAT! ! ! ! !

But I've never seen a way to bring in some data from a thread, leaving a flag to startup the ui thread. In unmanaged threads, I've attempted to start 1 ms UI timers to get the UI going again and that's never worked. I wonder if a .performclick would work I doubt it, that's way to easy. Anyway, It's time to go home.

Re: Visual Basic Express Edition Bluetooth COM Port

Carsten Kanstrup


I don't understand why you hate this threading model so much. I think it works quite well (except for the rather low efficiency).

Your GPS probably sends ASCII telegrams with the NMEA protocol, so when the DataReceived event fires, ReadLine does the entier job for you on the thread pool thread without blocking the UI thread. When a complete line is received, BeginInvoke post a message to the message queue telling that the line is waiting in a TME (ThreadMethodEntry) object in the ThreadCallbackList. This object contains a delegate pointing to the method to call on the UI thread, the input data (received line) and the compressed stack (safety permissions). When this message reaches the button of the queue, WndProc emties the entier ThreadCallbackList (more lines may be waiting) and calls the UI method(s) with the received line(s) as input. It is fully event driven and works quite nice. As you don't need any callback to the thread pool thread, there is no need for EndInvoke.

If you really want to write directly to your textbox from the thread pool thread, you can just switch the cross-thread test off by means of: Me.CheckForIllegalCrossThreadCalls = false, but I see no reason for that when a delegate and a BeginInvoke statement is all you need to do it by the book. In your case, there may be only one program writing to the textbox, but just look at my small test program. To make it easier to see which answer belongs to which transmission, I send a little "TX" to the textbox each time I transmit, so I have two programs accessing the same textbox, but because all programs are event driven and controlled by the message pump on the UI thread, I don't need any flags or locks.

I really think that you should give the theading model a chance. It doesn't help just to sit down and refuse to use anything, which is not VMS. I agree with you that VMS in many ways was superior to Windows, but there are no DEC computers anymore. Today it is Windows and .NET or you are out of business.

PS. Why do you use two COM ports One is enough.

Re: Visual Basic Express Edition Bluetooth COM Port

Keith K

Renee, you may want to try looking at this. Maybe it will get you started.

Re: Visual Basic Express Edition Bluetooth COM Port


Thank you Keith. I already have the portions of the interpreting software written. I'm not as horizontal as he is and I like my code better, But then I don't need to do as much as he does in that article either.