IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> vb.net写的串口通讯类模块 源码 -> 正文阅读

[嵌入式]vb.net写的串口通讯类模块 源码

以下是vb.net写的串口 通讯模块代码,主要分类打开串口,关闭串口,发送数据,数据转换,包括了ASCII和CRC16?MODBUS通讯协议两种:


Imports System.IO.Ports
Module SerialModule
  Public SendHex As String = "" '发送中的缓存数据
  Public PlcName As String = "COM1", PlcNum As Integer = 1, PlcCOM As New IO.Ports.SerialPort '显示屏串口与屏号
  Public WeighNum As Integer = 1, Weigh1 As Integer = 0, Weigh2 As Integer = 0, Weigh3 As Integer = 0, Weigh4 As Integer = 0, Weigh5 As Integer = 0, Weigh6 As Integer = 0

  Public Car1Min As Integer = 1000, Car1Max As Integer = 1800, Car1Dist As Integer = 100
  Public Car2Min As Integer = 1000, Car2Max As Integer = 1800, Car2Dist As Integer = 100





  'Function StrDup(Str As String, Optional len As Integer = 4, Optional Character As String = "0") As String
  '  Dim n As Integer = len - Str.Length
  '  If n <= 0 Then Return Str.Substring(0, len)
  '  Return Strings.StrDup(n, Character) & Str
  'End Function

  Sub PortList(Combox As ComboBox, Optional COMM As String = "")
    For Each R As String In SerialPort.GetPortNames()
      Combox.Items.Add(R)
    Next
    Combox.Text = COMM
  End Sub
  Function PortOpen(Port As SerialPort, COMM As String) As Boolean
    If Port.IsOpen = True Then Port.Close()
    Try
      Port.PortName = COMM
      Port.Open()
    Catch ex As Exception
    End Try
    Return Port.IsOpen
  End Function
  Sub PortClose(Port As SerialPort)
    If Port.IsOpen = True Then Port.Close()
  End Sub

  Function PortSendHex(Port As SerialPort, data As String, Optional ReadLen As Integer = 10) As String
    Dim WStr As String = data.Replace("-", "")
    WStr &= CRC16(WStr)
    SendHex = WStr
    If Port.IsOpen = False Then Return ""
    Dim bytes() As Byte = HexToByte(WStr), Buffer As Byte() = New Byte(8192) {}, Len As Integer, RStr As String = "", StartTick As Long = Now.Ticks
    Port.Write(bytes, 0, bytes.Length)
    Do
      Threading.Thread.Sleep(1)
      Len = Port.BytesToRead
      If Len > 0 Then
        Port.Read(Buffer, 0, Len)
        For i As Integer = 0 To Len - 1
          RStr &= Buffer(i).ToString("X2")
        Next
      End If
    Loop While RStr.Length < ReadLen AndAlso Now.Ticks - StartTick < 10000 * 500
    Return RStr
  End Function
  Function PortSendASCII(Port As SerialPort, data As String) As String
    Dim WStr As String = "3A" & ToHex(data.Replace("-", ""))
    WStr &= LRC(WStr) & "0D0A" '校验
    SendHex = WStr
    If Port.IsOpen = False Then Return ""
    Dim bytes() As Byte = HexToByte(WStr), Buffer As Byte() = New Byte(8192) {}, Len As Integer, RStr As String = "", StartTick As Long = Now.Ticks
    Port.Write(bytes, 0, bytes.Length)
    Do
      Threading.Thread.Sleep(1)
      Len = Port.BytesToRead
      If Len > 0 Then
        Port.Read(Buffer, 0, Len)
        For i As Integer = 0 To Len - 1
          RStr &= Buffer(i).ToString("X2")
        Next
      End If
    Loop While RStr.Contains("0D0A") = False AndAlso Now.Ticks - StartTick < 10000 * 500
    Return ToAsc(RStr)
  End Function
  Function ToHex(str As String) As String
    Dim RStr As String = ""
    For i As Integer = 0 To str.Length - 1
      RStr &= Asc(str.Substring(i, 1)).ToString("X2")
    Next
    Return RStr
  End Function
  Function ToAsc(Hex As String) As String
    Dim RStr As String = "", len As Integer = Hex.Length / 2
    For i As Integer = 0 To len - 1
      RStr &= Chr(CByte("&H" & Hex.Substring(i * 2, 2)))
    Next
    Return RStr
  End Function

  Function HexToByte(Hex As String) As Byte()
    Dim len As Integer = Hex.Length / 2, bytes(len - 1) As Byte
    For i As Integer = 0 To len - 1
      bytes(i) = CByte("&H" & Hex.Substring(i * 2, 2))
    Next
    Return bytes
  End Function

  Function ByteToHex(bytes() As Byte) As String
    Dim RStr As String = ""
    For i As Integer = 0 To bytes.Length - 1
      RStr &= bytes(i).ToString("X2")
    Next
    Return RStr
  End Function
  Function LRC(Str As String) As String
    Dim len As Integer = Str.Length / 2, sum As Integer = 0 '校验
    For i As Integer = 0 To len - 1
      sum += CByte("&H" & Str.Substring(i * 2, 2))
    Next
    Return (sum Mod 256).ToString("X2")
  End Function
  Function CRC16(Str As String) As String
    Dim len As Integer = Str.Length / 2, crc As UInt16 = &HFFFF '校验
    For i As Integer = 0 To len - 1
      crc = crc Xor CByte("&H" & Str.Substring(i * 2, 2))
      For j As Integer = 1 To 8
        If crc Mod 2 = 1 Then
          crc = (crc \ 2) Xor &HA001
        Else
          crc = crc \ 2
        End If
      Next
    Next
    Str = crc.ToString("X4")
    Return Str.Substring(2) & Str.Substring(0, 2) '高低反位
  End Function
  Function HexToInt(Str As String) As Integer
    If Str = "00000000" OrElse Str = "FFFFFFFF" Then Return 0
    If CByte("&H" & Str.Substring(0, 2)) <= &H7F Then Return CInt("&H" & Str)
    Return -(4294967295 - CUInt("&H" & Str))
  End Function
  Function IntToHex(v As Integer) As String
    If v = 0 Then Return "00000000"
    If v > 0 Then
      Dim R As String = v.ToString("X8")
      Return R.Substring(4) & R.Substring(0, 4) '高低反位
    End If
    Dim Str As String = (4294967295 + v).ToString("X8")
    Return Str.Substring(4) & Str.Substring(0, 4) '高低反位
  End Function



End Module

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-02-03 01:20:18  更:2022-02-03 01:20:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 10:50:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码