ͨ��Python���ӵ�Զ���豸��2/3��

Magnus Carlsson
Wed, 08 Apr 2015 10:04:50 +0000

Last time I mentioned that kvrlib also has a Python wrapper included in the �ƺ���Ϸ CANlib SDK and I thought we could take a quick look at this as well. We will try some discovery and configuration...

The post ͨ��Python���ӵ�Զ���豸��2/3�� appeared first on �ƺ���Ϸ.

������ͨ��Pythonʹ���ƺ���Ϸ��Ʒ��ƪϵ�����µĵڶ�ƪ��

1. ͨ��Python�����ƺ���Ϸ CANlib

2. ͨ��Python���ӵ�Զ���豸

3. ͨ��Python�����ƺ���Ϸ�豸���������̶ȸ��ߵķ���

�ϴ����ᵽ�����ƺ���Ϸ CANlib SDK�У�kvrlib����һ��Python v2.7��װ�����������ǿ��Կ��ٲ鿴���� kvrlib�����ڴ���Զ���豸����Blackbird��Ethercan�豸�����ǽ�����������ʾ���г���һЩ���ֺ�������

����������ʹ�÷��ֹ���ɨ�貢���ӵ�Զ���豸�����ǵ�Զ���豸���к�16���Ѿ����ӵ������ǵļ������ͬ��������

import sys
sys.path.append("C:/temp/Canlib_SDK_v5.9/Samples/Python")

import kvrlib

kl = kvrlib.kvrlib()
print "kvrlib version: %s" % kl.getVersion()

serialNo = 16
print "Connecting to device with serial number %s" % serialNo
addressList = kvrlib.kvrDiscovery.getDefaultAddresses(kvrlib.kvrAddressTypeFlag_BROADCAST)
print "Found %d addresses." % addressList.count
#print addressList
discovery = kl.discoveryOpen()
discovery.setAddresses(addressList)

delay_ms = 100
timeout_ms = 1000
discovery.setScanTime(delay_ms, timeout_ms)
print "Scanning devices...\n"
deviceInfos = discovery.getResults()
#print "Scanning result:\n%s" % deviceInfos
for deviceInfo in deviceInfos:
    if deviceInfo.ser_no == serialNo:
        deviceInfo.connect()
        print 'Connecting to the following device:'
        print '---------------------------------------------'
        print deviceInfo
        discovery.storeDevices(deviceInfos)
        break;
discovery.close()
kl.unload()

�ڵ��ýű�֮ǰ����Ӧ�����ú�envar PYTHONPATH�������Ǹ��ӵ�sys.path�����2����ʾ������Ҫע�⣬����������ʱ��Python��װ��ͨ���Ƿǳ����õ�������ζ��������ȡ���Ե�13�к͵�22�е�ע�ͣ�����ÿɶ������

���ϴ���������£�

kvrlib version: 8.9
Connecting to device with serial number 16
Found 4 addresses.
Scanning devices...

Connecting to the following device:
---------------------------------------------
name/hostname  : "�ƺ���Ϸ Remote #16" / "kv-07130-000016"
  ean/serial   : 73301-30007130 / 16
  fw           : 2.4.126
  addr/cli/AP  : 10.0.3.32 (IPV4) / 10.0.3.27 (IPV4) / unknown (UNKNOWN)
  usage/access : FREE / PUBLIC
  pass/enc.key : no / no

�������������豸�����������ã�Ϊʲô�������豸������Զ���豸�����Ѿ���CANlibͨ��0����6�У��ϴ򿪣����ǽ��豸��������Ϊ��Mr�ƺ���Ϸ������7�У���

import sys
sys.path.append("C:/temp/Canlib_SDK_v5.9/Samples/Python")

import kvrlib

canChannel = 0
deviceName = "Mr�ƺ���Ϸ"

kl = kvrlib.kvrlib()
print "Configurating device on channel %d." % canChannel
try:
    config = kl.configOpen(channel=canChannel, mode=kvrlib.kvrConfig.MODE_RW)
    config.getXml()
    print "Current device configuration:\n%s" % config.xml.toxml()
    print "Device name was '%s'" % config.xml.getElementsByTagName('NETWORK')[0].attributes['device_name'].value
    print "Setting new device name '%s'" % deviceName
    config.xml.getElementsByTagName('NETWORK')[0].attributes['device_name'].value = deviceName
    config.setXml()
    config.close()
except (kvrlib.kvrError) as ex:
    print ex

kl.unload()

���Ǵӵ�12�п�ʼ�Զ�/дģʽ�����ã�Ȼ������13�е������������ڵ�14���������XML���ú������ڽ���д�ص���18�е��豸֮ǰ�����豸����Ԫ�ء�

�������ݽ�������������

Configurating device on channel 0.
Current device configuration:
<?xml version="1.0" ?><KVASER>
<VERSION>4.3</VERSION>
<PRODUCTFAMILY>ethercan</PRODUCTFAMILY>
<DEVICE_ACCESS level="PUBLIC" password=""/>
<CONFIG_PROTECTION password="" protected="NO"/>
<NETWORK address_type="DHCP" device_name="�ƺ���Ϸ Remote #16" host_name="kv-07130-000016" mtu="1492" port="11416"/>
<PROFILE_INFO name="Unnamed Profile"/>
<TRAFFIC_ENCRYPTION key="" type="NONE"/>
<WEBSERVER change_log_settings="NO" debug_commands="NO" enabled="NO" user_data_download="NO"/>
</KVASER>
Device name was '�ƺ���Ϸ Remote #16'
Setting new device name 'Mr�ƺ���Ϸ'

���Ǵ��豸��ȡ��XML���ÿ���ͨ��ʹ������Python XML֧�ֽ��в��������14�к͵�17����ʾ��

�й����ù���XML����˵������������ҳ��鿴���ƺ���ϷԶ���豸����XML��ʽ�淶���ĵ���

���У����������Ҫ��֪�����豸��Ҫ��CANlibͨ��0����6�У��ϴ�������������һƪ�����н�����������