Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade to Python3 - C data types

Tags:

c

python-3.x

I am just trying to move a program from Python2.7 to Python3.5. As I already asked at Python3 C Library usage I see that the call of C libraries is not providing the same results like on Python2.7. My impression is now that the definition of C data types is not working anymore so that the initialization of objects in the C library maybe fails. I don't know a possibility to check if the C library call is working fine. Please have a look on the following code that a took from Python 2.7 - do I need to change the code for Python 3.5? All this is running on a 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)
like image 864
Eckhard M Avatar asked Sep 14 '26 20:09

Eckhard M


1 Answers

"the call of C libraries is not providing the same results like on Python2.7."

Could you please provide the results you are getting using Py 3.5?

Try using this import:

 from ctypes import *

And check this link: https://docs.python.org/3/library/ctypes.html

like image 134
Jesus Fung Avatar answered Sep 17 '26 11:09

Jesus Fung