<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>远程设备 | 通过 Python 连接到Kvaser CANlib开发包</title>
	<atom:link href="https://www.kvaser.cn/developer_category/remote-devices/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.kvaser.cn/developer_category/remote-devices/</link>
	<description>先进的CAN总线解决方案专家</description>
	<lastBuildDate>Thu, 20 Jun 2019 10:12:44 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.kvaser.cn/wp-content/uploads/2024/06/favicon-3.ico</url>
	<title>远程设备 | 通过 Python 连接到Kvaser CANlib开发包</title>
	<link>https://www.kvaser.cn/developer_category/remote-devices/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>通过Python访问Kvaser设备面向对象程度更高的方法（3/3）</title>
		<link>https://www.kvaser.cn/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Tue, 14 Apr 2015 10:05:56 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7186</guid>

					<description><![CDATA[<p>在测试期间，我们的注意力常常放在特定设备上，而不是以通道为中心的CANlib。</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/">通过Python访问Kvaser设备面向对象程度更高的方法（3/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="wpb-content-wrapper"><div class="vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><em>本文是通过 Python 使用 Kvaser 产品三篇系列文章的最后一篇：</em></p>
<ol>
<li><em><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/?p=7083"><em>通过 Python 访问 Kvaser CANlib</em></a></span><br />
</em></li>
<li><em><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/?p=7185">通过 Python 连接到远程设备</a></span><br />
</em></li>
<li><em>通过 Python 访问 Kvaser 设备的面向对象程度更高的方法</em></li>
</ol>
<p>以前我们已经见识到如何在 Python 中使用 canlib.py 和 kvrlib.py 包装器（目前为v2.7）。然而，这些主要只是“包装器”，并没有考虑到使用面向对象语言（Python）的灵活性（对于像C＃这样的其它包装器也是如此）。</p>
<p>在测试期间，我们的注意力常常放在特定设备上，而不是以通道为中心的 CANlib。当您处理远程设备时更是如此，如 Kvaser BlackBird v2 和 Ethercan，在远程连接时会启动新的 CANlib 通道。此时通过新的 Python 模块 kvDevice 来处理。</p>
<p>要打开特定设备，请提供 EAN 和序列号，如下所示。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">import os
import sys
sys.path.append(&quot;C:/temp/Canlib_SDK_v5.9/Samples/Python&quot;)

import kvDevice

device = kvDevice.kvDevice(ean=&quot;73-30130-00778-9&quot;, serial=1023)
print &quot;\n&quot;, device
</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>如果有匹配该EAN和序列号的设备，我们会得到以下输出：</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-clike">Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 3</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>如果我们不指定序列号，我们将获得与EAN匹配的第一个设备。也可以直接指定CANlib通道。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">device = kvDevice.kvDevice(ean=&quot;73-30130-00778-9&quot;)
print &quot;\n&quot;, device

device = kvDevice.kvDevice(ch=6)
print &quot;\n&quot;, device</code></pre><pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-clike">Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 2

Device: Kvaser Memorator Pro 5xHS (channel 4)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1e
Canlib channel: 6
</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>我们举最后一个例子，如果没有找到指定的设备，我们仍然会收到一个返回的设备，但是不会有任何相关信息。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">devices = kvDevice.kvDevice.allDevices()
print &quot;List all %d devices&quot; % (len(devices))
for dev in devices:
print &quot;\n&quot;, dev

print &quot;\n---\nTry and find a non-existing device.&quot;
device = kvDevice.kvDevice(ean=&quot;73-30130-00778-9&quot;, serial=100)
print &quot;\n&quot;, device</code></pre><pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-clike">List all 2 devices

Device: Kvaser Eagle (channel 0)
EAN           : 73-30130-00567-9
S/N           : 71
FW            : v2.5.261
Card          : 0
Drv           : kcany0a
Canlib channel: 0

Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 2
---
Try and find a non-existing device.

Device: None
EAN           : 73-30130-00778-9
S/N           : 100
FW            : None
Card          : None
Drv           : None
Canlib channel: None
</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>如果您查看模块 <code>kvDevice.py</code>，就会注意到，大多数属性存储在以下划线（<code>_driver</code>, <code>_channel</code>等）开头的名称中。这预示着这些值是已使用过的已知值。 （在 Python 中，开头用下划线通常是一个弱的“内部使用”指标）。也就是说，设备现在可以被删除或者重新插入，这样会重新编号，但是设备最后一次是通过 <code>kvDevice.py </code>打开，会发现那些值在被使用。 （该设备位于 CANlib 通道 <code>_channel</code>上，驱动程序名 <code>_driver</code>.）</p>
<p>希望本概述——通过Python用来帮助管理Kvaser设备，将有助于您在开发人员角色中获得更高的效率。</p>
<p>2015-05-22编辑：申明当前的包装器是为Python v2.7所编写。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h3>若有任何评论，请直接通过support@kvaser.com与我们联系。</h3>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/">通过Python访问Kvaser设备面向对象程度更高的方法（3/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>通过Python连接到远程设备（2/3）</title>
		<link>https://www.kvaser.cn/developer-blog/using-python-connect-remote-device-2-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Wed, 08 Apr 2015 10:04:50 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7185</guid>

					<description><![CDATA[<p>Last time I mentioned that kvrlib also has a Python wrapper included in the Kvaser CANlib SDK and I thought we could take a quick look at this as well. We will try some discovery and configuration...</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/using-python-connect-remote-device-2-3/">通过Python连接到远程设备（2/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="wpb-content-wrapper"><div class="vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><em>本文是通过Python使用Kvaser产品三篇系列文章的第二篇：</em></p>
<ol>
<li><em><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/accessing-kvaser-canlib-python-1-3/"><em>通过Python访问Kvaser CANlib</em></a></span><br />
</em></li>
<li><em>通过Python连接到远程设备</em></li>
<li><span style="text-decoration: underline;"><em><a href="http://www.kvaser.cn/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/">通过Python访问Kvaser设备的面向对象程度更高的方法</a></em></span></li>
</ol>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>上次我提到，在Kvaser CANlib SDK中，kvrlib还有一个Python v2.7包装器，我想我们可以快速查看下。 kvrlib库用于处理远程设备，如Blackbird和Ethercan设备。我们将在以下两个示例中尝试一些发现和配置。</p>
<p>首先让我们使用发现功能扫描并连接到远程设备。我们的远程设备序列号16，已经连接到与我们的计算机相同的网络：</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">import sys
sys.path.append(&quot;C:/temp/Canlib_SDK_v5.9/Samples/Python&quot;)

import kvrlib

kl = kvrlib.kvrlib()
print &quot;kvrlib version: %s&quot; % kl.getVersion()

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

delay_ms = 100
timeout_ms = 1000
discovery.setScanTime(delay_ms, timeout_ms)
print &quot;Scanning devices...\n&quot;
deviceInfos = discovery.getResults()
#print &quot;Scanning result:\n%s&quot; % deviceInfos                
for deviceInfo in deviceInfos:
    if deviceInfo.ser_no == serialNo:
        deviceInfo.connect()
        print &#039;Connecting to the following device:&#039;
        print &#039;---------------------------------------------&#039;
        print deviceInfo
        discovery.storeDevices(deviceInfos)
        break;
discovery.close()
kl.unload()</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>在调用脚本之前，您应该设置好envar PYTHONPATH，而不是附加到<code>sys.path</code>（如第2行所示）。还要注意，打包结果变量时，Python包装器通常是非常有用的。这意味着您可以取消对第13行和第22行的注释，并获得可读输出。</p>
<p>以上代码输出如下：</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-clike">kvrlib version: 8.9
Connecting to device with serial number 16
Found 4 addresses.
Scanning devices...

Connecting to the following device:
---------------------------------------------
name/hostname  : &quot;Kvaser Remote #16&quot; / &quot;kv-07130-000016&quot;
  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</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>现在让我们在设备上作少许配置，为什么不更改设备名称？远程设备现在已经在CANlib通道0（第6行）上打开，我们将设备名称设置为“MrKvaser”（第7行）。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">import sys
sys.path.append(&quot;C:/temp/Canlib_SDK_v5.9/Samples/Python&quot;)

import kvrlib

canChannel = 0            
deviceName = &quot;MrKvaser&quot;   

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

kl.unload()</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>我们从第12行开始以读/写模式打开配置，然后读入第13行的现有配置。在第14行输出整个XML配置后，我们在将其写回到第18行的设备之前设置设备名称元素。</p>
<p>以下内容将被输出到面板：</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-clike">Configurating device on channel 0.
Current device configuration:
&lt;?xml version=&quot;1.0&quot; ?&gt;&lt;KVASER&gt;
&lt;VERSION&gt;4.3&lt;/VERSION&gt;
&lt;PRODUCTFAMILY&gt;ethercan&lt;/PRODUCTFAMILY&gt;
&lt;DEVICE_ACCESS level=&quot;PUBLIC&quot; password=&quot;&quot;/&gt;
&lt;CONFIG_PROTECTION password=&quot;&quot; protected=&quot;NO&quot;/&gt;
&lt;NETWORK address_type=&quot;DHCP&quot; device_name=&quot;Kvaser Remote #16&quot; host_name=&quot;kv-07130-000016&quot; mtu=&quot;1492&quot; port=&quot;11416&quot;/&gt;
&lt;PROFILE_INFO name=&quot;Unnamed Profile&quot;/&gt;
&lt;TRAFFIC_ENCRYPTION key=&quot;&quot; type=&quot;NONE&quot;/&gt;
&lt;WEBSERVER change_log_settings=&quot;NO&quot; debug_commands=&quot;NO&quot; enabled=&quot;NO&quot; user_data_download=&quot;NO&quot;/&gt;
&lt;/KVASER&gt;
Device name was &#039;Kvaser Remote #16&#039;
Setting new device name &#039;MrKvaser&#039;</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>我们从设备获取的XML配置可以通过使用内置Python XML支持进行操作，如第14行和第17行所示。</p>
<p>有关配置过程XML完整说明，请在<a title="download section" href="http://www.kvaser.cn/support/downloads/">下载页面</a>查看<em>“Kvaser远程设备配置XML格式规范”</em>文档。</p>
<p>还有，我们真的需要“知道”设备需要在CANlib通道0（第6行）上打开吗？让我们在下一篇文章中解决这个问题。</p>
<p>2015-05-22编辑：申明当前的包装器是为Python v2.7所编写。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h3>若有任何评论，请直接通过support@kvaser.com与我们联系。</h3>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/using-python-connect-remote-device-2-3/">通过Python连接到远程设备（2/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>通过Python访问Kvaser CANlib 软件开发包（1/3）</title>
		<link>https://www.kvaser.cn/developer-blog/accessing-kvaser-canlib-python-1-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Wed, 01 Apr 2015 16:50:19 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7083</guid>

					<description><![CDATA[<p>There have been Python wrappers for CANlib included in the Kvaser CANlib SDK for a while now, but recently they have gotten an overhaul and more libraries and functions are wrapped as we speak.</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/accessing-kvaser-canlib-python-1-3/">通过Python访问Kvaser CANlib 软件开发包（1/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="wpb-content-wrapper"><div class="vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element vc_custom_1493881370730" >
		<div class="wpb_wrapper">
			<p><em>本文是通过Python使用Kvaser产品三篇系列文章的第一篇：</em></p>
<ol>
<li><em>通过Python访问Kvaser CANlib</em></li>
<li><span style="text-decoration: underline;"><em><a href="http://www.kvaser.cn/developer-blog/using-python-connect-remote-device-2-3/">通过Python连接到远程设备</a></em></span></li>
<li><span style="text-decoration: underline;"><em><a href="http://www.kvaser.cn/developer-blog/object-oriented-approach-accessing-kvaser-device-python-3-3/">通过Python访问Kvaser设备的面向对象程度更高的方法</a></em></span></li>
</ol>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>你知道你可以从 Python 访问 Kvaser CANlib 吗？现在 Kvaser CANlib SDK 中包含的 CANlib 已经支持Python 包装器一段时间了，但是我们最近对其进行了全面改造，如我们这里要提到的，程式库更丰富以及功能更多。目前，包装器仅在 Python v2.7 中进行了测试，不兼容 Python v3。</p>
<p>SDK 最新版见<a title="www.kvaser.cn/support/downloads/" href="http://www.kvaser.cn/downloads/">www.kvaser.cn/downloads/</a> （当前版本为<a title="Download CANlib SDK v5.9" href="http://www.kvaser.cn/software/7330130980150/V5_9_0/canlib.exe">v 5.9</a> 发布日期为2015-03-17）。如果您打算直接编辑示例，请确保将SDK安装到具有写入权限的位置（默认位置 C:\Program Files (x86)\Kvaser\Canlib\ 并不是最理想的保存位置。，如下所示，我选择了C:\temp\.的一个子目录）</p>
<p>下载的 Python 示例目录现在包含 CANlib 和 kvrlib 二者的包装器。要了解如何使用它们，请参阅每个包装器文件的底部。例如：如下所示的 canlib.py。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">import sys
sys.path.append(&quot;C:/temp/Canlib_SDK_v5.9/Samples/Python&quot;)

import canlib

cl = canlib.canlib()
print &quot;canlib version: %s&quot; % cl.getVersion()

channel = 0
handle1 = cl.openChannel(channel, canlib.canOPEN_ACCEPT_VIRTUAL)
print &quot;Using channel: %s, EAN: %s&quot; % (handle1.getChannelData_Name(),
handle1.getChannelData_EAN())

handle1.setBusOutputControl(canlib.canDRIVER_NORMAL)
handle1.setBusParams(canlib.canBITRATE_1M)
handle1.busOn()</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>运行上述代码输出结果为：<br />
<code><br />
canlib version: 8.9<br />
Using channel: Kvaser Ethercan Light HS (Remote) (channel 0), EAN: 73-30130-00713-0</code></p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>CANlib的老用户都知道，输出的canlib.dll版本号（8.9）与CANlib软件包（v5.9）的版本号不同。</p>
<p>添加变量<code>sys.path</code>（如第2行所示）是通过Python可以访问模块的一种方法，更普遍的方法是在调用脚本之前设置环境变量<code>PYTHONPATH</code>。</p>
<p>Python包装器使用<a title="ctypes home page" href="http://www.python.net/crew/theller/ctypes/">ctypes</a>与Kvaser CANlib进行对接。这是Python的外部函数库，它提供可兼容C语言的数据类型，并允许在DLL或共享库中调用函数。这意味着也可以在Linux上使用这些示例（但是由于kvrlib尚未移植到Linux，因此目前仅限于CANlib）。</p>
<p>在下一篇文章中，我们会了解如何使用Python连接到远程设备。</p>
<p>2015-05-22编辑：申明当前的包装器是为Python v2.7所编写。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h3>若有任何评论，请直接通过support@kvaser.com与我们联系。</h3>

		</div>
	</div>
</div></div></div></div><div class="vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper"></div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/accessing-kvaser-canlib-python-1-3/">通过Python访问Kvaser CANlib 软件开发包（1/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
