VB.net 利用SerialPort进行读取串口操作
2014-03-03 09:58
525 查看
Imports System
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'获取计算机有效串口
Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口
Dim port As String
For Each port In ports
portnamebox.Items.Add(port) '向combobox中添加项
Next port
'初始化界面
baudratebox.SelectedIndex() = 2
portnamebox.SelectedIndex() = 0
Serial_Port1() '初始化串口
Label3.Text = SerialPort1.IsOpen
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
sendbox.Text = "123"
' baudratebox.Text = baudratebox.Items(0) 注释和不注释的地方可以替换
'portnamebox.Text = portnamebox.Items(0)
End Sub
Private Sub Serial_Port1() '设置串口参数
SerialPort1.BaudRate = Val(baudratebox.Text) '波特率
SerialPort1.PortName = portnamebox.Text '串口名称
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
End Sub
'关闭串口连接
Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click
Try
SerialPort1.Close() '关闭串口
Label3.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = False Then
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
receivebox.Text = ""
receivebytes.Text = ""
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'打开串口连接
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click
Try
SerialPort1.Open() '打开串口
Label3.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = True Then
statuslabel.Text = "串口已连接"
statuslabel.ForeColor = Color.Green
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'发送数据
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
SerialPort1.Write(sendbox.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'触发接收事件
Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数
End Sub
'接收数据
Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Dim strIncoming As String
Try
receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead)
If SerialPort1.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '添加的延时
strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
receivebox.Text = strIncoming
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'获取计算机有效串口
Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口
Dim port As String
For Each port In ports
portnamebox.Items.Add(port) '向combobox中添加项
Next port
'初始化界面
baudratebox.SelectedIndex() = 2
portnamebox.SelectedIndex() = 0
Serial_Port1() '初始化串口
Label3.Text = SerialPort1.IsOpen
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
sendbox.Text = "123"
' baudratebox.Text = baudratebox.Items(0) 注释和不注释的地方可以替换
'portnamebox.Text = portnamebox.Items(0)
End Sub
Private Sub Serial_Port1() '设置串口参数
SerialPort1.BaudRate = Val(baudratebox.Text) '波特率
SerialPort1.PortName = portnamebox.Text '串口名称
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
End Sub
'关闭串口连接
Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click
Try
SerialPort1.Close() '关闭串口
Label3.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = False Then
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
receivebox.Text = ""
receivebytes.Text = ""
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'打开串口连接
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click
Try
SerialPort1.Open() '打开串口
Label3.Text = SerialPort1.IsOpen
If SerialPort1.IsOpen = True Then
statuslabel.Text = "串口已连接"
statuslabel.ForeColor = Color.Green
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'发送数据
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
SerialPort1.Write(sendbox.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'触发接收事件
Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数
End Sub
'接收数据
Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Dim strIncoming As String
Try
receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead)
If SerialPort1.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '添加的延时
strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
receivebox.Text = strIncoming
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
相关文章推荐
- vb.net利用SerialPort进行读取串口操作
- vb.net利用SerialPort进行读取串口操作
- vb.net利用SerialPort进行读取串口操作
- [VB.NET]利用vb.net创建一个完全可编辑的DataGrid,从而实现操作Access数据库进行增,删,改的功能,前提是不用任何其他的Button
- .NET的那些事儿(10)——利用C#.NET进行串口编程
- 利用VB.Net 通过Windows Sockets (Winsock)以及多线程编程进行桌面电脑与Pocket PC间的双向通信
- VB.Net 中的串口操作问题
- [VB.NET]在线程里频繁的进行委托操作主界面的ui是不是效果不好?
- 浅谈ADO.NET中利用DataAdapter进行数据操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 在VB中利用API进行串口通信
- 利用Mxnet自带的工具,进行数据转化,读取图片文件
- [VB.NET]VB如何进行网站数据库操作? :)
- VB.net 文件读取、写入、追加操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- Vb.net下,利用数组进行“组合”查询
- C#利用SerialPort控件进行串口编程小记(1)
- [VB.NET]请问一个页面后台中 进行2次数据库读取 上下之间的值怎么传 在线等!!!!!
- C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
- 初谈ADO.NET中利用DataAdapter进行数据操作