1、在Form1的设计模式下添加以下控件:

2、添加好控件之后我们就可以打开Form1.vb进行编程了:

 '使用串口需要引用的命名空间
Imports System.IO.Ports
Imports System Public Class Form1 '在设计视图中双击Form1窗体出现的函数,在第一次显示窗体前发生的事件写在这个里面,类似于ViewWillAppear
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 '初始化界面
baudratebox.Text = baudratebox.Items() '注释和不注释的地方可以替换
portnamebox.Text = portnamebox.Items()
'baudratebox.SelectedIndex() = 2
' portnamebox.SelectedIndex() = 0
Serial_Port1() '初始化串口
Label3.Text = SerialPort1.IsOpen
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
sendbox.Text = ""
receivebytes.Text = ""
linecheck.Enabled = True
timebox.Enabled = True '让发送、接收数据按钮不能点击(失能)
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False End Sub Private Sub Serial_Port1() '设置串口参数
'SerialPort1.BaudRate = Val(baudratebox.Text) '波特率
'SerialPort1.PortName = portnamebox.Text '串口名称
SerialPort1.PortName = portnamebox.SelectedItem
SerialPort1.BaudRate = Val(baudratebox.SelectedItem)
SerialPort1.DataBits = '数据位
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() '关闭串口 '让发送、接收数据按钮不能点击(失能)
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False 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() '打开串口
timebox.Enabled = True
'使能按钮
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True 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 Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
send()
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 Byte
Dim strIncoming As Integer
Dim str1() As String
Dim str2() As String
Dim bytes() As Byte
Dim i As Integer
Try
Threading.Thread.Sleep() '添加的延时
' receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead)
receivebytes.Text = Str(SerialPort1.BytesToRead) If SerialPort1.BytesToRead > Then ReDim bytes(SerialPort1.BytesToRead)
'strIncoming = Convert.ToByte(SerialPort1.ReadByte())
If receivecheck.Checked = True Then
strIncoming = SerialPort1.ReadByte()
bytes() = strIncoming
For i = To SerialPort1.BytesToRead
strIncoming = SerialPort1.ReadByte() '读取缓冲区中的数据
bytes(i) = strIncoming
Next
' SerialPort1.Write(sendbox.Text)'发送数据
SerialPort1.DiscardInBuffer()
str1 = Split(BitConverter.ToString(bytes), "-") ReDim str2(str1.Length - ) '去除str1中最后的字符
For i = To str1.Length -
str2(i) = str1(i)
Next
receivebox.Text = receivebox.Text & Join(str2, " ")
'BitConverter.ToString(bytes)
Else
receivebox.Text = receivebox.Text & SerialPort1.ReadExisting()
End If End If Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub Public Sub send() '发送数据过程
Dim databyte() As Byte
Dim str1() As String
Dim str2 As String
Dim str3 As String
Dim i As Integer Try
If sendcheck.Checked = False Then '不按照16进制发送
'timecheck.Enabled = True If linecheck.Checked = False Then '判断是否选中分行发送
SerialPort1.Write(sendbox.Text)
Else
SerialPort1.WriteLine(sendbox.Text)
End If Else '按照16进制发送
If InStr(sendbox.Text, " ") Then '判断是否有空格
str1 = Split(sendbox.Text)
str2 = Join(str1, "")
Else
str2 = sendbox.Text
End If If str2.Length Mod = Then '判断字符串字节数是否为偶数
ReDim databyte(str2.Length / ) '重新定义数组
For i = To str2.Length / -
databyte(i) = Convert.ToByte(Mid(str2, * i + , ), ) '两个字符转换为一个16进制字节
'databyte(i) = Val(Mid(str2, 2 * i + 1, 2))
Next
SerialPort1.Write(databyte, , databyte.Length - )
sendbytes.Text = Str(Val(sendbytes.Text) + databyte.Length - )
Else str3 = Mid(str2, , (str2.Length - )) & "" & Mid(str2, str2.Length)
ReDim databyte(str3.Length / )
For i = To str3.Length / -
databyte(i) = Convert.ToByte(Mid(str3, * i + , ), )
Next
SerialPort1.Write(databyte, , databyte.Length - )
sendbytes.Text = Str(Val(sendbytes.Text) + databyte.Length - )
End If
'databyte = System.Text.Encoding.Default.GetBytes(sendbox.Text)把每个字符转换成字节 End If Catch ex As Exception
MessageBox.Show(ex.Message)
End Try End Sub '更改串口设置
Private Sub portnamebox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles portnamebox.SelectedIndexChanged
Try
Serial_Port1()
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
sendbox.Text = ""
End Sub '清空接收区数据
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
receivebox.Text = ""
receivebytes.Text = End Sub '定时发送数据
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = timebox.Text
send()
End Sub '选择定时发送的触发事件
Private Sub timecheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timecheck.CheckedChanged If timecheck.Checked = True Then
If timebox.Text = "" Then
MsgBox("时间间隔不能为0")
timecheck.Checked = False
Else
send()
timebox.Enabled = False
End If
Else
timebox.Enabled = True
End If
End Sub End Class

然后运行,由于我电脑上没有串口,所以使用usb转串口线,这个线所用的转换芯片是ch340。

安装好驱动之后在计算机管理中查是COM3.

然后把串口的2,3针脚用杜邦线短路,因为这样的话我用电脑发送后又可以用电脑的同一个串口接收数据,方便测试。

测试效果如下:

VS2010环境下使用VB编写串口助手的更多相关文章

  1. VS2010环境下使用VB开发网络编程(WinHttp)

    首先点项目——>添加引用——>COM选项卡——>Microsoft WinHttp Services,version 5.1,然后点确定就可以添加Winhttp到项目引用中. 1.如 ...

  2. 通过编写串口助手工具学习MFC过程——(六)添加Edit编辑框控件

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  3. 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  4. 通过编写串口助手工具学习MFC过程——(二)通过“打开串口”按钮了解基本操作

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  5. Boost学习总结(一)VS2010环境下编译STLport和Boost

    Boost学习总结(一)VS2010环境下编译STLport和Boost Boost简介 Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库.1998年,Beman G.Da ...

  6. 通过编写串口助手工具学习MFC过程--(十一)弹出模态型对话框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  7. 通过编写串口助手工具学习MFC过程——(十)UpdateData()用法和编辑框的赋值、取值

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  8. 通过编写串口助手工具学习MFC过程——(九)自动识别串口的方法

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  9. 通过编写串口助手工具学习MFC过程——(八)遇到的一些问题

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

随机推荐

  1. android 5.0新特性学习--RecyclerView

    在过去很多年,我们的PC或者手机设备都是采用拟物化的设计风格,IOS采用扁平化的特性,android在2014年IO大会上说采用Material Design的设计风格,显示效果不能过于生硬的转换,而 ...

  2. CodeForces 678D Iterated Linear Function

    简单矩阵快速幂. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...

  3. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  4. 用MyEclipse自动生成hibernate映射文件和实体类

    创建数据库,创建相应的表 点击图标,选择MyEclipse Datebase Explorer 右击空白区域,选择new菜单,根据提示创建数据库连接,创建好后会显示你所创建的连接名,如图mysqldb ...

  5. scroll、scrollBy和 scrollTo三种方法定位滚动条位置

    在默认情况下,页面加载完后默认滚动在最顶端,有些时候我们需要在页面打开后,定位滚动条的位置,比如,横向和纵向滚动条居中,实现页面滚动的方法有三种:scroll.scrollBy和 scrollTo,三 ...

  6. gen_compile.sql

    set echo off pagesize 0 feedback off define v_input_un       = &1define v_input_pw       = & ...

  7. oracle数据库兼容mysql的差异写法

    1.sysdate改为sysdate(),或者now(); 2.nvl(expr1,expr2) 改为IFNULL(expr1,expr2) nvl2(expr1,expr2,expr3)改为 IF( ...

  8. Android程序两种退出的方法

    两种程序退出的方法: Context的finish()方法: android.os.Process的killProcess()方法:(当程序isRegistered()失败,说明程序被修改过,调用ki ...

  9. vmware中网络连接方式介绍

  10. Gulp自动构建前端开发一体化

    gulp是基于Nodejs的自动任务运行器, 她能自动化地完成 javascript/coffee/sass/less/html/image/css 等文件的的测试.检查.合并.压缩.格式化.浏览器自 ...