当前位置: 首页 > article >正文

TwinCAT3-Udp点对点自由协议通信

目录

1、添加库文件

2、功能块封装

3、程序变量定义

定义全局变量1

定义全局变量2

定义局部变量

4、程序代码

5、通信测试

6、接收到的消息在PLC操作系统弹窗显示

7、程序工程下载连接


1、添加库文件

2、功能块封装

3、程序变量定义

定义全局变量1

//Udp
	LOCAL_HOST_IP									: STRING(15) 	:= '192.168.1.100';	(*本机的IP*)
	LOCAL_HOST_PORT									: UDINT			:= 1001;			(*本机的Port*)
	REMOTE_HOST_IP									: STRING(15) 	:= '192.168.1.104';	(*对方的IP*)
	REMOTE_HOST_PORT								: UDINT 		:= 1001;			(*对方的Port*)
	
	Keep_dg_UdpScan:								UINT:=10;								//1次10毫秒,10次就是100毫秒

定义全局变量2

    //Udp-系统变量
	g_sTcIpConnSvrAddr					:  T_AmsNetId := '';
	bLogDebugMessages					: BOOL := TRUE;
	(* Some project specific error codes *)
	PLCPRJ_ERROR_SENDFIFO_OVERFLOW		: UDINT := 16#8103;
	PLCPRJ_ERROR_RECFIFO_OVERFLOW		: UDINT := 16#8104;
	
	//Udp
	sg_Send:							STRING:='@0,0#';
	sg_Recv:							STRING:='@0,0,0#';

定义局部变量

PROGRAM Udp
VAR
		
	fbSocketCloseAll 					: FB_SocketCloseAll;
    bCloseAll 							: BOOL := TRUE;

	fbPeerToPeer						: FB_PeerToPeer;
	sendFifo							: FB_Fifo;
	receiveFifo							: FB_Fifo;
	sendToEntry							: ST_FifoEntry;
	entryReceivedFrom					: ST_FifoEntry;
	tmp									: STRING;

	bSendOnceToItself					: BOOL;			//给自己发送一次
	bSendOnceToRemote					: BOOL;			//Udp目标机器发送一次
	times:								UINT:=0;		//
	
END_VAR

4、程序代码


//设置Udp自动发送间隔时间
IF times >= Keep_dg_UdpScan THEN
	times:=0;
	bSendOnceToRemote:=NOT bSendOnceToRemote;
END_IF
times:=times+1;

//复位,关闭所有连接
IF bCloseAll THEN (*On PLC reset or program download close all old connections *)
	bCloseAll := FALSE;
	fbSocketCloseAll( sSrvNetId:= g_sTcIpConnSvrAddr, bExecute:= TRUE, tTimeout:= T#10s );
ELSE
	fbSocketCloseAll( bExecute:= FALSE );
END_IF

IF NOT fbSocketCloseAll.bBusy AND NOT fbSocketCloseAll.bError THEN

	//Udp目标机器发送一次
	IF bSendOnceToRemote THEN
		bSendOnceToRemote 				:= FALSE;								(* clear flag *)
		sendToEntry.nRemotePort 		:= REMOTE_HOST_PORT;					(* remote host port number*)
		sendToEntry.sRemoteHost 		:= REMOTE_HOST_IP;						(* remote host IP address *)
		sendToEntry.msg 				:= sg_Send;								(* message text*);
		sendFifo.AddTail( new := sendToEntry );									(* add new entry to the send queue*)
		IF NOT sendFifo.bOk THEN												(* check for fifo overflow*)
			LogError( 'Send fifo overflow!', PLCPRJ_ERROR_SENDFIFO_OVERFLOW );
		END_IF
	END_IF

	//给自己发送一次
	IF bSendOnceToItself THEN
		bSendOnceToItself 				:= FALSE;								(* clear flag *)
		sendToEntry.nRemotePort 		:= LOCAL_HOST_PORT;						(* nRemotePort == nLocalPort => send it to itself *)
		sendToEntry.sRemoteHost 		:= LOCAL_HOST_IP;						(* sRemoteHost == sLocalHost =>send it to itself *)
		sendToEntry.msg 				:= 'Hello itself!';						(* message text*);
		sendFifo.AddTail( new := sendToEntry );									(* add new entry to the send queue*)
		IF NOT sendFifo.bOk THEN												(* check for fifo overflow*)
			LogError( 'Send fifo overflow!', PLCPRJ_ERROR_SENDFIFO_OVERFLOW );
		END_IF
	END_IF

	(* send and receive messages *)
	fbPeerToPeer( sendFifo := sendFifo, receiveFifo := receiveFifo, sLocalHost := LOCAL_HOST_IP, nLocalPort := LOCAL_HOST_PORT, bEnable := TRUE );
	//fbPeerToPeer( sendFifo := sendFifo, receiveFifo := receiveFifo, sLocalHost := REMOTE_HOST_IP, nLocalPort := REMOTE_HOST_PORT, bEnable := TRUE );
	
	(* remove all received messages  from receive queue *)
	REPEAT
		receiveFifo.RemoveHead( old => entryReceivedFrom );
		IF receiveFifo.bOk THEN
			//接收到的消息在PLC操作系统弹窗显示
			//tmp := CONCAT( 'RECEIVED from: ', entryReceivedFrom.sRemoteHost  );
			//tmp := CONCAT( tmp, ', Port: ' );
			//tmp := CONCAT( tmp, UDINT_TO_STRING( entryReceivedFrom.nRemotePort ) );
			//tmp := CONCAT( tmp, ', msg: %s' );
			//ADSLOGSTR(  ADSLOG_MSGTYPE_HINT OR ADSLOG_MSGTYPE_MSGBOX, tmp, entryReceivedFrom.msg );
		END_IF
	UNTIL NOT receiveFifo.bOk
	END_REPEAT

END_IF

//获取Udp接收到的消息
(*
IF fbPeerToPeer.receiveFifo.old.msg<>'' THEN
	sg_Recv:=entryReceivedFrom.msg;
END_IF
fbPeerToPeer.receiveFifo.old.msg:='';
*)

sg_Recv:=entryReceivedFrom.msg;






5、通信测试

6、接收到的消息在PLC操作系统弹窗显示

该段代码功能为,将接收到的消息在PLC操作系统里弹框显示。

要想不弹窗显示,去掉该段代码即可不再弹窗显示

7、程序工程下载连接

https://download.csdn.net/download/panjinliang066333/90542329


http://www.kler.cn/a/613736.html

相关文章:

  • 自动驾驶---打造自动驾驶系统之参考线平滑(四)
  • gitee 常用指令
  • springboot的跨域是什么?遇到跨域问题如何解决?
  • 华宇TAS应用中间件与晓窗科技智慧校园管理一体化平台完成兼容互认证
  • linux ACL权限控制之用户权限控制程序设计
  • 使用Python和OpenCV进行指纹识别与验证
  • Resume全栈项目(二)(.React+Ts)
  • 【漫话机器学习系列】166.向量(Vectors)
  • ubuntu 创建新用户
  • Flink Credit-based机制解析
  • 数字化攻防战场的进化论:红蓝对抗训练如何重塑网络安全范式
  • PTA 7-16 一元多项式求导
  • Axure项目实战:智慧城市APP(六)市民互动(动态面板、显示与隐藏)
  • 【Linux-驱动开发-模块的加载和卸载】
  • 【深度学习】训练集、测试集、验证集、过拟合、欠拟合详解
  • WebRTC简介及应用
  • 【Git】--- Git远程操作 标签管理
  • Anaconda Jupyter 默认启动位置修改
  • javaWeb Router
  • Python面试题库-持续更新中