Dim 读操作 As Boolean
Dim 写操作 As Boolean
Dim xie As Integer
Dim g As Integer
Dim t
Private Sub Command1_Click(Index As Integer)
xie = Index
写操作 = True
End Sub
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End
End Sub
Private Sub Form_Load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 3
MSComm1.Settings = "9600,e,7,1"
MSComm1.Handshaking = 0
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.InputLen = 0
MSComm1.PortOpen = True
i = 0
读操作 = False
写操作 = False
End Sub
Private Sub MSComm1_OnComm()
Dim Shou As String
Dim shi(1) As String
Dim wei(1) As String
Dim y(11) As String
If MSComm1.CommEvent = comEvReceive Then
Shou = MSComm1.Input
If 写操作 = True And 读操作 = False Then
写操作 = False
Timer1.Enabled = True
Else
Select Case g
Case 0
shou1 = Mid(Shou, 2, 4)
shi(0) = Val("&H" & Mid(shou1, 1, 2))
shi(1) = Val("&H" & Mid(shou1, 2, 4))
For j = 0 To 1
wei(j) = dec2bin(shi(j))
Next j
gfd = wei(1) + wei(0)
Case 1
d4 = Val("&H" & Mid(Shou, 4, 2) + Mid(Shou, 2, 2))
Case 2
d5 = Val("&H" & Mid(Shou, 4, 2) + Mid(Shou, 2, 2))
End Select
Select Case g
Case 0
For j = 0 To 11
y(j) = Mid(gfd, 16 - j, 1)
If y(0) = "1" Then
Shape1(0).FillColor = RGB(255, 0, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(1) = "1" Then
Shape1(1).FillColor = RGB(0, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(2) = "1" Then
Shape1(2).FillColor = RGB(255, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(3) = "1" Then
Shape1(3).FillColor = RGB(255, 0, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(4) = "1" Then
Shape1(4).FillColor = RGB(0, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(5) = "1" Then
Shape1(5).FillColor = RGB(255, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(6) = "1" Then
Shape1(6).FillColor = RGB(255, 0, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(7) = "1" Then
Shape1(7).FillColor = RGB(0, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(8) = "1" Then
Shape1(8).FillColor = RGB(255, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(9) = "1" Then
Shape1(9).FillColor = RGB(255, 0, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(10) = "1" Then
Shape1(10).FillColor = RGB(0, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
If y(11) = "1" Then
Shape1(11).FillColor = RGB(255, 255, 0)
Else
Shape1(j).FillColor = RGB(0, 0, 0)
End If
Next j
Case 1
Label5(0).Caption = d4
Label3(6).Caption = d4
Label3(7).Caption = d4
'Label4.Caption = "0" & Mid(Timer - t, 1, 4)
Case 2
Label5(1).Caption = d5
Label3(4).Caption = d5
Label3(5).Caption = d5
Label5(2).Caption = "0" + Mid(Timer - t, 1, 4)
g = -1
End Select
g = g + 1
读操作 = False
Timer1.Enabled = True
End If
End If
End Sub
Private Sub Timer1_Timer()
Dim Dats As String
If g = 0 Then t = Timer
If 写操作 = True Then
MSComm1.RThreshold = 1
Select Case xie
Case 0
Dats = "70008" + Chr(3)
Case 1
Dats = "80008" + Chr(3)
Case 2
a = Right("0000" & Hex(Val(Text1.Text) * 10), 4)
a1 = Right(a, 2) & Left(a, 2)
Dats = "1100002" + a1 + Chr(3)
Case 3
b = Right("0000" & Hex(Val(Text2.Text) * 10), 4)
b1 = Right(b, 2) & Left(b, 2)
Dats = "1100202" + b1 + Chr(3)
Case 4
b = Right("0000" & Hex(Val(Text3.Text) * 1), 4)
b1 = Right(b, 2) & Left(b, 2)
Dats = "1100402" + b1 + Chr(3)
Case 5
b = Right("0000" & Hex(Val(Text4.Text) * 1), 4)
b1 = Right(b, 2) & Left(b, 2)
Dats = "1100602" + b1 + Chr(3)
End Select
Else
读操作 = True
Select Case g
Case 0
MSComm1.RThreshold = 8
Dats = "000A002" + Chr(3)
Case 1
MSComm1.RThreshold = 8
Dats = "0100802" + Chr(3)
Case 2
MSComm1.RThreshold = 8
Dats = "0108202" + Chr(3)
End Select
End If
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.Output = Chr(2) + Dats + SumChk(Dats$)
Timer1.Enabled = False
End Sub
Private Function SumChk(Dats$) As String '校验和计算
Dim i&
Dim CHK&
For i = 1 To Len(Dats) 'Len 函数,返回 Long,其中包含字符串内字符的数目,或是存储一变量所需的字节数
CHK = CHK + Asc(Mid(Dats, i, 1)) 'Asc函数,返回一个 Integer,代表字符串中首字母的字符代码。返回 Variant (String),其中包含字符串中指定数量的字符
Next i
SumChk = Right(Hex$(CHK), 2) 'SumChk = Right(Hex$(CHK + 3), 2)
End Function
Private Function dec2bin(Dats$) As String '转换成二进制
Dim bin8
Dim bin4
Dim bin2
Dim bin1
Dim bin16
Dim bin32
Dim bin64
Dim bin128
If Dats \ 128 >= 1 Then
bin128 = 1
Else
bin128 = 0
End If
If (Dats Mod 128) \ 64 >= 1 Then
bin64 = 1
Else
bin64 = 0
End If
If (Dats Mod 64) \ 32 >= 1 Then 'Mod用来对两个数作除法并且只返回余数
bin32 = 1
Else
bin32 = 0
End If
If (Dats Mod 32) \ 16 >= 1 Then
bin16 = 1
Else
bin16 = 0
End If
If (Dats Mod 16) \ 8 >= 1 Then '\ 运算符用来对两个数作除法并返回一个整数
bin8 = 1
Else
bin8 = 0
End If
If (Dats Mod 8) \ 4 >= 1 Then 'Mod用来对两个数作除法并且只返回余数
bin4 = 1
Else
bin4 = 0
End If
If (Dats Mod 4) \ 2 >= 1 Then
bin2 = 1
Else
bin2 = 0
End If
If Dats Mod 2 = 0 Then
bin1 = 0
Else
bin1 = 1
End If
bin128 = CStr(bin128) 'CStr 函数将一数值转换为 String
bin64 = CStr(bin64)
bin32 = CStr(bin32)
bin16 = CStr(bin16)
bin8 = CStr(bin8) 'CStr 函数将一数值转换为 String
bin4 = CStr(bin4)
bin2 = CStr(bin2)
bin1 = CStr(bin1)
dec2bin = bin128 + bin64 + bin32 + bin16 + bin8 + bin4 + bin2 + bin1
End Function