Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 发出「;“地图”;Python2和Python3之间的函数_Python 3.x_Dictionary_Ctypes_Converters - Fatal编程技术网

Python 3.x 发出「;“地图”;Python2和Python3之间的函数

Python 3.x 发出「;“地图”;Python2和Python3之间的函数,python-3.x,dictionary,ctypes,converters,Python 3.x,Dictionary,Ctypes,Converters,我正在使用别人编写的代码从数字/模拟转换器读取和编码数据。我试图通过使用python 3来改进这段代码,因为它是在python 2中编码的 我对代码进行了大量简化,以理解为什么它不能在Python3上工作,并发现“map”函数行为存在问题,因为它指出“在Python3中发生了错误,但在Python2中没有发生错误” 我不知道我哪里做错了,因为我没有自己编写这段代码,有些部分我无法理解。 我应该做些什么改变才能使它与Python3一起工作 DTOL DTOL_原型 DTOL_defs

我正在使用别人编写的代码从数字/模拟转换器读取和编码数据。我试图通过使用python 3来改进这段代码,因为它是在python 2中编码的

我对代码进行了大量简化,以理解为什么它不能在Python3上工作,并发现“map”函数行为存在问题,因为它指出“在Python3中发生了错误,但在Python2中没有发生错误”

我不知道我哪里做错了,因为我没有自己编写这段代码,有些部分我无法理解。 我应该做些什么改变才能使它与Python3一起工作



DTOL



DTOL_原型



DTOL_defs



因此,我发现python 3和python 2之间存在以下差异: Python 3:

c_long(0)
<class 'ctypes.c_long'>
Available Boards:
Name = b'DT9818(00)'
Drivername = b'Dt9818'
1
-----
Initializing: DT9818(00)
Available Boards:
Name = b'DT9818(00)'
Drivername = b'Dt9818'
1
-----
Initializing: DT9818(00)
c_long(0)
c_ulong(0)
Error occured in<WinFunctionType object at 0x00000235269DC528>
c_long(0)
可用板:
名称=b'DT9818(00)'
Drivername=b'Dt9818'
1.
-----
初始化:DT9818(00)
可用板:
名称=b'DT9818(00)'
Drivername=b'Dt9818'
1.
-----
初始化:DT9818(00)
库隆(0)
库隆(0)
中发生错误
Python 2:

c_long(0)
<class 'ctypes.c_long'>
Available Boards:
Name = DT9818(00)
Drivername = Dt9818
1
-----
Initializing: DT9818(00)
Available Boards:
Name = DT9818(00)
Drivername = Dt9818
1
-----
Initializing: DT9818(00)
c_long(0)
c_ulong(0L)
c_long(0)
可用板:
名称=DT9818(00)
Drivername=Dt9818
1.
-----
初始化:DT9818(00)
可用板:
名称=DT9818(00)
Drivername=Dt9818
1.
-----
初始化:DT9818(00)
库隆(0)
库隆(0升)

最后两个参数分别对应于“print(subsystem_code)”和“print(elemNum)”。

我不知道您为什么认为映射会有问题。它的调用方式(用它构建列表)在两个Python版本上都是相同的

张贴

如果没有函数文档,我无法100%确定,但从它们的声明方式来看:

  • 旧版初始化:

    prototype=ctypes.WINFUNCTYPE(ctypes.c_int,ctypes.c_char_p,ctypes.POINTER(ctypes.c_ulong))
    参数标志=(1,“名称”),(2,“hDev”)
    
  • 旧类:

    prototype=ctypes.WINFUNCTYPE(ctypes.c_int、ctypes.c_ulong、ctypes.c_long、ctypes.c_uint、ctypes.POINTER(ctypes.c_ulong))
    参数标志=(1,“hDev”),(1,“子系统类型”),(1,“uiElementNr”),(2,“adhandle”)
    
  • 我可以得出结论,返回值表示函数执行状态,通常为:

    • 0获得成功
    • 表示错误代码的任何其他int值
    但是,在Initialize中,您可以这样设置hDev(它是olDaInitialize的输出,olDaGetDASS的输入)(也存在类型不匹配,但它通过自动强制转换隐藏):

    hdev=olDaInitialize(名称)
    self.hdev=hdev
    
    因此,将olDaInitialize的状态分配给hdev是没有意义的。正确的方法是:

    • 在初始化中:

      self.hdev=ctypes.c_ulong(0)
      
    • 在初始化中:

      hdev=ctypes.c_ulong(0)
      status=olDaInitialize(名称,ctypes.byref(hdev))
      如果status==0:#或任何表示成功的值
      self.hdev=hdev
      
    同样的事情也适用于adhandle(olDaGetDASS的最后一个参数),您在GetSubsystem中引用它(作为sshandle),并且可能在代码中使用其他地方

    当我们在改进部分时:

    • 传递给_init__和Initialize的名称似乎是多余的。我建议将其保留在初始值设定项中,并在正文中执行以下操作:

      self.name=name.encode(“utf-8”)
      
      然后从Initialize中删除参数,并在其中使用
      self.name

    • 您正在使用errcheck功能,但目前它毫无帮助。您可以对此进行改进(例如,同时显示错误代码)。有关的详细信息

    • Initialize被调用两次:在主脚本和setupGetSingleValue中。您应该从一个地方删除调用(我认为是后者)

    • OldaeMumboards(和listboardscallback)丢失。另外,前两行的打印位置在哪里

    • 请尝试保持标识符名称一致()

    @EDIT0

    添加一些简化代码,以检查问题是否重现

    code.py:

    !/usr/bin/env python3
    导入系统
    导入ctypes
    def main():
    oldaapi64=ctypes.CDLL(查找库(“oldaapi64”))
    olmem64=ctypes.CDLL(查找库(“olmem64”))
    oldainitialize=oldaapi64.oldainitialize
    oldainitialize.argtypes=[
    ctypes.c_char_p,ctypes.POINTER(ctypes.c_ulong),
    ]
    oldainitialize.restype=ctypes.c_int
    oldagetdass=olmem64.oldagetdass
    oldagetdass.argtypes=[
    ctypes.c_-ulong、ctypes.c_-long、ctypes.c_-uint、ctypes.POINTER(ctypes.c_-ulong),
    ]
    oldagetdass.restype=ctypes.c_int
    dev=ctypes.c_ulong(0)
    res=oldainitialize(b“DT9818(00)”,ctypes.byref(dev))
    打印({:s}返回{:d}.Dev:{:d}.format(oldainitialize.name、res、Dev.value))
    OLSS_AD=0
    元素数量=0
    handle=ctypes.c_ulong(0)
    res=oldagetdass(dev、OLSS\u AD、element\u num、ctypes.byref(handle))
    打印({:s}返回{:d}.Handle:{:d}.format(oldagetdass.name、res、Handle.value))
    如果名称=“\uuuuu main\uuuuuuuu”:
    打印(“Python{:s}on{:s}\n.”格式(sys.version,sys.platform))
    main()
    打印(“完成”)
    
    始终显示有问题的完整的
    回溯
    。回溯是什么意思?当您收到错误消息时,它以单词
    回溯开始。人们只输入最后一行错误(错误消息),但所有文本都可能很重要。在python 3中,我得到了:可用板:Name=b'DT9818(00)'Drivername=b'Dt9818'1----初始化:Dt9818(00)可用板:Name=b'Dt9818(00)'Drivername=b'Dt9818'1----初始化:Dt9818(00)cālong(0)cāulong(0)Python2中出现错误,第二个板的类型不同:cālong(0)cāulong(0L)(这个分别对应于:print(OLSS_AD)print(type(OLSS_AD))我没有
    
    import ctypes
    from ctypes.util import find_library
    
    dll = ctypes.CDLL(find_library('oldaapi64'))
    dll2 = ctypes.CDLL(find_library('OLMEM64'))
    
    def errcheck_all(ret, func, args):
        if ret:
            print("Error occured in"+ str(func))
            return
    
        return args
    
    def errcheck_none(ret, func, args):
        if ret:
            print("Error occured in"+ str(func))
            print(ret)
            return
    
    # ----------- Initialize ---------------------------------
    prototype = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_char_p, ctypes.POINTER(ctypes.c_ulong))
    paramflags = (1, "name"), (2,"hDev")
    olDaInitialize = prototype(('olDaInitialize', dll), paramflags)
    olDaInitialize.errcheck = errcheck_all
    # -----------END Initialize ---------------------------------
    
    # ----------- olDaGetDASS ---------------------------------
    prototype = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_ulong, ctypes.c_long, ctypes.c_uint, ctypes.POINTER(ctypes.c_ulong))
    paramflags = (1, "hDev"), (1,"SubsystemType"), (1,"uiElementNr"), (2,"adhandle")
    olDaGetDASS = prototype(('olDaGetDASS',dll), paramflags)
    olDaGetDASS.errcheck = errcheck_all
    # ----------- olDaGetDASS ---------------------------------
    
    import ctypes
    
    (OLSS_AD, 
    OLSS_DA, 
    OLSS_DIN, 
    OLSS_DOUT, 
    OLSS_SRL, 
    OLSS_CT) = list(map(ctypes.c_int, (range(6))))
    
    OL_ENC_BINARY = 200
    
    c_long(0)
    <class 'ctypes.c_long'>
    Available Boards:
    Name = b'DT9818(00)'
    Drivername = b'Dt9818'
    1
    -----
    Initializing: DT9818(00)
    Available Boards:
    Name = b'DT9818(00)'
    Drivername = b'Dt9818'
    1
    -----
    Initializing: DT9818(00)
    c_long(0)
    c_ulong(0)
    Error occured in<WinFunctionType object at 0x00000235269DC528>
    
    c_long(0)
    <class 'ctypes.c_long'>
    Available Boards:
    Name = DT9818(00)
    Drivername = Dt9818
    1
    -----
    Initializing: DT9818(00)
    Available Boards:
    Name = DT9818(00)
    Drivername = Dt9818
    1
    -----
    Initializing: DT9818(00)
    c_long(0)
    c_ulong(0L)