这个是小弟写的程序,肯定还会有很多漏洞,希望论坛里的各位老大哥,帮我完善完善。希望大家都能在工控行业一展宏图。
Private Sub Command1_Click()
Dim sendata As String
Dim tim As String
Dim ml As String
Dim i As String
Dim a As String
Dim c As String
Dim d As String
i = Text1.Text
a = Hex(i)
c = Strings.Right("0000" & a, 4)
d = Mid(c, 3, 2) + Mid(c, 1, 2)
ml = "1" + "1000" + "02" + d + Chr(3)
sendata = Chr(2) + ml + sumchk(ml)
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.Output = sendata
Do
dummy = DoEvents
Loop Until MSComm1.InBufferCount >= 8
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
End Sub
Private Function sumchk(dats As String) As String
Dim i
Dim CHK
For i = 1 To Len(dats)
CHK = CHK + Asc(Mid(dats, i, 1))
Next i
sumchk = Right(Hex(CHK), 2)
End Function
Private Sub Timer1_Timer()
Dim mlbl As String
Dim num As String
Dim sendata As String
Dim getdata As String
Dim tim
mlbl = "0" + "1000" + "02" + Chr(3)
sendata = Chr(2) + mlbl + sumchk(mlbl)
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.Output = sendata
tim = Timer
Do
If Timer > tim + 1 Then
Text3.Text = "通信超时"
Exit Sub
End If
Loop Until MSComm1.InBufferCount >= 8
getdata = MSComm1.Input
getdata = Mid(getdata, 2, 4)
Text2.Text = Val("&H" + Mid(getdata, 3, 2) + Mid(getdata, 1, 2))
End Sub