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. Java类锁和对象锁实践(good)

    一.前言 之前对类锁和对象锁是否是互斥的不是太确定,因此决定编写相关的程序进行实践一下.编写前对相关定义约定约定如下: 1. 类锁:在代码中的方法上加了static和synchronized的锁,或者 ...

  2. android 5.0新特性学习--Drawable Tinting(为图片资源着色)

    使用android:tint属性去调整色调.android:tintMode 着色模式 screen multiply and src_atop/src_in/src_oversetTint(int ...

  3. dage手法之 头部和banner ad tpl_header

    <div class="top2"> <?php if ($current_page_base == 'index' || $current_page_base ...

  4. html input密码显示为“*”

    1. 功能需求:HTML中,在input password输入框中输入字符将默认显示为“实体圆点”,但这里要求将实体圆点字符替换成“*”号显示. 2. 局限:鼠标光标非IE浏览器不一定显示,选择多个字 ...

  5. 使用Emmet加速Web前端开发

    Emmet插件以前被称作为Zen Coding,是一个文本编辑器的插件,它可以帮助您快速编写HTML和CSS代码,从而加速Web前端开发.早在2009年,Sergey Chikuyonok写过一篇文章 ...

  6. hibernate事务并发问题(脏读,不可重复读,幻读)

    脏读  dirty read:  读了别的事务没有提交的事务, 可能回滚, 数据可能不对. 不可重复读 non repeatable read: 同一个事务里前后读出来的数据不一样, 被另一个事务影响 ...

  7. call_grant_exec.sql

    set echo off feedback off verify off pagesize 0 linesize 120 define v_grantee                 = & ...

  8. 使用JQuery结合HIghcharts实现从后台获取JSON实时刷新图表

    项目做了一个报表,可以实时的观察呼叫中心的电话访问量,之前的版本是使用JFreechart做的,使用一段时间后出现内存溢出,服务器的内存使用量会变得很大,所以改用Ajax前台加载数据的方式实现实时报表 ...

  9. 可信执行环境(TEE)介绍

    可信执行环境(TEE)是Global Platform(GP)提出的概念.针对移动设备的开放环境,安全问题也越来越受到关注,不仅仅是终端用户,还包括服务提供者,移动运营商,以及芯片厂商.TEE是与设备 ...

  10. 一个简单但详细的解释Windows文件映射读取数据文件的例子

    #include <windows.h>#include <string.h>#include <string>#include <iostream>u ...