[英]Python and UIO devices: Why does mmap.read() work and os.read() fail?
內核版本:4.19
Python版本:3.5.6
平台:Xilinx Ultrascale+ Zynq
我正在開發一些可以讀寫 UIO 設備的 python 代碼。 我找到了一種有效的方法,也找到了一種我無法理解的沒有明顯原因的方法。 我擔心這意味着我錯過了整個方法的某些東西,並且將來會回來咬我。
對於我的初始測試,我正在讀取和寫入 PL 中 5 個 LSB 處於活動狀態的單個寄存器。 這是工作代碼:
>>> import mmap
>>> fid= open('/dev/uio0', 'r+b', 0) # read/write, binary, non-buffered
>>> regs= mmap.mmap(fid.fileno(), 4)
>>> regs.read(4)
b'\x1c\x00\x00\x00'
>>> regs.seek(0)
>>> regs.write(b'\xF3\x00\x00')
>>> regs.seek(0)
>>> regs.read(4)
b'\x13\x00\x00\x00'
這是失敗代碼的兩個示例:
僅使用標准讀取功能失敗
>>> fid= open('/dev/uio0', 'r+b', 0) # read/write, binary, non-buffered
>>> fid.read(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 5] Input/output error
使用 os 也會失敗,但我可以向 mmap 提供文件描述符並且一切正常。
>>> fid= os.open('/dev/uio0', os.O_SYNC | os.O_RDWR)
>>> os.read(fid, 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 5] Input/output error
>>> import mmap
>>> regs= mmap.mmap(fid, 4)
>>> regs.read(4)
b'\x13\x00\x00\x00'
>>> regs.seek(0)
>>> regs.write(b'\x65\x00\x00\x00')
>>> regs.seek(0)
>>> regs.read(4)
b'\x05\x00\x00\x00'
誰能解釋為什么其他兩種方法失敗了? 從我坐的地方看,它們看起來相當。
提前致謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.