s7-1200支持标准tcp协议,可以作为客户端和服务器与其他设备或软件进行tcp通信。
通过调用tsend_c、trcv_c、tcon等指令可以与通信伙伴建立tcp的连接。在pc上
使用网络调试助手与s7-1200建立tcp通信是一种简便的调试方法。
鉴于西门子官网对于lad形式的通信已有详细的介绍,本文仅通过scl实现tcp通信的方
式来介绍使用scl编写项目程序。(附:lad实现tcp通信链接。)
https://www.ad.siemens.com.cn/productportal/prods/s7-1200_plc_easy_plus/11-comm/01-ethernet/03-tcp/03-3rd_party.html
pc端在确认与tcp下游设备交互正常以后,项目内新建fb\_tcp底层块调用tsend\_c、trcv\_c指令块。
1.需设置ip与端口号
2.使能会先建立连接 收到反馈后是一直循环通讯状态 断掉使能后连接也会释放
3.#connect.activeestablished: true:客户端 (client) false:服务器 (server)
server时 remoteport:=0
底层块定义输入1.tcon_ip_v4:包括建立指定连接时所需的所有地址参数。使用 tcon_ip_v4 时,可通过调用指令“mb_client”建立连接;
2.tcon_configured:包括所组态连接的地址参数。使用 tcon_configured 时,将使用下载硬件配置后由 cpu 创建的已有连接。
本文tsend_c、trcv_c指令connect脚使用 tcon_ip_v4 数据类型参数。包含有ip、设备id、硬件标识符以及设备端口号等等。底层程序编写开放上述各参数,方便后续调用。
#connect_instance.id := #id;
#connect_instance.interfaceid := #interfaceid;
#connect_instance.localport := #localport;
if #activeestablished then
#connect_instance.remoteport := #remoteport;
else
#connect_instance.remoteport := 0;
end_if;
#connect_instance.connectiontype := 16#0b;
#connect_instance.activeestablished := #activeestablished;
#connect_instance.remoteaddress.addr[1] := #"addr[1]";
#connect_instance.remoteaddress.addr[2] := #"addr[2]";
#connect_instance.remoteaddress.addr[3] := #"addr[3]";
#connect_instance.remoteaddress.addr[4] := #"addr[4]";
根据 tsend_c、trcv_c指令各个引脚对需要开放出来的各个参数在底层块作出申明。
底层块其他变量申明for #innumbertemp := 1 to len(#strsenddata) by 1 do
#arrsenddatatemp[#innumbertemp - 1] := #strsenddata#innumbertemp;
end_for;
#r_trig_instance(clk:=#bsendup);
if #bconnectdone then
#tsend_instance(req := #bsendup,
id := #connect_instance.id,
len := int_to_udint(len(#strsenddata) + 1),
data := #arrsenddatatemp);
if #r_trig_instance.q then
#bsenddone := false;
end_if;
end_if;
if #tsend_instance.done then
#bsenddone := true;
end_if;
if #tsend_instance.busy or #tsend_instance.error then
#bsenddone := false;
end_if;
#trcv_c_instance(en_r := true,
cont := not #bdisconnect and not #bdisconnect_instance,
len := 0, //优化的块地址为0,非优化为最大长度
adhoc := true,
connect:=#connect_instance,
data := #arrreceivedatat_instance);
if #trcv_c_instance.done then
#strreceivedata := '';
for #innumbertemp := 0 to udint_to_int(#trcv_c_instance.rcvd_len) - 1 by 1 do
#strreceivedata := concat(in1 := #strreceivedata, in2 := #arrreceivedatat_instance[#innumbertemp]);
end_for;
#breceivedone := true;
end_if;
if #trcv_c_instance.error then
#breceivedone := false;
end_if;
if #tsend_instance.error or #trcv_c_instance.error then
#berror := true;
if #tsend_instance.error then
#werrorid := #tsend_instance.status;
end_if;
if #trcv_c_instance.error then
#bdisconnect_instance := true;
#werrorid := #trcv_c_instance.status;
end_if;
else
#berror := false;
#werrorid := 0;
end_if;
if not #trcv_c_instance.error then
#bdisconnect_instance := false;
end_if;
if #tsend_instance.status = 16#7004 or #tsend_instance.status = 16#7005
or #trcv_c_instance.status = 16#7004 or #trcv_c_instance.status = 16#7006 then
#bconnectdone := true;
else
#bconnectdone := false;
end_if;
底层块fb_tcp软体结束。
新建调用块fb_tcp_invocation,如下所示“对号入座”即可以
.`
#fb_tcpinstance(interfaceid:=#interfaceid,
id:=#id,
activeestablished:=#activeestablished,
"addr[1]":=#"addr[1]",
"addr[2]":=#"addr[2]",
"addr[3]":=#"addr[3]",
"addr[4]":=#"addr[4]",
remoteport:=#remoteport,
localport:=#localport,
bsenddone:=#status.bsenddone,
breceivedone:=#status.breceivedone,
strreceivedata:=#status.strreceivedata,
bconnectdone=>#status.bconnectdone,
berror=>#status.berror,
werrorid=>#status.werrorid);
调用样例如上图,建立对应变量后即可完成多个设备的tcp通信调用。
希望可以帮助到你。
感谢看到这里,谢谢