发表于:2006/2/16 18:48:00
#0楼
最近用VB作了 一个上位机与FX2nPLC串行通讯的程序,不知什么原因总是不能返回需要的信息,请各位朋友指点一二,部分代码如下:
Private Sub Form_Load()
MSComm1.Settings = "9600,e,7,1"
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
MSComm1.OutBufferSize = 1024
MSComm1.OutBufferCount = 0
MSComm1.PortOpen = True
MSComm1.InputLen = 0
End Sub
Private Sub send_Click()
Dim inf As String
inf = CmdRead(Text2.Text)
If inf <> "error" Then
Text1.Text = Text1.Text + inf
Else
MsgBox ("通讯错误")
End If
End Sub
Function CmdRead(Rd As String) As String
Dim x As Integer
Dim Y As Integer
Dim Z As Integer
Dim waittime, starttime As Long
Dim insr As Integer
MSComm1.InBufferCount = 0
read$ = Chr$(5) & Rd & CheckSum(Rd)
'读数据帧由ENQ、命令串和和校验码组成
MSComm1.Output = read$
waittime = 1
starttime = Timer
Do
DoEvents
Loop Until Timer > starttime + waittime
Data$ = MSComm1.Input
If Data$ <> "" Then
insr = Asc(Data$)
CmdRead = Hex$(insr)
Else
CmdRead = "error"
End If
End Function
用ENQ00FFBRAX00000740和ENQ00FFTT204ABCD34等正确指令发送,只返回06H代码,请问有谁知道是怎么回事,PLC内D8120已作了相应设置,用网上下载的软件通讯没问题
Private Sub Form_Load()
MSComm1.Settings = "9600,e,7,1"
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
MSComm1.OutBufferSize = 1024
MSComm1.OutBufferCount = 0
MSComm1.PortOpen = True
MSComm1.InputLen = 0
End Sub
Private Sub send_Click()
Dim inf As String
inf = CmdRead(Text2.Text)
If inf <> "error" Then
Text1.Text = Text1.Text + inf
Else
MsgBox ("通讯错误")
End If
End Sub
Function CmdRead(Rd As String) As String
Dim x As Integer
Dim Y As Integer
Dim Z As Integer
Dim waittime, starttime As Long
Dim insr As Integer
MSComm1.InBufferCount = 0
read$ = Chr$(5) & Rd & CheckSum(Rd)
'读数据帧由ENQ、命令串和和校验码组成
MSComm1.Output = read$
waittime = 1
starttime = Timer
Do
DoEvents
Loop Until Timer > starttime + waittime
Data$ = MSComm1.Input
If Data$ <> "" Then
insr = Asc(Data$)
CmdRead = Hex$(insr)
Else
CmdRead = "error"
End If
End Function
用ENQ00FFBRAX00000740和ENQ00FFTT204ABCD34等正确指令发送,只返回06H代码,请问有谁知道是怎么回事,PLC内D8120已作了相应设置,用网上下载的软件通讯没问题