您现在的位置:机电论文网>> 机床加工>> 正文内容

pc与松下fp系列plc的通信

作者: 来源: 发布时间:2018/2/11 16:12:47  点击数:327
一. 引言
plc作为工业现场的控制器,以其性能稳定,编程简单,结构紧凑,通用性强等优点,已得到了广泛的运用;而计算器在图象处理,报表打印,中文显示等方面具有很强的功能;在现代的控制系统中,已经在很多地方将两者结合起来使用,充分发挥两者的优势,因此,pc和plc之间的通信显的尤为重要,大多数plc的通信协议都采用的是modbus,而松下fp系列采用的是其专用的mewtocol-com协议.本文通过实例详细讲解了利用该协议,达到pc和plc通信的目的.
二. 松下plc的通信协议
松下fp系列plc的通信协议是采用松下公司专用的mewtocol-com协议.在该协议中,数据传送采用的是ascii码形式,首先由计算器发送指令,plc接受到指令后,自动进行相应的响应,计算器根据plc的响应,可以得知plc是否正确执行了刚刚发送的指令,这样就构成了一个交互式的操作,以确保pc 与 plc之间的通信正常. mewtocol-com的指令格式如下:
指令信息:

应答信息(正常时):

应答信息(发生错误时):

其中, “ % ”为起始符号,标记每一帧报文的开始. “ cr ”为结束符号,标记每一帧报文的结束. “ ad ”为每一站plc的地址,用两位十六进制数表示,如 01 则代表第一台plc. “ # ”, “ $ ”, “ ! ”标记该帧报文为何种类型,其分别对应为:指令信息,应答信息(正常),应答信息(错误). “ bcc ”为校验码,为两位十六进制数,其初值为“ 0 ”,然后从起始符开始与该帧报文中每一字节按位进行异或运算得到的.
三. pc端vb程序的编写
pc端的程序我们采用vb6.0 和 mscomm32 控件来编写.vb的通信控件mscomm32提供了简单的串行口通信功能,使用它可以建立与串行口的连接,通过串行口连接到其它设备,发送命令,接受资料,以及监视和响应串行通信中发生的事件和错误等.我们只要通过上位机的程序,向plc按照mewtocol-com协议规定的格式发送命令,plc就可以作出相应的响应.为了使读者能够清楚的了解两者之间的通信,现举例如下:
整个程序的界面如图 (1) 所示.

程序的部分代码如下:
private sub cmd_connect_click()
lbl_state.caption = "正在建立连接,请稍后..."
doevents
mscomm1.portopen = true ‘建立与串口的连接.
mscomm1.inputlen = 1
mscomm1.rthreshold = 1 ‘设置接受缓冲区接受到一个字符就产生oncomm事件.
tempoutputstr = "%" & trim(combo2.text) & "#rt" ‘combo2为plc的站别.
commandstr = "rt" ‘ “rt”命令用来读取plc当前的相关信息.
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr) ‘将传送的内容显示在文本框中
timer1.enabled = true ‘ 系统开始计时,以判断pc和plc连接是否正常.
end if
end sub
private sub cmd_dt_click()
dim i as integer
dim tempwd as string
if option3.value = true then
commandstr = "rd"
for i = 0 to 15
text1(i).text = ""
next i
tempoutputstr = "%" & trim(combo2.text) & "#rd" & "d" & trim(ucase(txt_bof.text)) & trim(ucase(txt_eof.text)) ‘” rd “’命令用来读取dt中的数据.
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
else
commandstr = "wd"
tempoutputstr = "%" & trim(combo2.text) & "#wd" & "d" & trim(ucase(txt_bof.text)) & trim(ucase(txt_eof.text)) ‘” wd “命令用来将数据写入到相应的dt中.
for i = 0 to val(txt_eof.text) - val(txt_bof.text)
tempwd = hex$(val(text1(i).text))
do while len(tempwd) <>; 4
tempwd = "0" & tempwd
loop
tempwd = right$(tempwd, 2) & left$(tempwd, 2)
tempoutputstr = tempoutputstr & tempwd
next i
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
end sub
private sub cmd_run_click() ‘控制plc运行
commandstr = "rm"
rpboolean = true
mscomm1.output = "%" & trim(combo2.text) & "#rmr4a" & chr$(13)
txt_pcplc.text = "%" & trim(combo2.text) & "#rmr4a"
end sub
private sub cmd_stop_click() ‘控制plc停止
commandstr = "rm"
rpboolean = false
mscomm1.output = "%" & trim(combo2.text) & "#rmp48" & chr$(13)
txt_pcplc.text = "%" & trim(combo2.text) & "#rmp48"
end sub
private sub cmd_tc_click()
dim tempws as string
dim tempwk as string
if option9.value = true and option11.value = true then
commandstr = "rs" ‘读取定时器/计数器中的默认值
tempoutputstr = "%" & trim(combo2.text) & "#rs" & trim(ucase(txt_tc.text)) & trim(ucase(txt_tc.text))
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option9.value = true and option12.value = true then
commandstr = "ws" ‘写入定时器/计数器中的默认值
tempoutputstr = "%" & trim(combo2.text) & "#ws" & trim(ucase(txt_tc.text)) & trim(ucase(txt_tc.text))
tempws = hex$(val(text2.text))
do while len(tempws) <>; 4
tempws = "0" & tempws
loop
tempws = right$(tempws, 2) & left$(tempws, 2)
tempoutputstr = tempoutputstr & tempws
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option10.value = true and option11.value = true then
commandstr = "rk" ‘读取定时器/计数器中的经过值
tempoutputstr = "%" & trim(combo2.text) & "#rk" & trim(ucase(txt_tc.text)) & trim(ucase(txt_tc.text))
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option10.value = true and option12.value = true then
commandstr = "wk" ‘写入定时器/计数器中的经过值
tempoutputstr = "%" & trim(combo2.text) & "#wk" & trim(ucase(txt_tc.text)) & trim(ucase(txt_tc.text))
tempwk = hex$(val(text2.text))
do while len(tempwk) <>; 4
tempwk = "0" & tempwk
loop
tempwk = right$(tempwk, 2) & left$(tempwk, 2)
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
end sub
private sub cmd_yr_click()
dim tempyr as string
dim tempvalue as string
dim binvalue as string
dim hexvalue as string
dim i as integer
if option1.value = true then
tempyr = "y"
else
tempyr = "r"
end if
if option5.value = true and option7.value = true then
commandstr = "rcs" ‘读取单个触点的状态
tempoutputstr = "%" & trim(combo2.text) & "#rcs" & tempyr & trim(ucase(txt_yr.text))
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option6.value = true and option7.value = true then
commandstr = "wcs" ‘写入单个触点的状态
if label22.backcolor = &hc0& then
tempvalue = "1"
else
tempvalue = "0"
end if
tempoutputstr = "%" & trim(combo2.text) & "#wcs" & tempyr & trim(ucase(txt_yr.text)) & tempvalue
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option5.value = true and option8.value = true then
commandstr = "rcc" ‘读取字单元触点的状态
tempoutputstr = "%" & trim(combo2.text) & "#rcc" & tempyr & trim(ucase(txt_yr.text)) & trim(ucase(txt_yr.text))
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
if option6.value = true and option8.value = true then
commandstr = "wcc" ‘写入字单元触点的状态
for i = 0 to 15
if label1(i).backcolor = &hc0& then
tempvalue = "1"
else
tempvalue = "0"
end if
binvalue = binvalue & tempvalue
next i
for i = 1 to 16 step 4
tempvalue = bintohex(mid$(binvalue, i, 4))
hexvalue = hexvalue & tempvalue
next i
tempoutputstr = "%" & trim(combo2.text) & "#wcc" & tempyr & trim(ucase(txt_yr.text)) & trim(ucase(txt_yr.text)) & hexvalue
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
txt_pcplc.text = tempoutputstr & calbcc(tempoutputstr)
end if
end sub
private sub mscomm1_oncomm()
dim i as integer
dim temprec as string
dim tempinputsingle as string
dim tempvalue as string
dim binvalue as string
tempinputsingle = mscomm1.input ‘读取接收缓冲区中的内容,并清空缓冲区
tempinput = tempinput + tempinputsingle
if tempinputsingle = chr$(13) then ‘当接收到回车符时,表示一帧内容接受完毕.
if instr(1, tempinput, "!") <>; 0 then
lbl_state.caption = "命令参数错误,请输入正确的参数!"
tempinput = ""
exit sub
else
tempinput = trim(tempinput)
txt_plcpc.text = tempinput ‘将接收到的信息显示在文本框中.
select case commandstr ‘分类对接收到的信息进行处理.
case "rt"
select case mid$(tempinput, 7, 2)
case "20"
lbl_kind.caption = " fpσ系列"
case "03"
lbl_kind.caption = " fp3 系列"
case "02"
lbl_kind.caption = " fp5 系列"
case else
lbl_kind.caption = "未知系列"
end select
lbl_version.caption = mid$(tempinput, 9, 2)
lbl_size.caption = mid$(tempinput, 11, 2) & "k"
if mid$(tempinput, 15, 9) = "000000000" then
lbl_error.caption = "无错误!"
else
lbl_error.caption = "plc 有错误!"
end if
if mid$(tempinput, 14, 1) = "1" then
shape_run.fillcolor = &hc0&
shape_stop.fillcolor = &h80ffff
else
shape_run.fillcolor = &h80ffff
shape_stop.fillcolor = &hc0&
end if
timer1.enabled = false
lbl_state.caption = "连接成功!"
case "rm"
if rpboolean = true then
shape_run.fillcolor = &hc0&
shape_stop.fillcolor = &h80ffff
else
shape_run.fillcolor = &h80ffff
shape_stop.fillcolor = &hc0&
end if
case "rd"
for i = 0 to len(tempinput) - 10 step 4
temprec = mid$(tempinput, 7 + i, 4)
text1(i / 4).text = val("&h" & right(temprec, 2) & left(temprec, 2))
next i
case "rcs"
temprec = mid$(tempinput, 7, 1)
if temprec = "1" then
label22.backcolor = &hc0&
else
label22.backcolor = &hffff&
end if
case "rcc"
temprec = mid$(tempinput, 7, 4)
for i = 1 to 4
tempvalue = hextobin(mid$(temprec, i, 1))
binvalue = binvalue & tempvalue
next i
for i = 0 to 15
if mid$(binvalue, i + 1, 1) = "1" then
label1(i).backcolor = &hc0&
else
label1(i).backcolor = &hffff&
end if
next i
case "rs"
temprec = mid$(tempinput, 7, 4)
text2.text = val("&h" & right(temprec, 2) & left(temprec, 2))
case "rk"
temprec = mid$(tempinput, 7, 4)
text2.text = val("&h" & right(temprec, 2) & left(temprec, 2))
end select
tempinput = ""
end if
end if
end sub
private sub timer1_timer()
trynum = trynum + 1
if trynum = 4 then ‘总共发送三次连接信息,如无响应,则提示错误!
lbl_state.caption = "pc 与 plc 通讯失败,请检查通讯线路是否正常!"
if mscomm1.portopen = true then mscomm1.portopen = false
timer1.enabled = false
trynum = 0
exit sub
end if
mscomm1.output = tempoutputstr & calbcc(tempoutputstr) & chr$(13)
end sub
public function calbcc(byval inputstr$) as string '计算校验码
dim i as integer
dim tempstr as byte
tempstr = 0
inputstr$ = trim(inputstr$)
for i = 1 to len(inputstr$)
tempstr = tempstr xor asc(mid(inputstr$, i, 1))
next i
calbcc = right$("0" & hex$(tempstr), 2)
end function
以上程序在win98+vb6.0中调试通过,串行通信参数须设置为 “9600,o,8,1”.
四. 结论
pc不仅能实现一般人机界面的相关功能,而且还可以处理和统计大量的数据,另外,通过网络,也可以实现远程控制.本文只是拋砖引玉,读者可以根据实际的需要,利用mewtocol-com协议,制作出现场所需要的控制程序.
五. 参考文献
1. 松下电工 mewtocol-com通信协议 2002
2. 松下电工 fpσ系列编程手册 2002
作者单位:广东省东莞市石碣镇台达电子三厂自动化工程部
地址:广东省东莞市 523308
email:

更多
字体:【小】-【中】-【大】【关闭此页】

上一篇:自动记录式起重机力矩限制器的研制'   下一篇:基于can总线的现场设备控制管理系统'


特别声明:机电之家(http://www.jdzj.com )所共享的机电类资料,机电论文、机电类文章、机电企业类管理制度、机电类软件都来自网上收集,其版权归作者本人所有,如果有任何侵犯您权益的地方,请联系我们,我们将马上进行处理。购买的论文都出自原创,保证作者的原创的版权的转让,任何纠纷由法律解决。