Question Serial Port Problem: The given port name is invalid...

rhoaste

Member
Joined
Jun 16, 2010
Messages
19
Programming Experience
10+
Hi.

I am having problems communicating with a UBLOX GPS device using the serial interface over USB. I would like to be able to receive data from the device asynchronously in a receive event when data is sent from the GPS to the computer.

VB.NET:
        mySerialPort.PortName = "COM7"
        mySerialPort.Close()

        mySerialPort.BaudRate = 115200
        mySerialPort.Parity = Parity.None
        mySerialPort.DataBits = 8
        mySerialPort.StopBits = StopBits.One
        mySerialPort.DtrEnable = True

        mySerialPort.Open()

Error received on the MySerialPort.Open()
The given port name is invalid. It may be a valid port, but not a serial port.

My GPS device is sending data to the system on COM7 with the above settings and I can verify this with a terminal emulator such as PuTTY

250i61i.jpg


2089yzk.jpg


When I run this code:
VB.NET:
        For Each sp As String In My.Computer.Ports.SerialPortNames
            Debug.WriteLine(sp)
        Next
I get this...
COM4
COM1
COM7

But when I run this code:
VB.NET:
        Using search As New ManagementObjectSearcher("SELECT * FROM Win32_SerialPort")
            For Each mo In search.Get
                Debug.WriteLine(mo.Item("DeviceID") & " " & mo.Item("Caption"))
            Next

        End Using
This is the result (COM7 missing):
COM4 Intel(R) Active Management Technology - SOL (COM4)
COM1 Communications Port (COM1)

Unfortunately I have not really made any progress as to why I can retrieve data from the GPS serially via a terminal application, but cannot even open the port using .Net

Amongst other articles I have read, such as this one: If you *must* use .NET System.IO.Ports.SerialPort, I'm now concerned about using System.IO.Ports.SerialPort.

Any help would be gratefully received.

Thanks and regards.
 
I think COM Port name strings are a fixed length.

The best way I've got COM port names is by using My.Computer.Ports.SerialPortNames.
Add these to a list and use this to get the port you want to use.

VB.NET:
    For Each COMString As String In My.Computer.Ports.SerialPortNames
      COMPortList.Add(COMString)
    Next

    mySerialPort.PortName = COMPortList.Item(i)
 

Latest posts

Back
Top