【问题标题】:Error converting a C ioctl/mmap call to Python?将 C ioctl/mmap 调用转换为 Python 时出错?
【发布时间】:2020-04-06 18:05:25
【问题描述】:

我想在 Python 中使用 Linux 驱动程序,并且我正在转换我的 C 程序。

我的 C 程序运行良好,但 Python 中出现错误:IOError: [Errno 14] Bad address

我正在使用:

Python 2.7.13(默认,2018 年 6 月 11 日,22:51:25) [GCC 7.2.0] 在 linux2 上

我的操作系统是 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

【问题讨论】:

    标签: python c embedded cpython


    【解决方案1】:

    来自the Python doc

    fcntl.fcntl(fd, op[, arg])
    

    参数arg 是可选的,默认为整数值0。如果存在,它可以是整数值或字符串。如果缺少参数或整数值,此函数的返回值是 C fcntl() 调用的整数返回值。 当参数是字符串时,它表示二进制结构,例如由struct.pack() 创建。二进制数据被复制到一个缓冲区,其地址传递给 C fcntl() 调用。成功调用后的返回值是缓冲区的内容,转换为字符串对象。

    以上内容也适用于fcntl.ioctl()。在您的情况下,您应该使用 struct.pack() 来传递值,而不是直接传递整数,如下所示:

    import struct
    
    # ...
    
    tx_total_len = SIZE_SAMPLE * NUMBER_OF_SAMPLES
    ptr = struct.pack('I', tx_total_len)
    #                  I == unsigned int of 4 bytes (uint32_t)
    
    ret = fcntl.ioctl(fd, DUMMY_START_TX, ptr)
    

    【讨论】:

    • @CarlosAlbertoRuizNaranjo 不客气。如果这解决了您的问题,您可以通过单击左侧的复选标记按钮来接受我的回答,以便您的问题可以标记为已解决。
    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多