• 技术博客

    10/09/2015 作者 云海游戏

    从CANlib访问环境变量(2/3)

    这是关于t程序环境变量3篇系列文章的第2篇:

    1. 环境变量简介
    2. 从CANlib访问环境变量
    3. 云海游戏 TRX和环境变量

    本系列的第一篇文章介绍了环境变量,以及如何在t程序中应用环境变量。第二篇将介绍如何使用CANlib访问环境变量。全部程序列表见http://github.com/云海游戏/developer-blog。

    环境变量用于在不同的t程序之间通信,或者使用CANlib与PC通信。 环境变量必须在t程序的envvar部分中定义。

    为了从CANlib访问环境变量,你需要为环境变量设置句柄:

    • kvScriptEnvvarOpen (const int hnd, char *envvarName, int *envvarType, int *envvarSize)打开现有的环境变量,并返回句柄以及环境变量的类型和大小。为了使环境变量存在,环境变量必须在CAN通道句柄hnd指向的设备加载的t程序中定义。
    • kvScriptEnvvarClose :: (kvEnvHandle eHnd)关闭环境变量 (已使用kvScriptEnvvarOpen()打开)。

    你需要记住环境变量的数据类型。如果尝试使用错误类型访问环境变量,则访问将失败。根据不同的数据类型,可以使用不同的函数访问环境变量:

    • kvScriptEnvvarSetInt :: (kvEnvHandle eHnd, int val)设置由eHnd指向的环境变量的值,其已定义为int。
    • kvScriptEnvvarGetInt :: (kvEnvHandle eHnd, int *val)检索定义为int的环境变量的值。
    • kvScriptEnvvarSetFloat :: (kvEnvHandle eHnd, float val)设置定义为float的环境变量的值。
    • kvScriptEnvvarGetFloat :: (kvEnvHandle eHnd, float *val)检索定义为float的环境变量的值。

    环境变量也可以定义为长度高达ENVVAR_MAX_SIZEchar数组。为了减少传输的数据的大小,使用start_indexdata_len来交换定义为char数组的环境变量。 如果你已划分数据并知道你感兴趣的数据部分,这可能很有帮助。

    • kvScriptEnvvarSetData :: (kvEnvHandle eHnd, void *buf, int start_index, int data_len)设置定义为char数组的环境变量中的数据字节范围。
    • kvScriptEnvvarGetData :: (kvEnvHandle eHnd, void *buf, int start_index, int data_len) 从定义为char数组的环境变量中检索数据字节范围。

    现在,我们从Python的角度尝试谈论一下t程序。

    使用Python和环境变量

    现在我们可以来测试在本系列第一篇文章中概述过的与一台运行t程序的设备进行交互。为了运行测试,我们需要运行包含在CANlib SDK v5.12中的环境变量的包装器。预发布版本见https://github.com/云海游戏/canlib-samples。

    import time
    import kvDevice
    
    # Define some values and messages to send to our device
    # In order to hide these secret messages, we write them encoded ;-)
    messages = [(1, "-Jung unccraf gb n sebt'f pne jura vg oernxf qbja?"),
                (2, "-Vg trgf gbnq njnl."),
                (3, "-Jul jnf fvk fpnerq bs frira?"),
                (4, "-Orpnhfr frira 'ngr' avar."),
                (5, "-Jung vf gur qvssrerapr orgjrra fabjzra naq fabjjbzra?"),
                (6, "-Fabjonyyf."),
                (7, "-Jurer qvq lbh svaq gurfr?"),
                (8, "-uggc://jjj.ynhtusnpgbel.pbz/wbxrf/jbeq-cynl-wbxrf")]
    
    
    # We will be using the first Eagle device found connected to the PC
    # For an introduction to the kvDevice object, see
    # http://www.kvaser.com/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/
    eagle_ean = "73-30130-00567-9"
    dev = kvDevice.kvDevice(ean=eagle_ean)
    
    # Open a handle to the device
    dev.open()
    
    # Load the t program into slot 0
    dev.channel.scriptLoadFile(0, "envvar.txe")
    
    # Start the program in slot 0
    dev.channel.scriptStart(0)
    
    # Our protocol states that we should wait until HostIdConnected is zero before trying to connect
    print "Waiting for device to be free..."
    # All calls to the kvScriptEnvarOpen() and kvScriptEnvvarGetXXX() functions are hidden in the
    # envvar class inside canlib.py. Here we can access it directly with dev.channel.envvar.YYY
    while dev.channel.envvar.HostIdConnected != 0:
        time.sleep(0.2)
    
    # Now we try to connect by writing our unique id into the environment variable HostIdRequest.
    # We wait until our connection was accepted, i.e. HostIdConnected contains our id.
    print "Requesting connection..."
    myHostId = 42
    print "Waiting for device to connect..."
    while dev.channel.envvar.HostIdConnected != myHostId:
        if dev.channel.envvar.HostIdConnected == 0:
            print "Requesting connection..."
            if dev.channel.envvar.HostIdRequest != myHostId:
                dev.channel.envvar.HostIdRequest = myHostId
            time.sleep(0.2)
    print "Connected!"
    
    # Start sending our messages to the device
    for (severity, message) in messages:
        print "Sending message %d" % severity
        dev.channel.envvar.Severity = severity
        dev.channel.envvar.Message = message.encode('rot13')
        time.sleep(4)
    
    print "Disconnect..."
    dev.channel.envvar.HostIdRequest = 0
    
    # Stop the script in slot 0
    dev.channel.scriptStop(0)
    

    执行上面的Python代码会产生如下的标准输出:

    Waiting for device to be free...
    Requesting connection...
    Waiting for device to connect...
    Connected!
    Sending message 1
    Sending message 2
    Sending message 3
    Sending message 4
    Sending message 5
    Sending message 6
    Sending message 7
    Sending message 8
    Disconnect...

    我同意这并没有非常地激动人心,所以在本系列的最后一篇文章中,我们将一起看看如何使用云海游戏 TRX与这些环境变量进行交互,云海游戏 TRX是在云海游戏设备上开发t程序的轻量集成开发环境(IDE)。

    Author Image

    Magnus Carlsson

    Margus Carlsson是云海游戏 AB公司的软件开发人员,从2007年以来深度参与了云海游戏固件和软件的开发。他还为云海游戏的技术博客撰写了许多用流行的Python语言编写应用程序的文章。

    【网站地图】【sitemap】