使用列表框中的可用COM端口 [英] Using available com ports from listbox

查看:85
本文介绍了使用列表框中的可用COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将可用的com端口放入listbox1之后尝试执行此操作,然后当用户从中选择一个com端口时,从列表框中的选定值到PortName进行分配

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

推荐答案

为了更清洁的代码你更好打开时创建一个新的端口对象。因为它是一个系统资源,所以你必须小心处理它,所以每次使用结束后关闭它。否则端口可能会被阻止,直到下次重启。从通信代码中分离UI代码以避免代码混乱。



阅读 VB.NET中的SerialPort示例(C#)
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#).


这篇关于使用列表框中的可用COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆