clockus:ULINT;sendPulse:BOOL;sendDataNormal:DUT_SEND_DATA_Normal;pSource:POINTER TO BYTE;pTarget:POINTER TO BYTE;id_SendBuffer:ARRAY[0..199] OF BYTE;//发送缓冲器。2、在主程序里增加以下语句
GetSystemTime(uliTimeUs=>clockus);//获取系统时间(微秒为单位)sendDataNormal.STAMP:=ULINT_TO_UDINT(clockus);//截取低4字节的值。sendPulse:=NOT(sendPulse);//发送脉冲
//周期计数IF sendPulse THEN sendDataNormal.data1:=sendDataNormal.data1+1; IF UINT_TO_INT( sendDataNormal.data1) >=30000 THEN sendDataNormal.data1:=0; END_IFEND_IF
//双精度格式的周期计数sendDataNormal.data3:=UINT_TO_LREAL(sendDataNormal.data1);
//数据打包到发送缓冲器,直接操作字节数组。pTarget:=ADR(id_SendBuffer);
pSource:=ADR(sendDataNormal.STAMP);FOR i:=0 TO SIZEOF(sendDataNormal.STAMP)-1 BY 1 DO pTarget^:=pSource^; pTarget:=pTarget+1; pSource:=psource+1;END_FORpSource:=ADR(sendDataNormal.data1);FOR i:=0 TO SIZEOF(sendDataNormal.data1)-1 BY 1 DO pTarget^:=pSource^; pTarget:=pTarget+1; pSource:=psource+1;END_FORpSource:=ADR(sendDataNormal.data2);FOR i:=0 TO SIZEOF(sendDataNormal.data2)-1 BY 1 DO pTarget^:=pSource^; pTarget:=pTarget+1; pSource:=psource+1;END_FORpSource:=ADR(sendDataNormal.data3);FOR i:=0 TO SIZEOF(sendDataNormal.data3)-1 BY 1 DO pTarget^:=pSource^; pTarget:=pTarget+1; pSource:=psource+1;END_FOR 三、利用联合类型(Union)进行操作
TYPE union_udint :UNION Value:UDINT; Bytes:ARRAY[0..3] OF BYTE;END_UNIONEND_TYPE
TYPE union_uint :UNION Value:UINT; Bytes:ARRAY[0..1] OF BYTE;END_UNIONEND_TYPE
TYPE union_real :UNION Value:REAL; Bytes:ARRAY[0..3] OF BYTE;END_UNIONEND_TYPE
TYPE union_lreal :UNION Value:LREAL; Bytes:ARRAY[0..7] OF BYTE;END_UNIONEND_TYPE2、定义新的数据结构
TYPE DUT_SEND_DATA:STRUCT STAMP :union_udint;//单位为微秒的时间戳 起始地址:0 data1:union_uint;//UInt类型的数值 data2:union_real;//浮点数类型的数值 data3:union_lreal;//双精度类型的数值END_STRUCTEND_TYPE3、在主程序里新建局部变量
clockus:ULINT;sendPulse:BOOL;sendData:DUT_SEND_DATA;id_SendBuffer:ARRAY[0..199] OF BYTE;//发送缓冲器。pArray:UINT;i:UINT;4、在主程序里增加以下语句
GetSystemTime(uliTimeUs=>clockus);//获取系统时间(微秒为单位)sendPulse:=NOT(sendPulse);//发送脉冲sendData.STAMP.Value:=ULINT_TO_UDINT(clockus);//截取低4字节的值。//周期计数IF sendPulse THEN sendData.data1.Value:=sendData.data1.Value+1; IF UINT_TO_INT( sendData.data1.Value) >=30000 THEN sendData.data1.Value:=0; END_IF sendData.data3.Value:=UINT_TO_LREAL(sendData.data1.Value);END_IF
//数据打包到发送缓冲器,直接操作字节数组。pArray:=0;FOR i:=0 TO SIZEOF(sendData.STAMP.Bytes)-1 BY 1 DO id_SendBuffer[pArray]:=sendData.STAMP.Bytes; pArray:=pArray+1;END_FORFOR i:=0 TO SIZEOF(sendData.data1.Bytes)-1 BY 1 DO id_SendBuffer[pArray]:=sendData.data1.Bytes; pArray:=pArray+1;END_FORFOR i:=0 TO SIZEOF(sendData.data2.Bytes)-1 BY 1 DO id_SendBuffer[pArray]:=sendData.data2.Bytes; pArray:=pArray+1;END_FORFOR i:=0 TO SIZEOF(sendData.data3.Bytes)-1 BY 1 DO id_SendBuffer[pArray]:=sendData.data3.Bytes; pArray:=pArray+1;END_FOR