Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to select and use a com port to write Hello World from a drop down list I've been following and using examples that I've found online. Everything seems fine when I Drop Down the List, but when I try to write to the Com Port which I have the USB Serial Adapter plugged into, I get an error that the port name isn't recognized (even though it did make it to the drop down List). Here's a copy of the code, any suggestions?? Thanks!

What I have tried:

My code:
VB.NET
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class Form1

    Dim myport As Array
    Delegate Sub settextcallback(ByVal [text] As String)

    Private Sub Form1_Load(sender As Object, e As EventArgs)
        Handles MyBase.Load

        myport = IO.Ports.SerialPort.GetPortNames()
        Printer_Comport.Items.AddRange(myport)

    End Sub

    Private Sub Printer_Comport_MouseClick(sender As Object, e As MouseEventArgs)
        Handles Printer_Comport.MouseClick

        Printer_Comport.Items.Clear()
        myport = IO.Ports.SerialPort.GetPortNames
        Printer_Comport.Items.AddRange(myport)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Handles Button1.Click

        SerialPort1.PortName = Printer_Comport.Text 'Heres where I get error
        SerialPort1.BaudRate = 9600
        SerialPort1.Open()
        SerialPort1.Write("Hello World")
        SerialPort1.Close()
    End Sub
End Class
Posted
Updated 1-Oct-23 8:58am
v4
Comments
Graeme_Grant 29-Sep-23 19:55pm    
Post the full error please by using the "Improve question" link above

If I paste your code into my app, I don't get an error where you show - I do get the error "The port 'COM1:' does not exist" when I reach the call to Open because I don't have any COM ports attached to this PC, but I'd expect that.

So ... you are going to have to use the debugger to find out exactly what is going on.
I'd start by checking communications can be established using Hyperterminal or similar, then when that works, put a breakpoint at the top of the method, and look at the variables to see what is in the list of port names, what is in the combobox Text, and so on. Once you know that, it should be fairly obvious - but we can't do any of that for you!
 
Share this answer
 
The only thing I can see that would throw any kind of error on that line would be if Printer_Comport.Text returned an empty string. That means nothing is selected in your control, whatever it is.
 
Share this answer
 
Problem Solved, The Driver for the USB Serial interface wasnt good
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900