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. JspWriter与PrintWriter的关系

    一.JspWriter与PrintWriter的关系: 1.都是继承自java.io.Writer类. JspWriter可以在JSP页面中直接用out对象输出.可以用pageContext.getO ...

  2. JSON数据传递

    Servlet端代码 try { while (rs.next()) { for(int i=0;i<60;i++){ Day[i]+=rs.getInt("Day"+(i+ ...

  3. 客户调查(client)

    客户调查(client) 题目描述 公司派你去和几位客户面谈,以了解他们对公司产品的意见.你逐个打电话与客户联系,得知他们一般都很忙,不过他们还是可以为你抽出一点时间.现在的问题是有些客户的时间有冲突 ...

  4. 转:使用WITH AS提高性能简化嵌套SQL

    使用WITH AS提高性能简化嵌套SQL   一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片 ...

  5. 关于JSON.parse在ie6,ie7下未定义的issue

    情况是这样的: 在ie6下出现一个js error,说是JSON.parse为定义,一查,才知道,ie6,ie7不支持JSON. solution:只要在使用JSON之前加载个json2.js就行了. ...

  6. GPRS优点介绍及GPRS上网相关知识(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878732.html 单片机微控制器以其体积小.功耗低.使用方便等特点,广泛应用于各种工业.民用的嵌入式系统 ...

  7. Modbus RTU 通信工具设计(转)

    Modbus RTU 通信工具设计 Modbus 是一个工业上常用的通讯协议.一种通讯约定. ModBus 协议是应用层报文传输协议(OSI 模型第7层),它定义了一个与通信层无关的协议数据单元(PD ...

  8. [算法] aov图拓扑算法

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...

  9. JS的事件动态绑定机制

    动态添加标签+动态添加事件 window.onload=function(){ (已存在元素节点)事件绑定: (未来元素节点)事件绑定: } 它会扫描元素节点,如果元素节点存在(静态写好的),就可以绑 ...

  10. 解决airserver在Windows下安装失败的问题

    airserver 可以将iphone 实时投影到mac 和 pc.在mac上安装非常简单.但是在Windows上安装时会有很多问题.之前我电脑安装很快就完成了(因为我之前已经在不知情的前提先事先装过 ...