倍福 - 使用二进制文件保存配方数据(基于纯代码实现)- 实例使用

(方法代码见上篇文章)

1.读取 - 手动读取/上电自动读取

1).上电自动读取

因为PLC启动后同步需要时间,所以选择上电1S后读取一次,为了确保数据读取必然成功,再3s后再次读取。

stRecipeHandle.rtAlwaysTrue(CLK:= TRUE);
IF stRecipeHandle.rtAlwaysTrue.Q THEN
  	stRecipeHandle.bFirstRead := TRUE;
END_IF

(* ********* Read Parameter Excute ******** *)
stRecipeHandle.tonFirstDelay(IN:= stRecipeHandle.bFirstRead, PT:= T#1S);
stRecipeHandle.tonSecondDelay(IN:= stRecipeHandle.bSecondRead, PT:= T#3S);
stRecipeHandle.rtRead(CLK:= bManualReadRecipe OR stRecipeHandle.tonFirstDelay.Q OR stRecipeHandle.tonSecondDelay.Q);
IF stRecipeHandle.rtRead.Q THEN
  	MEMSET(destAddr:= ADR(stRecipeHandle.bReadParam), fillByte:= 1, n:= SIZEOF(stRecipeHandle.bReadParam));
  	IF stRecipeHandle.bSecondRead THEN
  	  	stRecipeHandle.bSecondRead := FALSE;
  	END_IF
  	IF stRecipeHandle.bFirstRead THEN
  	  	stRecipeHandle.bFirstRead := FALSE;
  	  	stRecipeHandle.bSecondRead := TRUE;
  	END_IF
END_IF


(* stRecipeHandle结构体详情 *)
TYPE ST_RecipeHandle :
STRUCT	
	(* 内部用 *)
	rtRead,rtSave						:	R_TRIG;
	bReadAllFalse,bSaveAllFalse			:	BOOL;
	bSaveParam							:	ARRAY[1..MAX_RECIPE_NUM]	OF	BOOL;
	bReadParam							:	ARRAY[1..MAX_RECIPE_NUM]	OF	BOOL;
	fbReadBinaryFile					:	ARRAY[1..MAX_RECIPE_NUM]	OF 	FB_ReadFmBinaryFile;
	fbSaveBinaryFile					:	ARRAY[1..MAX_RECIPE_NUM] 	OF 	FB_WriteToBinaryFile;
	
	(* 上电第一次自动读取配方用 *)
	rtAlwaysTrue						:	R_TRIG;
	bFirstRead,bSecondRead				:	BOOL;
	tonFirstDelay,tonSecondDelay		:	TON;
		
	(* 记录配方保存和读取时间用 *)
	fActSaveTime,fActReadTime			:	LREAL;
	tonReadParamTime,tonSaveParamTime	:	TON;
END_STRUCT
END_TYPE

2.保存 - 手动保存/自动保存

1).自动保存可以选择比较所有要操作的数据结构体,如果有不同的,触发自动保存,但是由于数据量大的时候,比较消耗的资源比较大,这里不再描述;

这里选择的是手动保存,因为配方的修改多数情况下是人为修改的,所以手动保存也可以满足大部分的工况要求;

(* ********* Save Parameter Excute ******** *)
stRecipeHandle.rtSave(CLK:= bManualSaveRecipe);
IF stRecipeHandle.rtSave.Q THEN
  	MEMSET(destAddr:= ADR(stRecipeHandle.bSaveParam), fillByte:= 1, n:= SIZEOF(stRecipeHandle.bSaveParam));
END_IF

3.实例化

stRecipeHandle.fbSaveBinaryFile[1](sFileName:= 'Param1', pBuff:= ADR(stParam1), nLen:= SIZEOF(stParam1), bExecute:= stRecipeHandle.bSaveParam[1]);
stRecipeHandle.fbReadBinaryFile[1](sFileName:= 'Param1', pBuff:= ADR(stParam1), nLen:= SIZEOF(stParam1), bExecute:= stRecipeHandle.bReadParam[1]);

stRecipeHandle.fbSaveBinaryFile[2](sFileName:= 'Param2', pBuff:= ADR(stParam2), nLen:= SIZEOF(stParam2), bExecute:= stRecipeHandle.bSaveParam[2]);
stRecipeHandle.fbReadBinaryFile[2](sFileName:= 'Param2', pBuff:= ADR(stParam2), nLen:= SIZEOF(stParam2), bExecute:= stRecipeHandle.bReadParam[2]);



stRecipeHandle.bReadAllFalse := stRecipeHandle.bSaveAllFalse := TRUE;
FOR i := 1 TO MAX_RECIPE_NUM BY 1 DO
  	IF stRecipeHandle.bSaveParam[i] AND (NOT stRecipeHandle.fbSaveBinaryFile[i].bBusy OR stRecipeHandle.fbSaveBinaryFile[i].bError) THEN
  	  	stRecipeHandle.bSaveParam[i] := FALSE;
  	END_IF
  	IF stRecipeHandle.bReadParam[i] AND (NOT stRecipeHandle.fbReadBinaryFile[i].bBusy OR stRecipeHandle.fbReadBinaryFile[i].bError) THEN
  	  	stRecipeHandle.bReadParam[i] := FALSE;
  	END_IF
  	stRecipeHandle.bReadAllFalse := NOT stRecipeHandle.bReadParam[i] AND stRecipeHandle.bReadAllFalse;
  	stRecipeHandle.bSaveAllFalse := NOT stRecipeHandle.bSaveParam[i] AND stRecipeHandle.bSaveAllFalse;
END_FOR
IF stRecipeHandle.bSaveAllFalse THEN
  	bManualSaveRecipe := FALSE;
END_IF
IF stRecipeHandle.bReadAllFalse THEN
  	bManualReadRecipe := FALSE;
END_IF

4.结果展示

操作保存功能之后,就可以在选择保存的路径下看到对应的.dat文件

操作读取功能之后,会把保存在.dat内的数据,覆盖到现有参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Awby1228

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值