Imports System.IO.Ports
Public Class SharedFunction
#Region "串口操作"
'打开串口
'''
''' 打开串口
'''
''' 串口
''' 串口号
''' 波特率
'''
Public Shared Function OpenSerial(m_Serial As SerialPort, portName As String, baudRate As Integer) As Boolean
If IsNothing(m_Serial) OrElse m_Serial.IsOpen Then Return False
Try
m_Serial.PortName = portName
m_Serial.BaudRate = baudRate
m_Serial.Parity = Parity.Even
m_Serial.Open()
Return True
Catch ex As Exception
Return False
End Try
End Function
'设置串口波特率
'''
''' 设置串口波特率
'''
''' 串口
''' 波特率
'''
Public Shared Function SetSerialBaudRate(m_Serial As SerialPort, baudRate As Integer) As Boolean
If IsNothing(m_Serial) OrElse Not m_Serial.IsOpen Then Return False
m_Serial.BaudRate = baudRate
Return True
End Function
'''
''' 关闭串口
'''
''' 串口
''' 串口关闭标志
''' 串口等待关闭标志
Public Shared Sub CloseSerial(m_Serial As SerialPort, ByRef isread As Boolean, ByRef isListen As Boolean)
If IsNothing(m_Serial) OrElse Not m_Serial.IsOpen Then Return
isread = True
While isListen
Application.DoEvents()
Threading.Thread.Sleep(10)
End While
Threading.Thread.Sleep(10)
m_Serial.Close()
isread = False
End Sub
#End Region
#Region "数据转换"
Public Shared Function ByteToHexString(databuff() As Byte) As String
Dim strData As String = String.Empty
For i = 0 To databuff.Length - 1
strData &= $" {Hex(databuff(i)).PadLeft(2, "0"c)}"
Next
Return strData
End Function
Public Shared Function StringToHexString(hexstr As String) As String
'判断字符串是否为空或 者为空字符串
If String.IsNullOrEmpty(hexstr) Then Return Nothing
'替换字符中的分隔符号
hexstr = hexstr.Trim().Replace(" ", "").Replace("-", "").Replace(",", "").Replace(":", "").Replace(";", "").Replace(".", "")
'判断字符串的长度是否为偶数
If hexstr.Length Mod 2 <> 0 Then
'不为偶数往最后一个字节前面补0
hexstr = hexstr.Insert(hexstr.Length - 1, "0")
End If
'以2为步长,将字符串转换为字节数组 不足2位前面补0
Dim databuff() As Byte = Enumerable.Range(0, hexstr.Length).Where(Function(x) x Mod 2 = 0).Select(Function(x) Convert.ToByte(hexstr.Substring(x, 2), 16)).ToArray()
Return ByteToHexString(databuff)
End Function
Public Shared Function HexStringToByte(hexstr As String) As Byte()
'判断字符串是否为空或 者为空字符串
If String.IsNullOrEmpty(hexstr) Then Return Nothing
'替换字符中的分隔符号
hexstr = hexstr.Trim().Replace(" ", "").Replace("-", "").Replace(",", "").Replace(":", "").Replace(";", "").Replace(".", "")
'判断字符串的长度是否为偶数
If hexstr.Length Mod 2 <> 0 Then
'不为偶数往最后一个字节前面补0
hexstr = hexstr.Insert(hexstr.Length - 1, "0")
End If
'以2为步长,将字符串转换为字节数组 不足2位前面补0
Return Enumerable.Range(0, hexstr.Length).Where(Function(x) x Mod 2 = 0).Select(Function(x) Convert.ToByte(hexstr.Substring(x, 2), 16)).ToArray()
End Function
'''
''' Int转2个字节Byte
''' 高字节在前,低字节在后
'''
'''
'''
Public Shared Function IntToByteHB(ByVal i As Integer) As Byte()
Dim btemp() As Byte = {0, 0}
Dim b() As Byte = BitConverter.GetBytes(i)
btemp(0) = b(0)
btemp(1) = b(1)
Return btemp
End Function
'''
''' Int转2个字节Byte
''' 高字节在前,低字节在后
'''
'''
'''
Public Shared Function IntToByteLB(ByVal i As Integer) As Byte()
Dim btemp() As Byte = {0, 0}
Dim b() As Byte = BitConverter.GetBytes(i)
btemp(0) = b(1)
btemp(1) = b(0)
Return btemp
End Function
#End Region
#Region "数据校验"
'计算数据和 校验
Public Shared Function GetCheckSum(ByVal data As Byte()) As Byte
Dim sum As Integer = 0
For i As Integer = 0 To data.Length - 1
sum += data(i)
Next
Return sum
End Function
Public Shared Function GetCheckSumMod(ByVal data As Byte()) As Byte
Dim sum As Integer = 0
For i As Integer = 0 To data.Length - 2
sum += data(i)
Next
sum = sum And &HFF
Return sum
End Function
Public Shared Function GetCheckSumMod2(ByVal data As Byte()) As Byte
Dim sum As Integer = 0
For i As Integer = 0 To data.Length - 1
sum += data(i)
Next
sum = sum And &HFF
Return sum
End Function
'''
''' 和校验取余数
''' 求Byte数组的和校验取余数
'''
''' Byte数组
'''
Public Function GetSumCheckMod(dataPacket As Byte()) As Byte
Dim sum As Integer
For idx = 0 To dataPacket.Length - 1
sum += dataPacket(idx)
sum = sum And &HFF
Next
Dim sumMod As Byte = &HFF - sum
Return sumMod
End Function
#End Region
#Region "跨线程调用UI"
Delegate Sub delSetFromUI(control As Control, mText As String, textcolor As Color, cbolor As Color)
Public Shared Sub SetFromUI(ByVal control As Control, mText As String, textcolor As Color, cbolor As Color)
If IsNothing(control) Then Return
If control.InvokeRequired Then
control.Invoke(New delSetFromUI(AddressOf SetFromUI), control, mText, textcolor, cbolor)
Else
control.Text = mText
control.ForeColor = textcolor
control.BackColor = cbolor
End If
End Sub
#End Region
End Class