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. javascript预编译

    刚学前端的小白,第一次写博客,难免有点幼稚.以后每周写两次博客,慢慢积累. 笨鸟不必先飞,但一定是最后一个留下的.加油! JS的预编译定义 在一段程序执行前,js会把var和function这两个关键 ...

  2. swift(2)元祖(Tuple)

    let somePoint = (, ) switch somePoint { , ): // 位于远点 println("(0, 0) is at the origin") ): ...

  3. 微信小程序 问题收集

    1. Q: 想知道微信小程序这些证书的具体要求在哪儿能查到? 就比如说,他要求有卫生和计划生育委员会批文 我想知道是需要省级还是市级的 但是官网查不到 A:市级省级的我也不是特清楚 [/ak] 应该是 ...

  4. boost之词法解析器spirit

    摘要:解析器就是编译原理中的语言的词法分析器,可以按照文法规则提取字符或者单词.功能:接受扫描器的输入,并根据语法规则对输入流进行匹配,匹配成功后执行语义动作,进行输入数据的处理. C++ 程序员需要 ...

  5. JSP直接调用一个action定向到页面

    方法:写function <script type="text/javascript"> function mainPas(){ window.location.hre ...

  6. css 内联与块

    内联元素可以理解为不能直接设置宽度和高度元素,比如span,你为他设置宽度和高度没有效果,除非你把它设置成块级元素. 如下面的代码把display:block;属性值去掉的话,宽度和高度都不会起作用了 ...

  7. 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法

    其实是c盘或者其他盘的 jdk 安装的太多了,把其他的都卸载掉就行了

  8. FusionCharts使用问题及解决方法(五)-FusionCharts常见问题大全

    在前4篇文章中,我们总结了FusionCharts XT图表使用中的一些常见问题(FAQ)及解决方法,本文继续讨论FusionCharts使用者常见的一些复杂报错及错误的调试/解决方法. 问题描述:是 ...

  9. hihocoder网络流一·Ford-Fulkerson算法

    网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个大城市都会遇 ...

  10. string字符串转C风格字符串 进而转换为数字

    要求如题 头文件stdlib.h中有一个函数atof() 可以将字符串转化为双精度浮点数(double) double atof(const char *nptr); 此字符串为C风格字符串,因此需要 ...