如何在 TwinCAT 3 中将字节数组转换为各种 L'OREAL(double) 值
How to convert byte array into various LREAL(double) values in TwinCAT3
我现在正在尝试将我原来的 python 套接字读取代码转换为 TwinCAT3 中的结构文本。
以下是将 read_data = socket.recv(1116) 数据解压缩为不同内容的原始 python 代码:
def ur_read(read_data):
moment=[0,0,0,0,0,0]
joint_p=[0,0,0,0,0,0]
joint_v=[0,0,0,0,0,0]
tcp_p=[0,0,0,0,0,0]
tcp_v=[0,0,0,0,0,0]
tcp_force=[0,0,0,0,0,0]
state=0
time_new=0
try:
for i in range(len(read_data)):
if [read_data[i],read_data[i+1],read_data[i+2],read_data[i+3]] == [0,0,4,92]:
time = struct.unpack('>d', read_data[i+4:i+12])
moment[0], = struct.unpack('>d', read_data[i+204:i+212])
moment[1], = struct.unpack('>d', read_data[i+212:i+220])
moment[2], = struct.unpack('>d', read_data[i+220:i+228])
moment[3], = struct.unpack('>d', read_data[i+228:i+236])
moment[4], = struct.unpack('>d', read_data[i+236:i+244])
moment[5], = struct.unpack('>d', read_data[i+244:i+252])
joint_p[0], = struct.unpack('>d', read_data[i+252:i+260])
joint_p[1], = struct.unpack('>d', read_data[i+260:i+268])
joint_p[2], = struct.unpack('>d', read_data[i+268:i+276])
joint_p[3], = struct.unpack('>d', read_data[i+276:i+284])
joint_p[4], = struct.unpack('>d', read_data[i+284:i+292])
joint_p[5], = struct.unpack('>d', read_data[i+292:i+300])
joint_v[0], = struct.unpack('>d', read_data[i+300:i+308])
joint_v[1], = struct.unpack('>d', read_data[i+308:i+316])
joint_v[2], = struct.unpack('>d', read_data[i+316:i+324])
joint_v[3], = struct.unpack('>d', read_data[i+324:i+332])
joint_v[4], = struct.unpack('>d', read_data[i+332:i+340])
joint_v[5], = struct.unpack('>d', read_data[i+340:i+348])
tcp_p[0], = struct.unpack('>d', read_data[i+444:i+452])
tcp_p[1], = struct.unpack('>d', read_data[i+452:i+460])
tcp_p[2], = struct.unpack('>d', read_data[i+460:i+468])
tcp_p[3], = struct.unpack('>d', read_data[i+468:i+476])
tcp_p[4], = struct.unpack('>d', read_data[i+476:i+484])
tcp_p[5], = struct.unpack('>d', read_data[i+484:i+492])
tcp_v[0], = struct.unpack('>d', read_data[i+492:i+500])
tcp_v[1], = struct.unpack('>d', read_data[i+500:i+508])
tcp_v[2], = struct.unpack('>d', read_data[i+508:i+516])
tcp_v[3], = struct.unpack('>d', read_data[i+516:i+524])
tcp_v[4], = struct.unpack('>d', read_data[i+524:i+532])
tcp_v[5], = struct.unpack('>d', read_data[i+532:i+540])
tcp_force[0], = struct.unpack('>d', read_data[i+540:i+548])
tcp_force[1], = struct.unpack('>d', read_data[i+548:i+556])
tcp_force[2], = struct.unpack('>d', read_data[i+556:i+564])
tcp_force[3], = struct.unpack('>d', read_data[i+564:i+572])
tcp_force[4], = struct.unpack('>d', read_data[i+572:i+580])
tcp_force[5], = struct.unpack('>d', read_data[i+580:i+588])
state,= struct.unpack('>d', read_data[i+1052:i+1060])
time_new, = struct.unpack('>d', read_data[i+4:i+12])
break
except:
print('exp')
根据python代码,我在TwinCAT3中写了套接字连接和读取功能块。但是,我现在在进行数据编码时遇到了一些问题:
FUNCTION_BLOCK DataEncoding
VAR_INPUT
DataReceived: ARRAY[0..1115] OF BYTE;
END_VAR
VAR_OUTPUT
moment: ARRAY[0..5] OF LREAL := [6(0.0)]; // LREAL stands for Double
joint_pos: ARRAY[0..5] OF LREAL := [6(0.0)];
joint_vel: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_pos: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_vel: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_force: ARRAY[0..5] OF LREAL := [6(0.0)];
END_VAR
VAR
i: INT;
BYTEARR_to_STRING: BYTEARR_TO_MAXSTRING;
END_VAR
正文部分:
FOR i := 0 TO 1116 BY 1 DO
IF DataReceived[i] = 0 AND DataReceived[i+1] = 0 AND DataReceived[i+2] = 4 AND
DataReceived[i+3] = 92
moment[0] := TO_LREAL(BYTEARR_to_STRING(in:= DataReceived[(i+204)..(i+212)]));
END_IF
END_FOR
我遇到的问题是我不知道如何写字节数组和LREAL之间的转换。此外,DataReceived[(i+204)..(i+212)] 的数组索引似乎有一些错误。谁能教我如何索引 ARRAY[0..1115] 中的特定部分(204 到 212)?还有字节数组和LREAL的转换。
谢谢!
您可以将字节 memcpy 到 REAL。
ADR()是获取变量内存地址的函数:
FOR i := 0 TO 1116 BY 1 DO
IF DataReceived[i] = 0 AND DataReceived[i+1] = 0 AND DataReceived[i+2] = 4 AND DataReceived[i+3] = 92 THEN
memcpy(ADR(moment[0]), ADR(DataReceived[(i+204)]), 212-204);
END_IF
END_FOR
我现在正在尝试将我原来的 python 套接字读取代码转换为 TwinCAT3 中的结构文本。 以下是将 read_data = socket.recv(1116) 数据解压缩为不同内容的原始 python 代码:
def ur_read(read_data):
moment=[0,0,0,0,0,0]
joint_p=[0,0,0,0,0,0]
joint_v=[0,0,0,0,0,0]
tcp_p=[0,0,0,0,0,0]
tcp_v=[0,0,0,0,0,0]
tcp_force=[0,0,0,0,0,0]
state=0
time_new=0
try:
for i in range(len(read_data)):
if [read_data[i],read_data[i+1],read_data[i+2],read_data[i+3]] == [0,0,4,92]:
time = struct.unpack('>d', read_data[i+4:i+12])
moment[0], = struct.unpack('>d', read_data[i+204:i+212])
moment[1], = struct.unpack('>d', read_data[i+212:i+220])
moment[2], = struct.unpack('>d', read_data[i+220:i+228])
moment[3], = struct.unpack('>d', read_data[i+228:i+236])
moment[4], = struct.unpack('>d', read_data[i+236:i+244])
moment[5], = struct.unpack('>d', read_data[i+244:i+252])
joint_p[0], = struct.unpack('>d', read_data[i+252:i+260])
joint_p[1], = struct.unpack('>d', read_data[i+260:i+268])
joint_p[2], = struct.unpack('>d', read_data[i+268:i+276])
joint_p[3], = struct.unpack('>d', read_data[i+276:i+284])
joint_p[4], = struct.unpack('>d', read_data[i+284:i+292])
joint_p[5], = struct.unpack('>d', read_data[i+292:i+300])
joint_v[0], = struct.unpack('>d', read_data[i+300:i+308])
joint_v[1], = struct.unpack('>d', read_data[i+308:i+316])
joint_v[2], = struct.unpack('>d', read_data[i+316:i+324])
joint_v[3], = struct.unpack('>d', read_data[i+324:i+332])
joint_v[4], = struct.unpack('>d', read_data[i+332:i+340])
joint_v[5], = struct.unpack('>d', read_data[i+340:i+348])
tcp_p[0], = struct.unpack('>d', read_data[i+444:i+452])
tcp_p[1], = struct.unpack('>d', read_data[i+452:i+460])
tcp_p[2], = struct.unpack('>d', read_data[i+460:i+468])
tcp_p[3], = struct.unpack('>d', read_data[i+468:i+476])
tcp_p[4], = struct.unpack('>d', read_data[i+476:i+484])
tcp_p[5], = struct.unpack('>d', read_data[i+484:i+492])
tcp_v[0], = struct.unpack('>d', read_data[i+492:i+500])
tcp_v[1], = struct.unpack('>d', read_data[i+500:i+508])
tcp_v[2], = struct.unpack('>d', read_data[i+508:i+516])
tcp_v[3], = struct.unpack('>d', read_data[i+516:i+524])
tcp_v[4], = struct.unpack('>d', read_data[i+524:i+532])
tcp_v[5], = struct.unpack('>d', read_data[i+532:i+540])
tcp_force[0], = struct.unpack('>d', read_data[i+540:i+548])
tcp_force[1], = struct.unpack('>d', read_data[i+548:i+556])
tcp_force[2], = struct.unpack('>d', read_data[i+556:i+564])
tcp_force[3], = struct.unpack('>d', read_data[i+564:i+572])
tcp_force[4], = struct.unpack('>d', read_data[i+572:i+580])
tcp_force[5], = struct.unpack('>d', read_data[i+580:i+588])
state,= struct.unpack('>d', read_data[i+1052:i+1060])
time_new, = struct.unpack('>d', read_data[i+4:i+12])
break
except:
print('exp')
根据python代码,我在TwinCAT3中写了套接字连接和读取功能块。但是,我现在在进行数据编码时遇到了一些问题:
FUNCTION_BLOCK DataEncoding
VAR_INPUT
DataReceived: ARRAY[0..1115] OF BYTE;
END_VAR
VAR_OUTPUT
moment: ARRAY[0..5] OF LREAL := [6(0.0)]; // LREAL stands for Double
joint_pos: ARRAY[0..5] OF LREAL := [6(0.0)];
joint_vel: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_pos: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_vel: ARRAY[0..5] OF LREAL := [6(0.0)];
tcp_force: ARRAY[0..5] OF LREAL := [6(0.0)];
END_VAR
VAR
i: INT;
BYTEARR_to_STRING: BYTEARR_TO_MAXSTRING;
END_VAR
正文部分:
FOR i := 0 TO 1116 BY 1 DO
IF DataReceived[i] = 0 AND DataReceived[i+1] = 0 AND DataReceived[i+2] = 4 AND
DataReceived[i+3] = 92
moment[0] := TO_LREAL(BYTEARR_to_STRING(in:= DataReceived[(i+204)..(i+212)]));
END_IF
END_FOR
我遇到的问题是我不知道如何写字节数组和LREAL之间的转换。此外,DataReceived[(i+204)..(i+212)] 的数组索引似乎有一些错误。谁能教我如何索引 ARRAY[0..1115] 中的特定部分(204 到 212)?还有字节数组和LREAL的转换。
谢谢!
您可以将字节 memcpy 到 REAL。
ADR()是获取变量内存地址的函数:
FOR i := 0 TO 1116 BY 1 DO
IF DataReceived[i] = 0 AND DataReceived[i+1] = 0 AND DataReceived[i+2] = 4 AND DataReceived[i+3] = 92 THEN
memcpy(ADR(moment[0]), ADR(DataReceived[(i+204)]), 212-204);
END_IF
END_FOR