Imports SystemImports 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 StringFor 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.ClickTry            SerialPort1.Close() '关闭串口            Label3.Text = SerialPort1.IsOpenIf SerialPort1.IsOpen = False Then                statuslabel.Text = "串口未连接"                statuslabel.ForeColor = Color.Red                receivebox.Text = ""                receivebytes.Text = ""End IfCatch ex As Exception            MessageBox.Show(ex.Message)End TryEnd Sub

'打开串口连接    Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.ClickTry            SerialPort1.Open() '打开串口            Label3.Text = SerialPort1.IsOpenIf SerialPort1.IsOpen = True Then                statuslabel.Text = "串口已连接"                statuslabel.ForeColor = Color.GreenEnd IfCatch ex As Exception            MessageBox.Show(ex.Message)End TryEnd Sub

'发送数据    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickTry            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.DataReceivedMe.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数    End Sub

'接收数据    Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)Dim strIncoming As StringTry            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 = strIncomingEnd IfCatch ex As Exception            MessageBox.Show(ex.Message)End TryEnd SubEnd Class

转载于:https://www.cnblogs.com/jiaxiaoai/archive/2011/08/09/2132502.html

vb.net利用SerialPort进行读取串口操作相关推荐

  1. C#利用SerialPort类对串口发送接收数据

    1.连接串口方法 SerialPort ser = new SerialPort();//也可以在工具箱中直接拖SerialPort控件 public void OpenCom() {try{//波特 ...

  2. 利用SerialPort类实现收发短信(C# 2.0)

    作者:veryhappy(wx.net) 在.NET 1.1版本中微软并没有封装一个串口操作的类,但是我们可以在网络上找到通过WIN32 API读写文件方式访问串口的开源类库,比如:JustinIO. ...

  3. 使用Python读取串口数据

    Python读取串口数据 使用Python读取串口数据 1.编程流程 2.应用serial库 3.编码设计 4.编程实现 使用Python读取串口数据 1.编程流程 串口数据流程为, 1.实例化串口连 ...

  4. VB、WIN32API函数在串口操作模块的运用

    1.应用 1.1RegOpenKeyEx函数涉及注册表的相关知识,先了解注册表的作用及数据结构 注册表的作用 注册表是windows操作系统中的一个核心数据库,其中存放着各种参数,直接控制着windo ...

  5. SerialPort实现对串口COM的操作(有些纠结)

    这段时间做了一个小型贷款软件,包括客户管理,交易管理,存折管理和打印,统计分析等模块,其中要求结合存折读卡器一起使用,比如添加存折的时候,存折号要求刷完卡后读出来,刷卡后可以显示客户信息等,和银行的刷 ...

  6. TF:利用TF读取数据操作,将CIFAR-10 数据集中的训练图片读取出来,并保存为.jpg格式

    TF:利用TF读取数据操作,将CIFAR-10 数据集中的训练图片读取出来,并保存为.jpg 格式 目录 输出结果 核心代码 输出结果 核心代码 def inputs_origin(data_dir) ...

  7. 利用Python读取并操作注册表

    利用Python读取并操作注册表 一.Python与注册表相关的库 winreg(主要) win32api 二.注册表结构:键.值 三.winreg--访问Windows注册表 1.模块功能: win ...

  8. 在C#中使用SerialPort类实现串口通信

    在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信等.本文章将本人在学习过程中从网络上搜集到的相关信息写出来供大家参考. 下面主要介绍该类的主要属性(表1) ...

  9. 如何在QT中读取串口数据

    总是能在别人的博客中学到太多太多,谢谢各位对知识的无私共享,谢谢大家 前言 去年我使用Qt编写串口通信程序时,将自己的学习过程写成了教程(Qt编写串口通信程序全程图文讲解),但是由于时间等原因,我只实 ...

最新文章

  1. 成功解决_catboost.CatBoostError: Bad value for num_feature: Cannot convert ‘b‘\x8f\x91‘‘ to float
  2. 解引用NULL为什么会导致程序挂死?
  3. ios如何获取gps坐标(定位第二节)
  4. 英寸和厘米的交互python_Python的新手-尝试将厘米编码为英寸,反之亦然
  5. Golang练习题(自己认为比较不错的)
  6. matlab生成不重复的随机数_怎么生成不重复随机数——《超级处理器》应用
  7. 算法篇---java算法应用
  8. 三菱plc控制电动推杆
  9. c语言红外解码程序,红外线遥控器软件解码原理和程序(C语言)
  10. 数据库范式(1 2 3 BCNF范式)详解
  11. 使用Robot Framework实现多平台自动化测试
  12. 一些Mac OS X的使用技巧
  13. 怎么彻底清除计算机病毒,Win7旗舰版系统如何才能彻底删除电脑病毒
  14. 专访刘秋杉:我的人生是一场没有边界的无限游戏
  15. 深度学习课程资源整理
  16. 大数据——Flink dataStream 中窗口函数的使用
  17. UnityShader实战 之 体积光的实现
  18. 常用继电器模块的PCB设计与实物分享
  19. 传感器与测试技术搜题软件,【必备】自考《02202传感器与检测技术》历年真题及答案【23份】【更新至2021年4月题】...
  20. Split The Tree(dfs序+树状数组)

热门文章

  1. python字典转dataframe_python DataFrame转dict字典过程详解
  2. Arduino笔记-Rouch Sensor的使用
  3. Java web中使用JQuery加载某页面后,自动调用Servlet(GET方法,POST方法)
  4. python的坐标代码_基于Python的地图坐标服务接口调用代码实例
  5. android led灯框架_LED面板灯的特点:应用领域、产品结构与产品分类
  6. cron 12点执行_【技术指南】Crontab调度重复执行的任务
  7. mel滤波器组频率响应曲线_非常好的滤波器知识总结,值得一看!
  8. 导出参考文献是ciw格式_使用 EndNote 9 引用参考文献
  9. (王道408考研操作系统)第三章内存管理-第一节8:非连续分配管理方式之段页式管理方式
  10. 面试题7:重建二叉树