16,553
社区成员
发帖
与我相关
我的任务
分享
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Str = TextBox1.Text
Hst = Hex(Str)
If Hst.Length < 5 Then
Select Case Hst.Length
Case Is = 1
zh = "000" + Hst
Case Is = 2
zh = "00" + Hst
Case Is = 3
zh = "0" + Hst
Case Is = 4
zh = Hst
End Select
a = Mid(zh, 1, 2)
b = Mid(zh, 3, 4)
' MsgBox(a)
'MsgBox(b)
Strz = "01 10 00 28 00 06 0C " & a & " " & b & " 00 00 D8 F0 FF FF " & c & " " & d
MsgBox(Strz)
End If
Dim TempStr As String
Dim i As Integer
Dim LL As Long
Dim Jyh0 As Byte
Dim Jyh1 As Byte
Dim ValCRC As Long
c = ""
d = ""
If Trim(Strz) = "" Then Exit Sub '是空格,则跳出函数
TempStr = ""
For i = 1 To Len(Strz)
TempStr = Mid(Trim(Strz), i, 1)
If TempStr <> " " Then
SendStr = SendStr + TempStr '去掉全部空格
End If
Next i
LL = Len(SendStr)
If (LL Mod 2) > 0 Then Exit Sub '奇数个字符,则跳出函数
LL = LL / 2 - 1
'重定义数组,下界为0
For i = 0 To LL
Cmd(i) = Val("&H" + Mid(SendStr, i * 2 + 1, 2) + "&") '!!!
Next i
ValCRC = CRC(Cmd)
Jyh0 = ValCRC Mod 256
Jyh1 = Int(ValCRC / 256) '取出CRC高低位
c = Strings.Right("0" + Hex(Jyh0), 2)
d = Strings.Right("0" + Hex(Jyh1), 2) 'Hex(10),返回'A'
RelayControl(Strz)
End Sub
Public Function CRC_part(ByVal Brr() As Byte) As Long ''''''CRC校验核,部分数据进行校验 Brr按地址传递!!!
Dim i As Integer, j As Integer
Dim TempVal As Long
Dim YWval As Long
Dim LSB As Integer
TempVal = &HFFFF&
For i = 0 To UBound(Brr()) - 2
TempVal = TempVal Xor Brr(i)
For j = 1 To 8
YWval = Int(TempVal / 2)
LSB = TempVal Mod 2
If LSB = 1 Then
YWval = YWval Xor &HA001&
End If
TempVal = YWval
Next j
Next i
CRC_part = TempVal Mod 65536
End Function
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Or e.KeyChar = "." Then
If e.KeyChar = "." And InStr(TextBox1.Text, ".") > 0 Then
e.Handled = True
Else
e.Handled = False
End If
Else
e.Handled = True
End If
End Sub
'传入控制器模块指令
Private Sub RelayControl(ByVal RelayInstruct As String)
Try
Dim SendArray() As String = Split(RelayInstruct)
Dim HexBytes() As Byte
ReDim HexBytes(SendArray.Length - 1)
Dim i As Integer
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
SerialPort1.Open()
For i = 0 To 16
HexBytes(i) = Val("&H" & SendArray(i))
Next
SerialPort1.Write(HexBytes, 0, HexBytes.Length)
Catch ex As Exception
Public Function CRC(ByVal Brr() As Byte) As Long ''''CRC校验核,全部数据进行校验 Brr按地址传递!!!
Dim i As Integer, j As Integer
Dim TempVal As Long
Dim YWval As Long
Dim LSB As Integer
TempVal = &HFFFF& '最后的&
For i = 0 To UBound(Brr())
TempVal = TempVal Xor Brr(i)
For j = 1 To 8
YWval = Int(TempVal / 2) 'Int,返回不大于x的最大整数
LSB = TempVal Mod 2
If LSB = 1 Then
YWval = YWval Xor &HA001& '?????
End If
TempVal = YWval
Next j
Next i
CRC = TempVal Mod 65536
End Function
End Try
End Sub
Private Sub Label2_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.ParentChanged
Label2.Text = Strz
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
RelayControl(Hstop)
End Sub
End Class
Public Function CRC(ByVal Brr() As Byte) As Long ''''CRC校验核,全部数据进行校验 Brr按地址传递!!!
For i = 0 To UBound(Brr())