发表于:2009/10/10 10:04:09
#0楼
小弟是新手,在FX2N中编入一个最简单的电机正反转的程序,在VB6中编制程序来读取Y0和Y1的状态,结果是向FX2N发出的读取指令发出了,但却没有数据返回
附VB程序
Public y0, y1 As Boolean
Public lim_left, lim_right As Integer
Public Function ReadPlc()
Dim recstr$, cmdstr$
Dim jym As Integer
cmdstr = Chr$(2) + "0000502" + Chr(3)
For n = 2 To 7
jym = Asc(Mid(cmdstr, n, 1)) + jym
Next
MSComm1.Output = cmdstr + Right(Str(Hex(jym + 3)), 2) + Chr(13)
t1 = Timer
Do While (Timer - t1 < 0.1) '延时准备接收
DoEvents
Loop
recstr = MSComm1.Input
Text1.Text = recstr
If Asc(Left(recstr, 1)) = 6 Then
If Mid(recstr, 3, 1) = "1" Then
y0 = True
Else
y0 = False
End If
If Mid(recstr, 2, 1) = "1" Then
y1 = True
Else
y1 = False
End If
Else
Label1.Caption = "读数据错误"
End If
End Function
Private Sub Form_Load()
lim_left = 830
lim_right = 5200
MSComm1.CommPort = 4
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub Timer1_Timer()
ReadPlc
DoEvents
If y0 = True And y1 = False Then
car.Left = car.Left + 15
Label1.Caption = "zz"
ElseIf y0 = False And y1 = True Then
car.Left = car.Left - 15
Label1.Caption = "fz"
ElseIf y0 = False And y1 = False Then
Label1.Caption = "stop"
Else
Label1.Caption = "error"
End If
End Sub
我用的是SC09的USB-232转接头,请各位大侠给看看问题在什么地方,谢谢
附VB程序
Public y0, y1 As Boolean
Public lim_left, lim_right As Integer
Public Function ReadPlc()
Dim recstr$, cmdstr$
Dim jym As Integer
cmdstr = Chr$(2) + "0000502" + Chr(3)
For n = 2 To 7
jym = Asc(Mid(cmdstr, n, 1)) + jym
Next
MSComm1.Output = cmdstr + Right(Str(Hex(jym + 3)), 2) + Chr(13)
t1 = Timer
Do While (Timer - t1 < 0.1) '延时准备接收
DoEvents
Loop
recstr = MSComm1.Input
Text1.Text = recstr
If Asc(Left(recstr, 1)) = 6 Then
If Mid(recstr, 3, 1) = "1" Then
y0 = True
Else
y0 = False
End If
If Mid(recstr, 2, 1) = "1" Then
y1 = True
Else
y1 = False
End If
Else
Label1.Caption = "读数据错误"
End If
End Function
Private Sub Form_Load()
lim_left = 830
lim_right = 5200
MSComm1.CommPort = 4
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub Timer1_Timer()
ReadPlc
DoEvents
If y0 = True And y1 = False Then
car.Left = car.Left + 15
Label1.Caption = "zz"
ElseIf y0 = False And y1 = True Then
car.Left = car.Left - 15
Label1.Caption = "fz"
ElseIf y0 = False And y1 = False Then
Label1.Caption = "stop"
Else
Label1.Caption = "error"
End If
End Sub
我用的是SC09的USB-232转接头,请各位大侠给看看问题在什么地方,谢谢