我想将Linux驱动程序与Python结合使用,并且正在转换C程序。
我的C程序运行正常,但在Python中出现错误:IOError:[Errno 14]错误的地址
我正在使用:
Python 2.7.13(默认,2018年6月11日,22:51:25)[Linux2上的GCC 7.2.0]
我的操作系统是Petalinux。
这是C程序
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define DUMMY_IOC_MAGIC 'V'
#define DUMMY_START_TX_CYCLIC _IO(DUMMY_IOC_MAGIC, 0)
#define DUMMY_START_TX _IO(DUMMY_IOC_MAGIC, 1)
int main()
{
int fd, j, ret;
int32_t *map_vptr;
size_t number_of_samples = 64;
size_t size_sample = 4;
//////////////////////////////////////////////////////////////////////////////
printf("Init tx.\n");
fd = open("/dev/playback", O_RDWR | O_SYNC);
if (fd < 0) {
printf("Error opening device\n");
return -1;
}
else{
printf("Device is open...\n");
}
//////////////////////////////////////////////////////////////////////////////
map_vptr = (int32_t*) mmap(0, size_sample * number_of_samples, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map_vptr == MAP_FAILED) {
close(fd);
printf("Error mmapping the file\n");
return -1;
}
printf("Mapped.\n");
//////////////////////////////////////////////////////////////////////////////
int32_t *dst_ptr = map_vptr;
int32_t counter = 0;
for (j = 0; j < number_of_samples; ++j) {
dst_ptr[j] = counter;
counter = counter + 2;
}
//////////////////////////////////////////////////////////////////////////////
// start TX transactions
uint32_t tx_total_len = size_sample * number_of_samples;
ret = ioctl(fd, DUMMY_START_TX, &tx_total_len);
if (ret < 0)
{
printf("ioctl() failed, errno=%d\n", errno);
return -1;
}
printf("End tx.\n");
return 0;
}
这是Python程序
import mmap, os, fcntl
#define DUMMY_START_TX _IO('V', 1)
DUMMY_IOC_MAGIC = 'V'
DUMMY_START_TX = ord(DUMMY_IOC_MAGIC) << (4*2) | 1
NUMBER_OF_SAMPLES = 64;
SIZE_SAMPLE = 4
################################################################################
# Open device
print("Init tx.");
fd = os.open("/dev/playback", os.O_RDWR | os.O_SYNC)
if (fd < 0):
print("Error opening device")
exit(-1);
else:
print("Device is open...")
################################################################################
map_vptr = mmap.mmap(length = SIZE_SAMPLE*NUMBER_OF_SAMPLES, prot = mmap.PROT_READ | mmap.PROT_WRITE,
flags = mmap.MAP_SHARED, fileno = fd, offset = 0)
print("Mapped.");
################################################################################
for i in range(0,NUMBER_OF_SAMPLES):
map_vptr.write_byte(chr(i))
################################################################################
# start TX transactions
tx_total_len = SIZE_SAMPLE * NUMBER_OF_SAMPLES
ret = fcntl.ioctl(fd, DUMMY_START_TX, tx_total_len);
fd.close()
print("End tx.");
输出为:
Init tx.
Device is open...
Mapped.
Traceback (most recent call last):
File "tx_server.py", line 28, in <module>
ret = fcntl.ioctl(fd, DUMMY_START_TX, tx_total_len);
IOError: [Errno 14] Bad address
fcntl.fcntl(fd, op[, arg])
参数
arg
是可选的,默认为整数值0
。如果存在,则可以是整数值或字符串。如果参数丢失或为整数值,则此函数的返回值为Cfcntl()
调用的整数返回值。 当参数是字符串时,它代表二进制结构,例如由struct.pack()
创建。将二进制数据复制到缓冲区,该缓冲区的地址传递到Cfcntl()
调用。成功调用后的返回值是缓冲区的内容,转换为字符串对象。
以上对fcntl.ioctl()
也有效。在您的情况下,而不是直接传递整数,您应该使用struct.pack()
传递值,如下所示:
struct.pack()