发表于:2002/5/11 13:37:00
#0楼
以下是我编的串行通讯(CPM1),但是运行不出来,请那位高手指点一下,谢谢
Option Explicit
Dim AnswerL As Integer
Dim MLetter As String
Private Sub CmbCommand_Change()
Lbl3.Caption = ""
CmbCommand.Text = UCase(CmbCommand.Text)
If Len(CmbCommand.Text) = 2 Then
Call CmbCommand_Click
End If
End Sub
Private Sub CmbCommand_Click()
Lbl3.Caption = ""
Lbl3.Caption = ModCmb.Cmb_Information(CmbCommand.Text)
End Sub
'按取消键,取消发送并清空发送数据
Private Sub CmdECancle_Click()
TxtSend.Text = ""
End Sub
'关闭端口并退出系统
Private Sub CmdEnd_Click()
MSComm1.PortOpen = False
End
End Sub
'发送数据,并计算FCS
Private Sub CmdSend_Click()
Dim buf As String
Dim dummy As Integer
Call PLC_Send("00", CmbCommand.Text, TxtSend.Text)
End Sub
Private Sub Form_Load()
MSComm1.CommPort = Port + 1
MSComm1.Handshaking = Flow
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
'根据各引脚的电平的变化触发相应的事件
Select Case MSComm1.CommEvent
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvSend
Call CmdSend_Click
MSComm1.RThreshold = AnswerL
Case comEvReceive
Call PLC_Receive(MSComm1.Input)
Case comEvEOF
End Select
End Sub
'接收桢的FCS的计算
Private Function FCS_Tag(buf As String) As Integer
Dim RFCS As String
Dim FCSCK As String
Dim i As Integer
Dim L As Integer
Dim Q As Integer
Dim A As String
Q = 0
L = Len(buf)
'判别是否是一桢接收完数据
A = Right$(buf, 1)
If A = "*" Then
L = L - 3
Else: L = L - 2
End If
RFCS = Mid$(buf, L + 1, 2)
For i = 1 To L
Q = Asc(Mid$(buf, i, 1)) Xor Q
Next i
FCSCK = Hex(Q)
If Len(FCSCK) = 1 Then FCSCK = "0" + FCSCK
If RFCS <> FCSCK Then
FCS_Tag = 0
Else
FCS_Tag = 1
End If
End Function
'发送函数
Private Sub PLC_Send(Num As String, Command As Strin
Option Explicit
Dim AnswerL As Integer
Dim MLetter As String
Private Sub CmbCommand_Change()
Lbl3.Caption = ""
CmbCommand.Text = UCase(CmbCommand.Text)
If Len(CmbCommand.Text) = 2 Then
Call CmbCommand_Click
End If
End Sub
Private Sub CmbCommand_Click()
Lbl3.Caption = ""
Lbl3.Caption = ModCmb.Cmb_Information(CmbCommand.Text)
End Sub
'按取消键,取消发送并清空发送数据
Private Sub CmdECancle_Click()
TxtSend.Text = ""
End Sub
'关闭端口并退出系统
Private Sub CmdEnd_Click()
MSComm1.PortOpen = False
End
End Sub
'发送数据,并计算FCS
Private Sub CmdSend_Click()
Dim buf As String
Dim dummy As Integer
Call PLC_Send("00", CmbCommand.Text, TxtSend.Text)
End Sub
Private Sub Form_Load()
MSComm1.CommPort = Port + 1
MSComm1.Handshaking = Flow
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
'根据各引脚的电平的变化触发相应的事件
Select Case MSComm1.CommEvent
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvSend
Call CmdSend_Click
MSComm1.RThreshold = AnswerL
Case comEvReceive
Call PLC_Receive(MSComm1.Input)
Case comEvEOF
End Select
End Sub
'接收桢的FCS的计算
Private Function FCS_Tag(buf As String) As Integer
Dim RFCS As String
Dim FCSCK As String
Dim i As Integer
Dim L As Integer
Dim Q As Integer
Dim A As String
Q = 0
L = Len(buf)
'判别是否是一桢接收完数据
A = Right$(buf, 1)
If A = "*" Then
L = L - 3
Else: L = L - 2
End If
RFCS = Mid$(buf, L + 1, 2)
For i = 1 To L
Q = Asc(Mid$(buf, i, 1)) Xor Q
Next i
FCSCK = Hex(Q)
If Len(FCSCK) = 1 Then FCSCK = "0" + FCSCK
If RFCS <> FCSCK Then
FCS_Tag = 0
Else
FCS_Tag = 1
End If
End Function
'发送函数
Private Sub PLC_Send(Num As String, Command As Strin