我使用套接字库从UDP接收数据。
我发送的数据有32位宽(4字节),
我的代码:
with open(csvf, 'w', newline='', encoding='ascii') as csv_handle:
csv_writer = csv.writer(csv_handle, delimiter=',')
while(True):
try:
data,addr = s.recvfrom(buf)
csv_writer.writerow(data)
except (socket.timeout,KeyboardInterrupt, SystemExit):
raise
except Exception:
traceback.print_exc()
但在CSV文件中它提供了8位数据。
例如:
我在Wireshark中收到了这个UDP数据:
所需的CSV文件输出:
4,4,8,12,16等
实际CSV文件输出:
0,0,0,4,0,0,0,4,0,0,0,8
32位无符号Int,来自内存(在FPGA上)我读取u32值,这些值创建缓冲区,我通过lwIP堆栈发送
u32 data;
...
data = XIo_In32(XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+ptr);
现在,我看到第二个问题,litte / big endian也有问题,在Python中我也需要byteswap操作
答案 0 :(得分:0)
像这样:
from binascii import hexlify
def hex_to_val(msg,ndx=0):
""" msg = All or part of the packet.
ndx = tuple or integer
Tuple(start,end)
Integer is only starting index
Output is LIST
"""
tmp = ""
if type(ndx) == tuple : tmp = hexlify(msg[ndx[0]:ndx[1]])
else: tmp = hexlify(msg[ndx:])
return [int(tmp[i*4:(i*4)+4],16)for i in range(len(tmp)/4)]
print hex_to_val("\x00\x05\x00\x05\x00\x05\x00\x05",(0,-2))
>>>
[5, 5, 5]
>>>