(方法代码见上篇文章)
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内的数据,覆盖到现有参数