測試用,其他流程自行修改
Private Sub Command1_Click()
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False
Timer1.Enabled = False
Timer2.Enabled = False
End
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
cmm$ = "@00RD00000001" 'READ DM 0000
GoSub FCS
cmm1$ = cmm$ + R$ + "*"
Label1.Caption = cmm1$
MSComm1.Output = Trim(cmm1$ + Chr(13))
' Ū¨ú©Ò¦³¥i¥Î¸ê®Æ.
MSComm1.InputLen = 0
' Àˬd¸ê®Æ.
If MSComm1.InBufferCount Then
' Read data.
INSTRING1 = MSComm1.Input
End If
Label2.Caption = INSTRING1
Label3.Caption = Str(Val(Mid(INSTRING1, 8, 4)))
Label4.Caption = Str(Val(Mid(INSTRING1, 12, 2)))
Exit Sub
FCS:
Q = 0
L = Len(cmm$)
For I = 1 To L
Q = Asc(Mid(cmm$, I, 1)) Xor Q
Next I
R$ = Hex$(Q)
If Len(R$) = 1 Then
R$ = "0" + R$
End If
Return
End Sub
Private Sub Timer2_Timer()
Wmm$ = "@00WD0000" + Text1 'WRITE DM0000
GoSub FCS
Wmm1$ = Wmm$ + R$ + "*"
Label1.Caption = Wmm1$
MSComm1.Output = Trim(Wmm1$ + Chr(13))
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then
' Read data.
INSTRING2 = MSComm1.Input
End If
Label2.Caption = INSTRING2
Label3.Caption = ""
Label4.Caption = ""
Exit Sub
FCS:
Q = 0
L = Len(Wmm$)
For I = 1 To L
Q = Asc(Mid(Wmm$, I, 1)) Xor Q
Next I
R$ = Hex$(Q)
If Len(R$) = 1 Then
R$ = "0" + R$
End If
Return
End Sub