三菱PLC与VB 怎么通讯啊
各位大侠,谁有VB与三菱PLC232的通讯源码啊,有就麻烦给一份啊,我不知道通讯协议! 哥们,你来的很直接哦,这个你的找厂家要 请看三菱plc手册的相关内容 http://download.csdn.net/source/2515134 关于串口通信,参考一下这个,VB编写的串口调试器源代码,依照通信协议构建自己的通信命令字符串即可。http://download.csdn.net/source/1262066 这个得看说明书了,一般正规大厂的东西资料都很齐全的.
Dim auto As Boolean
Dim ONOFF As Boolean '按钮操作标志
Dim k As Integer '按钮操作判断 k=1为发自动操作命令、k=2为发手动操作命令、k=3为发手动喷命令、k=4为发手动滴命令
Dim j As Integer '轮循判断
Dim DATA As Boolean '读操作标志
Dim sendata As String, sendata1 As String, sendata2 As String 'sendata:读输入,sendata1:读输出,sendata2:读内部继电器
Dim sendata3 As String, sendata4 As String, sendata5 As String, sendata6 As String 'sendata3:自动,sendata4:手动,sendata5:手动喷,sendata6:手动滴
Private Sub Command1_Click() '手动喷
k = 3
ONOFF = True '按钮操作置真
auto = False
End Sub
Private Sub Command2_Click() '手动滴
k = 4
ONOFF = True '按钮操作置真
auto = True
End Sub
Private Sub Command3_Click() '手动/自动转换
Dim sendata3 As String
If Text3.Text = "手动" Then '如果目前为手动状态,则转化为自动状态
k = 1
Else
k = 2
End If
ONOFF = True '按钮操作置真
End Sub
Private Sub Form_Load()
On Error GoTo err1
sendata = "@00RR00000001" '"@" + "00" + "RR" + "0000" + "0001"---@:固定格式,"00":站号,"RR":读出输入/输出、内部辅助继电器命令,"0000":继电器地址,"0001":读取数量
sendata1 = "@00RR00100001" '"@" + "00" + "RR" + "0010" + "0001"---@:固定格式,"00":站号,"RR":读出输入/输出、内部辅助继电器命令,"0010":继电器地址,"0001":读取数量
sendata2 = "@00RR02000001" '"@" + "00" + "RR" + "2000" + "0001"---@:固定格式,"00":站号,"RR":读出输入/输出、内部辅助继电器命令,"2000":继电器地址,"0001":读取数量
sendata3 = "@00KSCIO 020000" '"@" + "00" + "KS" + "CIO " + "0200" + "00"---@:固定格式,"00":站号,"KS":强制置位命令,"CIO ":置位的继电器种类(输出、输入、内部、特殊辅助),"0200":通道号,"00":接点
sendata4 = "@00KRCIO 020000" '"@" + "00" + "KR" + "CIO " + "0200" + "00"---@:固定格式,"00":站号,"KR":强制置位命令,"CIO ":置位的继电器种类(输出、输入、内部、特殊辅助),"0200":通道号,"00":接点
sendata5 = "@00WR02010001" '"@" + "00" + "WR" + "0200" + "0001"---@:固定格式,"00":站号,"WR":写入命令,"0201":通道号,"0001":置201.00并复位201.01
sendata6 = "@00WR02010002" '"@" + "00" + "WR" + "0200" + "0002"---@:固定格式,"00":站号,"WR":写入命令,"0201":通道号,"0002":置201.01并复位201.00
i = 0
k = 0
ONOFF = False
DATA = False
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = 1 '通信口
MSComm1.Settings = "9600,E,7,2"
MSComm1.Handshaking = 0
MSComm1.InputLen = 0 '设置和返回input每次读出的字节数,设为0时读出接收缓冲区中的内容
MSComm1.OutBufferCount = 0 '设置和返回发送缓冲区的字节数,设为0时清空发送缓冲区
MSComm1.InBufferCount = 0 '设置和返回接收缓冲区的字节数,设为0时清空接收缓冲区
MSComm1.PortOpen = True
Exit Sub
err1:
MsgBox Err.Description
End Sub
Function FCS(a$) As String '校验计算程序(异或)
b% = 0
i% = Len(a$)
For i = 1 To i%
b% = b% Xor Asc(Mid(a$, i, 1))
Next
ff$ = Hex$(b%)
If Len(ff$) = 1 Then
ff$ = "0" + ff$
End If
FCS = ff$ + "*"
End Function
Private Sub Form_Resize()
Me.Height = 4000
Me.Width = 6000
Me.Left = 0.5 * (MDIForm1.Width - Me.Width)
Me.Top = 0.3 * (MDIForm1.Height - Me.Height)
End Sub
Private Sub MSComm1_OnComm()
Dim getdata As String 'getdata:读输入缓存区
Dim zhuangX As Boolean, zhuangX1 As Boolean 'zhuangX:干旱判断,zhuangX1:严重干旱判断
Dim zhuangY As Boolean, zhuangY1 As Boolean 'zhuangY:喷判断,zhuangY1:滴判断
If MSComm1.CommEvent = comEvReceive Then 'CommEvent的属性返回的值为comEvReceive时是发生了接收事件.
getdata = MSComm1.Input
If ONOFF = True And DATA = False Then '因操作按钮而产生的OnComm时,清除按钮操作标志与按钮操作判断并退出此事件,不做接收数据处理
ONOFF = False
k = 0
Timer1.Enabled = True
Exit Sub
End If
'读数据时返回格式为:"@" + "00" + "RR"+"00"+"四位数据"+FCS+"*"+CHR(13)
'@:固定格式,"00":站号,"00":正确处理时的结束码,FCS:校验和,"*"+CHR(13):固定格式
getdata = Mid(getdata, 11, 1) '因为我们要读的是继电器区的后两位状态,所以取四位数据的最后一位
DATA = False
Select Case j
Case 0 '读000.00与000.01的状态时
If Val(getdata) Mod 2 = 1 Then '除2的余数如果等于1,说明000.00闭合(8421---000.00为最后一位,只有它闭合getdata才为奇数)
zhuangX = True
Else
zhuangX = False
End If
If Val(getdata) Mod 4 >= 2 Then '除4的余数如果大于或等于2,说明000.01闭合(8421---000.01为倒数第二位,只有它闭合getdata除4的余数才会大于或等于2)
zhuangX1 = True
Else
zhuangX1 = False
End If
If zhuangX = False And zhuangX1 = False Then '如果000.00与000.01都不闭合,则显示湿润
Text1.Text = "湿润"
Else
If zhuangX = True Then '如果000.00闭合,则显示干旱
Text1.Text = "干旱"
Else
Text1.Text = "严重干旱" '如果000.01闭合,则显示严重干旱
End If
End If
Case 1 '读10.00与10.01的状态
If Val(getdata) Mod 2 = 1 Then '除2的余数如果等于1,说明10.00闭合(8421---10.00为最后一位,只有它闭合getdata才为奇数)
zhuangY = True
Else
zhuangY = False
End If
If Val(getdata) Mod 4 >= 2 Then '除4的余数如果大于或等于2,说明10.01闭合(8421---10.01为倒数第二位,只有它闭合getdata除4的余数才会大于或等于2)
zhuangY1 = True
Else
zhuangY1 = False
End If
If zhuangY = False And zhuangY1 = False Then '如果000.00与000.01都不闭合,则显示为空,喷嘴没有工作
Text2.Text = ""
Else
If zhuangY = True Then '如果10.00闭合,则显示喷
Text2.Text = "滴"
Else
Text2.Text = "喷" '如果10.01闭合,则显示滴
End If
End If
Case 2
If Val(getdata) Mod 2 = 1 Then '除2的余数如果等于1,说明200.00闭合(8421---200.00为最后一位,只有它闭合getdata才为奇数)
Text3.Text = "自动"
Else
Text3.Text = "手动"
End If
j = -1
End Select
j = j + 1 '进入一个读操作
Timer1.Enabled = True
End If
'
End Sub
Private Sub Timer1_Timer()
If Text3.Text = "自动" Then '如果目前为自动状态,手动“喷”“滴”控制按钮失效
Command1.Enabled = False
Command2.Enabled = False
Else
Command1.Enabled = True
Command2.Enabled = True
End If
If ONOFF = True Then
MSComm1.RThreshold = 11 '设定产生OnComm事件的字符数
'读命令返回格式为:"@" + "00" + "KS/KR"+"00"+FCS+"*"+CHR(13):正好11个字符,所以设定为返回11个字符后产生OnComm事件
'@:固定格式,"00":站号,"KS/KR":根据置/复位命令返回相对应的字符,"00":正确处理时的结束码,FCS:校验和,"*"+CHR(13):固定格式
Select Case k
Case 1
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata3 + FCS(sendata3) + Chr(13) '强制200.00置位
Case 2
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata4 + FCS(sendata4) + Chr(13) '强制200.00复位
Case 3
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata5 + FCS(sendata5) + Chr(13) '201.00置位,201.01复位
Case 4
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata6 + FCS(sendata6) + Chr(13) '201.01置位,201.00复位
End Select
Exit Sub
End If
DATA = True
MSComm1.RThreshold = 15 '设定产生OnComm事件的字符数
'读命令返回格式为:"@" + "00" + "RR"+"00"+"四位数据"+FCS+"*"+CHR(13):正好15个字符,所以设定为返回15个字符后产生OnComm事件
'@:固定格式,"00":站号,"00":正确处理时的结束码,FCS:校验和,"*"+CHR(13):固定格式
Select Case j
Case 0
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata + FCS(sendata) + Chr(13) '读000.00与000.01的状态
Case 1
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata1 + FCS(sendata1) + Chr(13) '读10.00与10.01的状态
Case 2
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.Output = sendata2 + FCS(sendata2) + Chr(13) '读200.00与200.01的状态
End Select
Timer1.Enabled = False '关闭定时器事件等待返回数据处理结束
End Sub
用控件,很简单吧,只要线不接错应该没什么问题 楼上的兄弟,你这个东西是很好,就是很贵哦。。。呵呵 其实串口通信这个东西,一理通百理明,只要搞懂了相应的通信协议,只需要你去实际调试,更多得需要你去体会。 没那么简单
哦?还有什么经验,交流下?? http://download.csdn.net/source/2822882
补充:VB , 基础类