发表于:2003/4/26 9:42:00
#0楼
plc和pc的通讯方面的问题,我不知道plc的通信指令怎么编,对很多概念还不是很清楚,比如说通信的格式。我想用vb界面:
用图中的搅拌按钮来触发plc中的指令,再由plc发送指令到pc,使界面中的圆圈和指针转动。
plc的梯形图是:
│─‖─── ╫─────────〇 y0
│ x0 │ │
│─‖── ───────〇T0 k100
y0
那plc的通信指令怎样编写啊?我用三菱的fx0n的。
还有,下面是我的vb界面的程序:请各位高手指教!!谢谢!!
Dim X, Y, xchange, ychange As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
If Option1.Value = True Then
MSComm1.CommPort = 1
ElseIf Option2.Value = True Then
MSComm1.CommPort = 2
Else
MsgBox ("选择串口")
End If
MSComm1.Settings = "9600,n,8,1"
'设置端口参数:传输速率9600,无校验,8位数据,1位停止位
End Sub
Private Sub Commandread_Click()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '打开串行端口
End If
MSComm1.InputLen = 0 '串行数据接收缓冲区初始化
rd$ = Chr$(5) + "01FFWR0D000001" + Chr$(13) + Chr$(10)
'准备向PLC发送的读取数据命令字符串
MSComm1.Output = rd$ '向PLC发送该字符串
Do
Dummy = DoEvents
Loop Until MSComm1.InBufferCount >= 9
rdh$ = MSComm1.Input '读取接收缓冲区数据
pd$ = Mid$(rdh$, 1, 5)
'取PLC应答字符串的前5位
If pd$ = Chr$(2) + "01FF" Then '判断读取是否正确
js$ = Chr$(6) + "01FF"
'向PLC发送已正确接收数据信号
MSComm1.Output = js$
rdh$ = Mid$(rdh$, 6, 4) '检出DO内数据并将其转化为十进制数
rdh = Val("&H" + Trim(rdh$))
X = MsgBox("读取正常", 16)
Else
X = MsgBox("读取数据错误!", 16)
End If
End Sub
Private Sub Commandwrite_Click()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '打开串行口
End If
MSComm1.InputLen = 0 '串行数据接收缓冲区初始化
szt$ = Trim(Str(Int(Timer / 60)))
xr$ = Trim(Str(Val(Space(4 - Len(szt$)))) + Hex(Val(szt$)))
'取上位机时钟值将其转化为PLC要求的格式
wr$ = Chr$(5) + "01FFWW0CN25001" + xr$ + Chr$(13) + Chr$(10)
'准备向PLC发送的写入数据命令字符串
MSComm1.Output = wr$ '向PLC发送该命令字符串
Do '等待接收PLC回答的字符串
Dummy = DoEvents
Loop Until MSComm1.InBufferCount >= 5
fh$ = MSComm1.Input '读取接收缓冲区数据
jc$ = Mid$(fh$, 1, 5) '取PLC应答字符串的前5位
If jc$ = Chr$(6) + "01FF" Then '判断写入是否正确
X = MsgBox("写入正常!", 16)
Else
X = MsgBox("没有正确写入!", 16)
End If
End Sub
Private Sub Commandclose_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关闭串行端口
X = MsgBox("关闭通信!", 16)
End If
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
X = 480
Y = 0
xchange = -1
ychange = 1
End Sub
Private Sub Option1_Click()
Option2.Value = False
End Sub
Private Sub Option2_Click()
Option1.Value = False
End Sub
Private Sub Timer1_Timer()
If X = 480 Then
xchange = -1
ElseIf X = -480 Then
xchange = 1
End If
X = X + 480 * xchange
Line1.X2 = Line1.X1 + X
If Y = 480 Then
ychange = -1
ElseIf Y = -480 Then
ychange = 1
End If
Y = Y + ychange * 480
Line1.Y2 = Line1.Y1 + Y
End Sub