升级到Python3-C数据类型

埃克哈德·M

我只是想将程序从Python2.7移到Python3.5。正如我在Python3 C库用法中已经问过的那样,我看到C库的调用没有提供与Python2.7相同的结果。我现在的印象是C数据类型的定义不再起作用,因此C库中的对象初始化可能会失败。我不知道检查C库调用是否工作正常的可能性。请查看以下来自Python 2.7的代码-是否需要更改Python 3.5的代码?所有这些都在Raspberry Pi上运行。

import ctypes,array         #ctypes laed die Libs in Python, array (BuildIn) fuer C Arrays

class YasdiMaster:
    def __init__(self,
                 ini_file="./yasdi.ini",
                 yasdiMaster_lib="libyasdimaster.so",
                 iDeviceHandleCount=50,
                 iChannelHandleCount=142,
                 DeviceNameBuffer=30,
                 DeviceTypeBuffer=30,
                 ValText=15,
                 ChannelName=30,
                 cChanUnit=10,
                 status_text_buffer=30):
        self.ini_file = ini_file
        self.yasdiMaster_lib = yasdiMaster_lib
        self.DriverCount = ctypes.c_ulong()
        self.pDriverCount = ctypes.pointer(self.DriverCount)
        self.iDeviceHandleCount = iDeviceHandleCount
        self.DeviceHandles = array.array("L",[0]*self.iDeviceHandleCount)
        self.iChannelHandleCount = iChannelHandleCount
        self.ChannelHandles = array.array("L",[0]*self.iChannelHandleCount)
        self.DeviceNameBuffer = " "*DeviceNameBuffer
        self.DeviceTypeBuffer = " "*DeviceTypeBuffer
        self.SNBuffer = ctypes.c_ulong()
        self.pSNBuffer = ctypes.pointer(self.SNBuffer)
        self.dDevHandle = ctypes.c_ulong()
        self.pdDevHandle = ctypes.pointer(self.dDevHandle)
        self.ChannelName = " "*ChannelName
        self.dblValue = ctypes.c_double(0)
        self.pdblValue = ctypes.pointer(self.dblValue)
        self.ValText = " "*ValText
        self.cChanUnit = " "*cChanUnit
        self.status_text_buffer = " "*status_text_buffer
        self.ChanType = ctypes.c_ushort()
        self.pChanType = ctypes.pointer(self.ChanType)
        self.ChanIndex = ctypes.c_int()
        self.pChanIndex = ctypes.pointer(self.ChanIndex)
        self.range_min = ctypes.c_double()
        self.prange_min = ctypes.pointer(self.range_min)
        self.range_max = ctypes.c_double()
        self.prange_max = ctypes.pointer(self.range_max)

        self.yasdiMaster = ctypes.cdll.LoadLibrary(self.yasdiMaster_lib)

    def yasdiMasterInitialize(self):
        self.yasdiMaster.yasdiMasterInitialize(self.ini_file,self.pDriverCount)
埃克哈德·M

在Python 3中,默认情况下字符串为Unicode。我必须将参数更改为ASCII:

ini_file="./yasdi.ini".encode('ascii')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章