我想在我的程序中添加一个用于选择com端口和波特率的组合框,任何人都可以帮助我吗? :D谢谢你的关注 [英] i want to add a combo box for selecting a com port and baud rate in my program, can anyone help me ? :D Thank you for your attention
本文介绍了我想在我的程序中添加一个用于选择com端口和波特率的组合框,任何人都可以帮助我吗? :D谢谢你的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Imports System.IO
Imports System。 IO.Ports
Imports System.Threading
Public 类 Form1
共享 _continue 作为 < span class =code-keyword> Boolean
共享 _serialPort 作为 SerialPort
Dim keysPressed As 新 HashSet( 键)
私人 Sub Form1_Load( ByVal sender As System。 对象, ByVal e As System.EventArgs)句柄 MyBase .Load
使用 SerialPort1
.Close()
.PortName = COM10
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.DtrEnable = < span class =code-keyword> True
.RtsEnable = True
.ReceivedBytesThreshold = 1
。打开()
结束 使用
结束 Sub
私有 Sub Form1_KeyDown( ByVal sender 作为 对象, ByVal e As System.Windows.Forms.KeyEventArgs)句柄 MyBase .KeyDown
keysPressed.Add(e.KeyCode)
如果 keysPressed.Contains(Keys.Left) AndAlso keysPressed.Contains(Keys.Right)然后
SerialPort1.Write( Q)
Label1.Text = Мирување
Label1.Text = Мирување
ElseIf keysPressed.Contains(Keys.Left)然后
SerialPort1 .Write( L)
Label1.Text = Лево
ElseIf keysPressed.Contains (Keys.Right)然后
SerialPort1.Write( D)
Label1.Text = Десно
结束 如果
If keysPressed.Contains(Keys.Up) AndAlso keysPressed.Contains(Keys.Down)然后
SerialPort1.Write( E)
Label1.Text = Мирување
Label1.Text = Мирување
ElseIf keysPressed.Contains(Keys.Up)然后
SerialPort1.Write( N)
Label1.Text = Напред
ElseIf keysPressed.Contains(Keys.Down)然后
SerialPort1.Write( S)
Label1.Text = Назад
结束 如果
刷新()
结束 Sub
私有 Sub Form1_KeyUp( ByVal sender 作为系统。对象, ByVal e 作为 System.Windows.Forms.KeyEventArgs)句柄 MyBase .KeyUp
选择 案例 e.KeyCode
案例 Keys.Left
SerialPort1.Write( Q)
Label1 .Text = Мирување
keysPressed.Clear()
案例 Keys.Right
SerialPort1.Write( W)
Label1.Text = Мирување
keysPressed.Clear()
Case Keys.Up
SerialPort1.Write( E)
Label1.Text = Мирување
keysPressed.Clear()
案例 Keys.Down
SerialPort1.Write( R)
Label1.Text = Мирување
keysPressed.Clear()
End 选择
结束 Sub
私有 Sub Button1_Click( ByVal 发件人作为系统。对象, ByVal e As System.EventArgs)
结束 Sub
结束 类
这里是项目文件
http://www.mediafire.com/do wnload / hpc9btpsyz27y5c / Project + files.rar< / a> [ ^ ]
解决方案
首先在表格中添加一个组合框
在你的表单加载方法中添加
ComboBox1.DataSource = SerialPort.GetPortNames()
GetPortNames()返回您机器上可用的所有端口。
对于波特率和端口选择的其他信息,您可以找到将其添加到表单加载方法
ComboBox1.DataSource = SerialPort.GetPortNames( )
Dim sport As 新 SerialPort(ComboBox1.Text)
TextBox1.Text = sport.BaudRate
并进一步向组合框索引更改事件方法,您可以添加以下
私人 < span class =code-keyword> Sub ComboBox1_Select edIndexChanged(发件人作为 对象,e 作为 EventArgs)句柄 ComboBox1.SelectedIndexChanged
Dim sport 正如 新 SerialPort(ComboBox1.Text)
TextBox1.Text = sport.BaudRate
结束 Sub
希望这会有所帮助。
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Dim keysPressed As New HashSet(Of Keys)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialPort1
.Close()
.PortName = "COM10"
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.DtrEnable = True
.RtsEnable = True
.ReceivedBytesThreshold = 1
.Open()
End With
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
keysPressed.Add(e.KeyCode)
If keysPressed.Contains(Keys.Left) AndAlso keysPressed.Contains(Keys.Right) Then
SerialPort1.Write("Q")
Label1.Text = "Мирување"
Label1.Text = "Мирување"
ElseIf keysPressed.Contains(Keys.Left) Then
SerialPort1.Write("L")
Label1.Text = "Лево"
ElseIf keysPressed.Contains(Keys.Right) Then
SerialPort1.Write("D")
Label1.Text = "Десно"
End If
If keysPressed.Contains(Keys.Up) AndAlso keysPressed.Contains(Keys.Down) Then
SerialPort1.Write("E")
Label1.Text = "Мирување"
Label1.Text = "Мирување"
ElseIf keysPressed.Contains(Keys.Up) Then
SerialPort1.Write("N")
Label1.Text = "Напред"
ElseIf keysPressed.Contains(Keys.Down) Then
SerialPort1.Write("S")
Label1.Text = "Назад"
End If
Refresh()
End Sub
Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Left
SerialPort1.Write("Q")
Label1.Text = "Мирување"
keysPressed.Clear()
Case Keys.Right
SerialPort1.Write("W")
Label1.Text = "Мирување"
keysPressed.Clear()
Case Keys.Up
SerialPort1.Write("E")
Label1.Text = "Мирување"
keysPressed.Clear()
Case Keys.Down
SerialPort1.Write("R")
Label1.Text = "Мирување"
keysPressed.Clear()
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
and here are the project files
http://www.mediafire.com/download/hpc9btpsyz27y5c/Project+files.rar</a>[^]
解决方案
First add a combobox to your Form
In your Form Load method add
ComboBox1.DataSource = SerialPort.GetPortNames()
GetPortNames() returns all the ports available on your machine.
For baud rate and other info on the port select you can find add this to your Form Load method
ComboBox1.DataSource = SerialPort.GetPortNames() Dim sport As New SerialPort(ComboBox1.Text) TextBox1.Text = sport.BaudRate
and further to the combo box index changed event method you can add the below
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim sport As New SerialPort(ComboBox1.Text) TextBox1.Text = sport.BaudRate End Sub
hope this helps.
这篇关于我想在我的程序中添加一个用于选择com端口和波特率的组合框,任何人都可以帮助我吗? :D谢谢你的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文