Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What im trying to do its after i put available com ports into listbox1, then when an user select a com port from it, make an assignment from selected value in listbox to PortName
.PortName = ListBox1.SelectedItem.Value

but this seems doesnt work. What im doing wrong? THanks

What I have tried:

Public Class Com
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        With Form1.SerialPort1
            .BaudRate = 9600    'for example, set the speed as you actually need it
            .Parity = System.IO.Ports.Parity.Space    'much more common would be .None
            .StopBits = 1          'or 2, though 2 should not normally be used (IMO)
            .PortName = ListBox1.SelectedItem.Value 'for example
            .Handshake = System.IO.Ports.Handshake.RequestToSend  'or .None
            .RtsEnable = True
            .Open()

        End With
    End Sub
End Class
Posted
Updated 28-Jun-17 2:07am
Comments
F. Xaver 28-Jun-17 5:09am    
use the debuger to watch what is getting set..
what have you used to fill the listbox? (listbox1 is allways a terrible name btw)
System.IO.Ports.SerialPort.GetPortNames to get the actual present devices?

from a fast look it should work fine.. but if you not block the listbox after that, or close and freeing the comport befor a new selection you will pretty sure get errors.
(and as your comments say.. some of those settings are kinda uncommon. I've never seen a device with .Parity = Space so far

1 solution

For cleaner code you better create a new port object when opening. Because it is a system resource you must handle it carefully, so close it after end of usage every time. Else the port may be blocked til next reboot. Separate UI-code from communication code to avoid messy code.

Read the article and code of SerialPort Sample in VB.NET (C#).
 
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