发表于:2004/12/14 15:41:00
#0楼
程序如下,我用vb编写上位机和光洋的SH通讯的程序,就是我向PLC写入write1 = "4E" & "21" & "05"之后,PLC会回复 "4E" & "21" & "06"
但是每当我运行的时候,总是出现错误:8005 串口已打开
我是初学者,希望懂一点的朋友,教一下,现在问个人真的不容易啊!!
Option Explicit
Private Sub Command1_Click()
Dim write1 As String
Dim w_fg As Boolean
write1 = "4E" & "21" & "05"
Text1.Text = write1
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = write1
MSComm1.PortOpen = False
End Sub
Private Sub Form_Load()
Dim setstring As String
Dim write1 As String
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
setstring = "9600,n,8,1"
MSComm1.CommPort = 3 '设置通讯口
MSComm1.Settings = setstring '设置通讯参数
MSComm1.InBufferCount = 0 '清空接受缓冲
MSComm1.InputLen = 0 '使 MSComm 控件读取接收缓冲区中全部的内容
MSComm1.DTREnable = False 'yuan wei false
MSComm1.InputMode = comInputModeBinary '二进制方式读取
MSComm1.Handshaking = 0
MSComm1.RThreshold = 1 '每收到一个数据产生一个OnComm事件
MSComm1.SThreshold = 1
End Sub
Private Sub MSComm1_OnComm() '接收到数据!!!!!!以后
dim data() as Byte
If MSComm1.CommEvent = comEvReceive Then '接收到数据!!!!!!以后
Data = MSComm1.Input
'显示通讯过程中的数据,可以方便调试
If Data(0) = &H4E And Data(1) = &H21 And Data(2) = &H6 Then '如果读到了ACK信号
Text1.Text = Data
End If
End Sub
但是每当我运行的时候,总是出现错误:8005 串口已打开
我是初学者,希望懂一点的朋友,教一下,现在问个人真的不容易啊!!
Option Explicit
Private Sub Command1_Click()
Dim write1 As String
Dim w_fg As Boolean
write1 = "4E" & "21" & "05"
Text1.Text = write1
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = write1
MSComm1.PortOpen = False
End Sub
Private Sub Form_Load()
Dim setstring As String
Dim write1 As String
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
setstring = "9600,n,8,1"
MSComm1.CommPort = 3 '设置通讯口
MSComm1.Settings = setstring '设置通讯参数
MSComm1.InBufferCount = 0 '清空接受缓冲
MSComm1.InputLen = 0 '使 MSComm 控件读取接收缓冲区中全部的内容
MSComm1.DTREnable = False 'yuan wei false
MSComm1.InputMode = comInputModeBinary '二进制方式读取
MSComm1.Handshaking = 0
MSComm1.RThreshold = 1 '每收到一个数据产生一个OnComm事件
MSComm1.SThreshold = 1
End Sub
Private Sub MSComm1_OnComm() '接收到数据!!!!!!以后
dim data() as Byte
If MSComm1.CommEvent = comEvReceive Then '接收到数据!!!!!!以后
Data = MSComm1.Input
'显示通讯过程中的数据,可以方便调试
If Data(0) = &H4E And Data(1) = &H21 And Data(2) = &H6 Then '如果读到了ACK信号
Text1.Text = Data
End If
End Sub