【发布时间】:2020-06-08 21:12:28
【问题描述】:
由于this,我需要使用python memoryview.cast('I') 来访问FPGA,避免双重读/写频闪。不用惊慌,你不需要 FPGA 来回答下面的问题......
所以这里出现了一个失败的 python 示例('testfile' 可以是这里的任何文件 - 仅超过 20 个字节 - 但对我来说,最终它将是一个 IO 映射的 FPGA HW):
#!/usr/bin/python
import struct
import mmap
with open('testfile', "r+b") as f:
mm=memoryview(mmap.mmap(f.fileno(), 20)).cast('I')
# now try to assign the 2 first U32 of the file new values 1 and 2
# mm[0]=1; mm[1]=2 would work, but the following fails:
mm[0:1]=memoryview(struct.pack('II',1,2)).cast('I') #assignement error
错误是:
./test.py
Traceback (most recent call last):
File "./test.py", line 8, in <module>
mm[0:1]=memoryview(struct.pack('II',1,2)).cast('I')
ValueError: memoryview assignment: lvalue and rvalue have different structures
我不明白这个错误......我们在谈论什么“不同的结构”??
如何重写赋值表达式的右侧以使其正常工作? 对于 FPGA,更改左侧将失败...因为似乎其他任何东西都会对硬件产生错误的信号...
更一般地说,我应该如何修改我的 32 位整数数组以适应分配的左侧...?
【问题讨论】:
-
切片不包含正确的端点。你需要
mm[0:2]。 -
当然!!!,真丢人!我经常在 matlab 和 python 之间交换...谢谢@Monica
标签: python memoryview