V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
blacklinux
V2EX  ›  Python

同一个 DLL 用 C 调用其中一个函数和用 PYTHON 调用同一个函数,返回的结果不同

  •  
  •   blacklinux · 2016-12-06 10:40:50 +08:00 · 2757 次点击
    这是一个创建于 2704 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python 代码如下

    import ctypes
    import time
    
    dll = ctypes.cdll.LoadLibrary('thinkgear')#cdll different windll
    dllVersion = dll.TG_GetDriverVersion();
    print(dllVersion)
    connectionId = dll.TG_GetNewConnectionId();
    print(connectionId)
    errCode = dll.TG_SetStreamLog(connectionId, "streamLog2.txt" );
    #print(errCode)
    errCode = dll.TG_SetDataLog(connectionId,"dataLog2.txt" );
    
    comPortName = ctypes.c_char_p()
    comPortName = "\\\\.\\COM5";
    errCode = dll.TG_Connect(connectionId,comPortName,9600,0);
    print errCode,'connection susser'
    
    packersRead = 0
    
    while 1:
        errCode = dll.TG_ReadPackets(connectionId,1)
        if errCode ==1 and dll.TG_GetValueStatus(connectionId,2)!=0:
            print dll.TG_GetValue(connectionId, 5),dll.TG_GetValue(connectionId, 6),dll.TG_GetValue(connectionId, 9)
            packersRead = packersRead + 1
            if packersRead>50:
                break
    
            
    dll.TG_Disconnect(connectionId)
    dll.TG_FreeConnection(connectionId)
    

    C 代码如下

    #include <Windows.h>  
    #include <stdio.h>  
    #include "thinkgear.h"  
      
    void wait()   
    {  
        system("pause");  
    }  
      
    int main()  
    {  
        char *comPortName = NULL;  
        int   dllVersion = 0;  // 动态库版本  
        int   connectionId = 0;  // 连接 ID  
        int   packetsRead = 0;  // 包数量  
        int   errCode = 0;      // 错误码  
      
        /* 获取动态库版本 */  
        dllVersion = TG_GetDriverVersion();  
        printf( "ThinkGear DLL version: %d\n", dllVersion );  
      
        /* 获取连接 ID */  
        connectionId = TG_GetNewConnectionId();  
        if( connectionId < 0 )   
        {  
            printf("ERROR: TG_GetNewConnectionId() returned %d.\n",   
                connectionId );  
            wait();  
            exit( EXIT_FAILURE );  
        }  
      
        /* 原始数据日志 */  
        errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );  
        if( errCode < 0 ) {  
            printf("ERROR: TG_SetStreamLog() returned %d.\n", errCode );  
            wait();  
            exit( EXIT_FAILURE );  
        }  
      
        /* ThinkGear 数据日志 */  
        errCode = TG_SetDataLog( connectionId, "dataLog.txt" );  
        if( errCode < 0 ) {  
            printf("ERROR: TG_SetDataLog() returned %d.\n", errCode );  
            wait();  
            exit( EXIT_FAILURE );  
        }  
      
        /* 准备连接的 COM 口 */  
        comPortName = "\\\\.\\COM5"; // \\.\COM3  
        errCode = TG_Connect( connectionId,   
            comPortName,   
            TG_BAUD_9600,   
            TG_STREAM_PACKETS );  
        if( errCode < 0 ) {  
            printf("ERROR: TG_Connect() returned %d.\n", errCode );  
            wait();  
            exit( EXIT_FAILURE );  
        }  
        
        /* 不停的读取数据 */  
        packetsRead = 0;  
        while(1/* packetsRead < 10*/ )   
        {  
            //Sleep(50);  
            /* 读一个报文 */  
            errCode = TG_ReadPackets( connectionId, 1 );  
      
            /* 如果这个报文读取成功 */  
            if( errCode == 1 )  
            {  
    	    int detla,theta,beta1;  
                if(( errCode = TG_GetValueStatus(connectionId, TG_DATA_ATTENTION)) != 0 )   
                {  
    	      	detla = TG_GetValue(connectionId, TG_DATA_DELTA); 
    		theta = TG_GetValue(connectionId, TG_DATA_THETA); 
    		beta1 = TG_GetValue(connectionId, TG_DATA_BETA1); 
                    printf("delta = %d, theta=%d, beta1=%d\n", detla, theta, beta1);  
                }  
            }   
            else  
            {  
                printf("ReadPackets:errcode=%d\n", errCode);  
                Sleep(1000);  
            }  
      
        }   
      
        /* 释放连接 */  
        TG_FreeConnection( connectionId );  
      
        /* End program */  
        system("pause");  
        return( EXIT_SUCCESS );  
    }  
    
    

    C 要用到的头文件在这https://git.oschina.net/blacklin/codes/74tlbowzdy9fm1h8uq32r16

    我觉得可能是 python 的问题,但是就是不知道问题在哪。。。

    5 条回复    2016-12-07 15:11:00 +08:00
    glasslion
        1
    glasslion  
       2016-12-06 13:00:41 +08:00
    comPortName = ctypes.c_char_p()
    comPortName = "\\\\.\\COM5";
    ---------------------------------------------------
    这一段 comPortName 最后又赋值成了 python 字符串, 而不是 c_char_p
    blacklinux
        2
    blacklinux  
    OP
       2016-12-06 14:45:03 +08:00
    @glasslion 我把这边改成 comPortName = ctypes.c_char_p("\\\\.\\COM5") 后,结果没有改变
    TaMud
        3
    TaMud  
       2016-12-06 18:05:11 +08:00
    最大的可能性是字节编码,不同
    blacklinux
        4
    blacklinux  
    OP
       2016-12-07 14:40:50 +08:00
    @TaMud 您觉得是传入的参数字节码有问题还是返回值的字节编码有问题?
    TaMud
        5
    TaMud  
       2016-12-07 15:11:00 +08:00
    @blacklinux 我这个没有具体分析过案例,你需要自已排查一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2207 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:42 · PVG 23:42 · LAX 08:42 · JFK 11:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.