<?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>CANlib开发包|Kvaser教程如何设置CANlib以及从CANlib访问环境变量</title>
	<atom:link href="https://www.kvaser.cn/developer_category/canlib-sdk/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.kvaser.cn/developer_category/canlib-sdk/</link>
	<description>先进的CAN总线解决方案专家</description>
	<lastBuildDate>Wed, 18 Dec 2024 10:14:22 +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>CANlib开发包|Kvaser教程如何设置CANlib以及从CANlib访问环境变量</title>
	<link>https://www.kvaser.cn/developer_category/canlib-sdk/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>设置CANlib：事件驱动</title>
		<link>https://www.kvaser.cn/developer-blog/setting-up-canlib-event-driven/</link>
		
		<dc:creator><![CDATA[Troy]]></dc:creator>
		<pubDate>Wed, 30 Sep 2015 16:20:35 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=8663</guid>

					<description><![CDATA[<p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-event-driven/">设置CANlib：事件驱动</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_video_widget wpb_content_element vc_clearfix   vc_video-aspect-ratio-169 vc_video-el-width-100 vc_video-align-left" >
		<div class="wpb_wrapper">
			
			<div class="wpb_video_wrapper"><a href="http://v.youku.com/v_show/id_XMTUzNzMzOTIyMA==">http://v.youku.com/v_show/id_XMTUzNzMzOTIyMA==</a></div>
		</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 class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><em>这是CANlib开发3篇系列文章的最后一篇：</em></p>
<div class="wpb_text_column wpb_content_element " style="color: #646b6e;">
<div class="wpb_wrapper">
<ol>
<li><span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/"><i>在Visual Studio中设置CANlib</i></a></span></li>
<li><span style="color: #0d4187;"><em><a style="color: #0d4187;" href="http://www.kvaser.cn/developer-blog/setting-up-canlib-basic-api-calls/">基础API调用</a></em></span></li>
<li><em>事件驱动应用程序</em></li>
</ol>
</div>
</div>
<h2><strong>CANlib 简介</strong></h2>
<p>在我们的上一个视频中，我们演示了如何创建一个控制台应用程序，它可以使用CANlib SDK中提供的基本API调用来配置和读取CAN总线上的报文。 在本视频中，我们将重点介绍如何使循环事件驱动读取更高效。</p>
<h2><strong>示例</strong></h2>
<p>在前面的示例中，我们使用了Windows C＃控制台应用程序。由于我们想在多线程应用程序中显示事件驱动循环，本示例将使用Windows Presentation Foundation程序，其中主线程处理图形用户界面（GUI）以及后台线程从CAN总线读取数据。</p>
<p>我们有一个简单的图形用户界面，一个按钮启动后台线程和一个按钮停止后台线程。我们还添加了一个文本框以显示接收到的报文。我们使用XAML设计了这个GUI。</p>
<p>我们将使用.NET后台工作线程来执行所有的CAN总线操作。这意味着我们必须包括System ComponentModel命名空间，并在创建主窗口时创建后台工作程序。 GUI所要做的就是在按下总线开启按钮时启动后台工作，当按下总线关闭按钮时停止后台工作，并显示后台线程报告的字符串。</p>
<p>我们的后台工作程序将执行ReceiveMessageLoop函数来从CAN总线中检索数据。 像我们前面的例子，我们需要包括canlibCLSNET命名空间和设置Kvaser设备的句柄。我们在InitializeChannel函数中放置了句柄设置指令。 我们初始化库，获取句柄，设置比特率，并使总线上的通道有效。我们还添加了对canIoCtl的调用，以获取一个Windows事件句柄，当驱动程序中发生某些事件时，它将触发。这可能是接收到的报文，报文传输或CAN控制器状态的改变。无论事件触发的原因是什么，你必须清空接收缓冲区，否则不会再获得另一个事件触发器。你会注意到canIoCtl返回的指针必须放在从WaitHandle类派生的类中。要使用WaitHandle类，我们必须包括System.Threading命名空间。</p>
<p>我们现在有一个正确初始化的通道句柄和一个事件句柄，所以回到ReceiveMessageLoop循环，直到GUI请求后台工作程序停止。在循环内，我们使用WaitAny方法等待1秒钟直到事件发生。 如果没有事件发生，我们再次循环。 如果CAN事件触发，我们使用canRead调用循环读取报文，直到该函数指示接收缓冲区为空且具有canERR_NOMSG状态。对于每个报文的消息，我们使用BackgroundWorker类中内置的ReportProgress方法将报文信息传递回GUI。</p>
<p>如果GUI请求后台工作程序停止，我们将退出while循环，进入ReleaseChannel函数，通过使通道处于非活动状态并关闭句柄来执行我们的标准清理。</p>
<p>现在应用程序仍然响应，并且使用较少的CPU资源，因为我们只检查消息以及当CAN事件或用户事件发生时更新显示。请记住，当使用多线程时，canOpenChannel调用返回的句柄不是线程安全的。因此每个线程必须使用canOpenChannel函数声明自己的物理通道句柄。</p>
<h2 class="p1" style="font-weight: 400; color: #da2d2d;"><strong>CANlib更多信息</strong></h2>
<p class="p1" style="color: #646b6e;">有关CANlib的更多信息，您可以查看我们的<span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/canlib-webhelp/">CANlib SDK帮助</a></span>，并查看我们的<span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/support/developer-blog/">技术博客</a></span>中的系列文章。</p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-event-driven/">设置CANlib：事件驱动</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>设置CANlib：基础API调用</title>
		<link>https://www.kvaser.cn/developer-blog/setting-up-canlib-basic-api-calls/</link>
		
		<dc:creator><![CDATA[Troy]]></dc:creator>
		<pubDate>Tue, 22 Sep 2015 20:48:05 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=8156</guid>

					<description><![CDATA[<p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-basic-api-calls/">设置CANlib：基础API调用</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_video_widget wpb_content_element vc_clearfix   vc_video-aspect-ratio-169 vc_video-el-width-100 vc_video-align-left" >
		<div class="wpb_wrapper">
			
			<div class="wpb_video_wrapper"><a href="http://v.youku.com/v_show/id_XMTUzNDA2OTIwMA==.html">http://v.youku.com/v_show/id_XMTUzNDA2OTIwMA==.html</a></div>
		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><em>这是CANlib开发3篇系列文章的第二篇：</em></p>
<div class="wpb_text_column wpb_content_element ">
<div class="wpb_wrapper">
<ol>
<li><span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/"><i>在Visual Studio中设置CANlib</i></a></span></li>
<li><em>基础API调用</em></li>
<li><span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/developer-blog/setting-up-canlib-event-driven/"><em>事件驱动应用程序</em></a></span></li>
</ol>
</div>
</div>
<h2><strong>简介</strong></h2>
<p><span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/">在上一个视频中</a></span>，我们建立了如何将CANlib SDK资源添加到您自己的Visual Studio项目中。在本视频中，我们将介绍通过Kvaser设备从CAN总线配置和读取报文的最少的CANlib函数调用。我们已经从上一个视频中向示例项目添加了必要的代码。此代码将配置设备或虚拟通道为250 kbps，然后循环，读取总线上的每个CAN帧，直到用户按下退出按钮。</p>
<h2>示例</h2>
<p>为了实现这一点，我们在上一个视频中描述的第一步是使用“using”指令导入canlibCLSNET类型。</p>
<p>我们的第一个CANlib函数调用必须是canInitalizeLibrary。调用此函数会将必要的DLL加载到内存中，并为所有连接的Kvaser硬件分配一个通道号。</p>
<p>我们现在需要使用canOpenChannel函数来处理Kvaser CAN电路。返回的句柄对于对库的后续调用是必要的。第一个参数指定所需的CAN电路。 第二个参数允许附加限定标志。在这种情况下，我们接受虚拟通道的句柄。</p>
<p>现在我们有一个句柄，我们可以为连接的CAN总线配置电路适当的比特率。canSetBusParams函数支持设置每个总线参数值或使用Kvaser的预定义比特率中的某一个。我们使用的预定义比特率为250 kbps</p>
<p>一旦配置了比特率，就可以通过调用canBusOn使电路在CAN总线上激活。 一旦该功能执行，CAN电路正在积极地参与CAN流量。</p>
<p>您可能已经注意到，在每次调用CANlib函数后调用DisplayError。对于更简洁的代码，我们在独立的函数中处理从CANlib返回的错误状态。如果存在错误条件，我们使用canGetErrorText函数将错误代码转换为字符串描述。检查CANlib函数返回的状态对于确认发生问题的位置至关重要。</p>
<p>在Main中，我们已经完成了配置和激活电路，因此我们可以从总线循环读取CAN帧。我们将使用canReadWait来检索CAN帧。CAN帧由标识符（id），0至8个数据字节（数据），数据长度代码（dlc），消息属性（标志）和时间戳（时间）组成。如果接收缓冲区为空，canReadWait中的最终参数将指定CAN帧等待的以毫秒为单位的时间长度。所以，当canReadWait返回时，我们需要检查一些事件，然后才假设我们有一个报文。如果返回的状态值是canOK，我们知道我们有事件要处理。如果返回的状态是canERR_NOMSG，那么我们只需要再次循环。 但是如果返回的状态是任何其他值，则电路的句柄不能再用于读取CAN数据。</p>
<p>再次，为了使代码更容易读取，我们将数据处理代码放在一个单独的函数DisplayMessage中，当canReadWait返回canOK状态时执行该函数。DisplayMessage显示了可以从返回的CAN帧数据中提取的许多信息。但最重要的是，该函数检查第二个指示符是否接收到实际的CAN数据帧。这是通过检查flags参数中的canMSG_ERROR_FRAME位来完成的。如果该位被置位，您收到错误帧，并且不处理标识符、数据长度代码或数据参数。如果该位未被置位，您就已收到一个CAN数据帧，并应根据需要处理该帧。</p>
<p>一旦完成，您应该通过使用canBusOff函数使总线上的CAN电路无效，然后使用canClose函数释放句柄来正常关闭程序。</p>
<p>现在让我们运行我们新的控制台应用程序，并实时对其工作进行查看。如你所见，应用程序能够连接到设备，开始接收CAN帧，并处理它们，直到我们按Escape退出。</p>
<h2 class="p1" style="font-weight: 400; color: #da2d2d;"><strong>更多信息</strong></h2>
<p class="p1" style="color: #646b6e;">有关CANlib的更多信息，您可以查看我们的<span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/canlib-webhelp/">CANlib SDK帮助</a></span>，并查看我们的<span style="color: #0d4187;"><a style="color: #0d4187;" href="http://www.kvaser.cn/support/developer-blog/">技术博客</a></span>中的系列文章。</p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-basic-api-calls/">设置CANlib：基础API调用</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Kvaser TRX和环境变量（3/3）</title>
		<link>https://www.kvaser.cn/developer-blog/kvaser-trx-environment-variables-3-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Tue, 15 Sep 2015 20:42:21 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=8107</guid>

					<description><![CDATA[<p>讲述如何使用Kvaser TRX查看环境变量并与其交互。</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/kvaser-trx-environment-variables-3-3/">Kvaser TRX和环境变量（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>这是关于t程序环境变量3篇系列文章的最后一篇：</em></p>
<ol>
<li><span style="text-decoration: underline;"><a title="环境变量简介" href="http://www.kvaser.cn/developer-blog/introduction-to-environment-variables-1-of-3/"><em>环境变量简介</em></a></span></li>
<li><span style="text-decoration: underline;"><a title="从CANlib访问环境变量" href="http://www.kvaser.cn/developer-blog/accessing-environment-variables-from-canlib-2-of-3/"><em>从CANlib访问环境变量</em></a></span></li>
<li><em>Kvaser TRX和环境变量</em></li>
</ol>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>在环境变量系列文章的最后一篇中，我们将会看到如何使用Kvaser TRX将不同的环境变量进行交互。 Kvaser TRX是一个轻量IDE，用于开发Kvaser设备的<em>t</em>程序，并包含在CANlib SDK <sup><a id="fnr.1" class="footref" href="#fn.1" name="fnr.1">1</a></sup>中。 完整的程序列表可访问<a title="http://github.com/Kvaser/developer-blog" href="http://github.com/Kvaser/developer-blog/">http://github.com/Kvaser/developer-blog</a>获取。</p>
<p>首先，我们通过使用Kvaser Eagle将能够运行<em>t</em>程序的设备连接到PC。现在我们启动Kvaser TRX并打开我们在本系列的第一篇文章里创建并保存在<code>envvar.t</code>中的<em>t</em>程序。我们通过选择File-&gt; Open选项，然后浏览文件，选择文件，然后单击“Open”。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen.png" class="vc_single_image-img  attachment-full" alt="" title="001-TRX_open" srcset="https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen.png 964w, https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen-324x243.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen-416x312.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen-300x225.png 300w, https://www.kvaser.cn/wp-content/uploads/2015/08/001-trxopen-768x577.png 768w" sizes="(max-width: 964px) 100vw, 964px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>图1：启动Kvaser TRX，将Eagle分析仪连接到PC，加载文件<code>envvar.t</code>。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>下一步是编译，下载和运行<em>t</em>程序。快速执行此操作的方法是按下Kvaser TRX工具栏上的“Play”’按钮。弹出的窗口将要求您保存新项目。您只需接受默认名称，然后单击“Save”。</p>
<p>一旦报文&#8221;Script no 0 started. proc=0&#8243; 出现在下部窗格的“设备输出”部分中，即代表<em>t</em>程序开始启动。在模拟引导延迟之后，将出现一条新报文&#8221;Signal that we are ready to accept connections&#8221;，见图2。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning.png" class="vc_single_image-img  attachment-full" alt="" title="002-TRX_running" srcset="https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning.png 964w, https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning-324x243.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning-416x312.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning-300x225.png 300w, https://www.kvaser.cn/wp-content/uploads/2015/08/002-trxrunning-768x577.png 768w" sizes="(max-width: 964px) 100vw, 964px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>图2：<em>t</em>程序已启动，我们可以在Kvaser TRX的“设备输出”选项中看到打印输出。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>要查看环境变量，我们首先通过从菜单中选择Window-&gt; Toggle Envvar窗口打开Envvar窗口。 在Envvar窗口中，我们输入我们想要查看的环境变量的名称，然后单击“Add Envvar”，如图3所示。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/08/02b-envvaradd.png" class="vc_single_image-img  attachment-full" alt="" title="02b-Envvar_add" srcset="https://www.kvaser.cn/wp-content/uploads/2015/08/02b-envvaradd.png 650w, https://www.kvaser.cn/wp-content/uploads/2015/08/02b-envvaradd-324x126.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/08/02b-envvaradd-416x161.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/08/02b-envvaradd-300x116.png 300w" sizes="(max-width: 650px) 100vw, 650px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>图3：将环境变量<code>HostIdConnected</code>添加到Kvaser TRX中的Envvar窗口。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>如果我们想看到在Envvar窗口中定期更新的环境变量的值，我们可以勾选“Auto Refresh”复选框，点击“Auto Refresh”之后，我们现在可以重新启动<em>t</em>程序（选择Run-&gt; Stop然后再选择Run-&gt; Start），并观察到<em>t</em>程序启动时<code>HostIdConnected</code>的值为1，并在报文“&#8221;Signal that we are ready to accept connections.”弹出时切换为0。这正如我们的预期结果，有关详细信息，请参阅<em>t</em>程序列表。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>现在我们按照添加环境变量<code>HostIdConnected</code>的方式再添加两个环境变量，<code>Severity</code>和<code>Message</code>。</p>
<p>当一个环境变量被选择时，将会显示出一个状态行，其中会显示该环境变量的名称、类型和值。显示值的对话框被标记为“New Value”，提示您可以在此处输入新值，然后单击“Set Value”以更新所选环境变量的值，请参见图4。</p>
<p>状态行还显示出一个名为“Remove”的按钮，可用于从Envvar窗口中删除所选的环境变量。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable.png" class="vc_single_image-img  attachment-full" alt="" title="04-Envvar_setvariable" srcset="https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable.png 1284w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-324x211.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-416x271.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-300x195.png 300w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-768x500.png 768w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-1024x667.png 1024w, https://www.kvaser.cn/wp-content/uploads/2015/08/04-envvarsetvariable-1200x781.png 1200w" sizes="(max-width: 1284px) 100vw, 1284px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>图4：在Kvaser TRX的Envvar窗口中为环境变量 <code>Message</code>设置新值。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>将Message的值更改为“My Message”，然后单击“Set Value”。除了在Envvar窗口中更新该值之外，我们还可以在运行的<em>t</em>程序中捕获到这次更改，并在Kvaser TRX窗口的下部窗格控制台中输入“Severity: 0 -My message”。</p>
<p>你还可以尝试并更改Severity。 更改将在Envvar窗口中更新，但由于我们的<em>t</em>程序中没有与该环境变量有关的设置，因此控制台区域中不会发生任何更改。</p>
<p>作为练习，你可以尝试并更改环境变量<code>HostIdConnected</code>的值。为什么Envvar窗口不显示新值？我们提示您检查一下变量<code>HostIdConnected</code>的<code>on envvar</code>设置……</p>
<p>关于<em>t</em>程序环境变量的系列文章就此结束，希望您满意。如果您有任何意见，请发送邮件至support@kvaser.com和我们取得联系。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h2 class="footnotes"><strong>脚注：</strong></h2>
<p><sup><a id="fn.1" class="footnum" href="#fnr.1" name="fn.1">1</a></sup>关于CANlib SDK的更多信息，请访问<a title="http://www.kvaser.cn/developer/canlib-sdk" href="http://www.kvaser.cn/developer/canlib-sdk/">http://www.kvaser.cn/developer/canlib-sdk/</a></p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/kvaser-trx-environment-variables-3-3/">Kvaser TRX和环境变量（3/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>从CANlib访问环境变量（2/3）</title>
		<link>https://www.kvaser.cn/developer-blog/accessing-environment-variables-from-canlib-2-of-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Thu, 10 Sep 2015 20:38:04 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=8106</guid>

					<description><![CDATA[<p>阐述如何使用CANlib访问环境变量。</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/accessing-environment-variables-from-canlib-2-of-3/">从CANlib访问环境变量（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>这是关于t程序环境变量3篇系列文章的第2篇：</em></p>
<ol>
<li><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/introduction-to-environment-variables-1-of-3/"><em>环境变量简介</em></a></span></li>
<li><em>从CANlib访问环境变量</em></li>
<li><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/kvaser-trx-environment-variables-3-3/"><em>Kvaser TRX和环境变量</em></a></span></li>
</ol>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>本系列的第一篇文章介绍了环境变量，以及如何在<em>t</em>程序中应用环境变量。第二篇将介绍如何使用CANlib访问环境变量。全部程序列表见<a href="http://github.com/Kvaser/developer-blog">http://github.com/Kvaser/developer-blog</a>。</p>
<p>环境变量用于在不同的<em>t</em>程序之间通信，或者使用CANlib与PC通信。 环境变量必须在<em>t</em>程序的envvar部分中定义。</p>
<p style="line-height: 18.0pt; background: white; margin: 0cm 0cm 20.25pt 0cm;"><span style="color: #303232;">为了从<span lang="EN-US">CANlib</span>访问环境变量，你需要为环境变量设置句柄：</span></p>
<ul>
<li><code>kvScriptEnvvarOpen (const int hnd, char *envvarName, int *envvarType, int *envvarSize)</code>打开现有的环境变量，并返回句柄以及环境变量的类型和大小。为了使环境变量存在，环境变量必须在CAN通道句柄<code>hnd</code>指向的设备加载的<em>t</em>程序中定义。</li>
<li><code>kvScriptEnvvarClose :: (kvEnvHandle eHnd)</code>关闭环境变量 (已使用<code>kvScriptEnvvarOpen()</code>打开)。</li>
</ul>
<p style="line-height: 18.0pt; background: white; margin: 0cm 0cm 20.25pt 0cm;"><span style="color: #303232;">你需要记住环境变量的数据类型。如果尝试使用错误类型访问环境变量，则访问将失败。根据不同的数据类型，可以使用不同的函数访问环境变量：</span></p>
<ul>
<li><code>kvScriptEnvvarSetInt :: (kvEnvHandle eHnd, int val)</code>设置由eHnd指向的环境变量的值，其已定义为<code>int</code>。</li>
<li><code>kvScriptEnvvarGetInt :: (kvEnvHandle eHnd, int *val)</code>检索定义为<code>int</code>的环境变量的值。</li>
<li><code>kvScriptEnvvarSetFloat :: (kvEnvHandle eHnd, float val)</code>设置定义为<code>float</code>的环境变量的值。</li>
<li><code>kvScriptEnvvarGetFloat :: (kvEnvHandle eHnd, float *val)</code>检索定义为<code>float</code>的环境变量的值。</li>
</ul>
<p>环境变量也可以定义为长度高达<code>ENVVAR_MAX_SIZE</code>的<code>char</code>数组。为了减少传输的数据的大小，使用<code>start_index</code>和<code>data_len</code>来交换定义为<code>char</code>数组的环境变量。 如果你已划分数据并知道你感兴趣的数据部分，这可能很有帮助。<span id="result_box" class="" lang="zh-CN"></span></p>
<ul style="list-style-type: disc;">
<li><code>kvScriptEnvvarSetData :: (kvEnvHandle eHnd, void *buf, int start_index, int data_len)</code>设置定义为<code>char</code>数组的环境变量中的数据字节范围。</li>
<li><code>kvScriptEnvvarGetData :: (kvEnvHandle eHnd, void *buf, int start_index, int data_len)</code> 从定义为<code>char</code>数组的环境变量中检索数据字节范围。</li>
</ul>
<p>现在，我们从Python的角度尝试谈论一下<em>t</em>程序。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h2><strong>使用Python和环境变量</strong></h2>
<p>现在我们可以来测试在本系列第一篇文章中概述过的与一台运行<em>t</em>程序的设备进行交互。为了运行测试，我们需要运行包含在CANlib SDK v5.12中的环境变量的包装器。预发布版本见<a title="https://github.com/Kvaser/canlib-samples" href="https://github.com/Kvaser/canlib-samples/">https://github.com/Kvaser/canlib-samples</a>。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">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, &quot;-Jung unccraf gb n sebt&#039;f pne jura vg oernxf qbja?&quot;),
            (2, &quot;-Vg trgf gbnq njnl.&quot;),
            (3, &quot;-Jul jnf fvk fpnerq bs frira?&quot;),
            (4, &quot;-Orpnhfr frira &#039;ngr&#039; avar.&quot;),
            (5, &quot;-Jung vf gur qvssrerapr orgjrra fabjzra naq fabjjbzra?&quot;),
            (6, &quot;-Fabjonyyf.&quot;),
            (7, &quot;-Jurer qvq lbh svaq gurfr?&quot;),
            (8, &quot;-uggc://jjj.ynhtusnpgbel.pbz/wbxrf/jbeq-cynl-wbxrf&quot;)]


# 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 = &quot;73-30130-00567-9&quot;
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, &quot;envvar.txe&quot;)

# 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 &quot;Waiting for device to be free...&quot;
# 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 &quot;Requesting connection...&quot;
myHostId = 42
print &quot;Waiting for device to connect...&quot;
while dev.channel.envvar.HostIdConnected != myHostId:
    if dev.channel.envvar.HostIdConnected == 0:
        print &quot;Requesting connection...&quot;
        if dev.channel.envvar.HostIdRequest != myHostId:
            dev.channel.envvar.HostIdRequest = myHostId
        time.sleep(0.2)
print &quot;Connected!&quot;

# Start sending our messages to the device
for (severity, message) in messages:
    print &quot;Sending message %d&quot; % severity
    dev.channel.envvar.Severity = severity
    dev.channel.envvar.Message = message.encode(&#039;rot13&#039;)
    time.sleep(4)

print &quot;Disconnect...&quot;
dev.channel.envvar.HostIdRequest = 0

# Stop the script in slot 0
dev.channel.scriptStop(0)
</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>执行上面的Python代码会产生如下的标准输出：</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<pre>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...</pre>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>我同意这并没有非常地激动人心，所以在本系列的最后一篇文章中，我们将一起看看如何使用Kvaser TRX与这些环境变量进行交互，Kvaser TRX是在Kvaser设备上开发<em>t</em>程序的轻量集成开发环境（IDE）。</p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/accessing-environment-variables-from-canlib-2-of-3/">从CANlib访问环境变量（2/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>环境变量简介（1/3）</title>
		<link>https://www.kvaser.cn/developer-blog/introduction-to-environment-variables-1-of-3/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Tue, 01 Sep 2015 20:37:37 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=8105</guid>

					<description><![CDATA[<p>环境变量简介以及如何在t程序中使用它们的示例阐述。</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/introduction-to-environment-variables-1-of-3/">环境变量简介（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" >
		<div class="wpb_wrapper">
			<p><em>这是关于t程序环境变量3篇系列文章的第1篇：</em></p>
<ol>
<li><em>环境变量简介</em></li>
<li><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/accessing-environment-variables-from-canlib-2-of-3/"><em>从CANlib访问环境变量</em></a></span></li>
<li><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/kvaser-trx-environment-variables-3-3/"><em>Kvaser TRX和环境变量</em></a></span></li>
</ol>

		</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 class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>第一篇文章介绍环境变量，以及如何在<em>t</em>程序中应用环境变量。</p>
<p>环境变量用于在不同的<em>t</em>程序之间通信，或者使用CANlib与PC通信。 图表1展示了Kvaser设备上正在运行的三个脚本。当脚本1设置一个环境变量，其它脚本或使用了CANlib的电脑也可以读取新数值。除此以外，脚本可以设定一个钩子（hook），达到特定环境变量值时触发。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/08/envarcommunication.png" class="vc_single_image-img  attachment-full" alt="" title="envar_communication" srcset="https://www.kvaser.cn/wp-content/uploads/2015/08/envarcommunication.png 490w, https://www.kvaser.cn/wp-content/uploads/2015/08/envarcommunication-324x278.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/08/envarcommunication-416x357.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/08/envarcommunication-300x257.png 300w" sizes="(max-width: 490px) 100vw, 490px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>图表1：有关环境变量值的信息如何分布的概述。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>术语“envvar”通常用作较长环境变量的简写。有关完整的文档，请阅读Kvaser t编程语言文档 <sup><a id="fnr.1" class="footref" href="#fn.1" name="fnr.1">1</a></sup>。</p>
<p>环境变量只能在<em>t</em>程序的<code>envvar</code>段中定义。就像在<code>variables</code>部分中定义的变量一样，环境变量在全局是可见的。这相当于在C程序中定义全局范围内的事项，即变量在声明和编译结束之间是可见的。</p>
<p>环境变量的类型可以是<code>int</code>，<code>float</code>或<code>char</code>数组。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">envvar {
  char Message[128];
  int Severity;
  int HostIdRequest;
  int HostIdConnected;
}</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>环境变量的大小限制于<code>ENVVAR_MAX_SIZE</code>（<em>t</em>编译程序3.2版本中为4096字节），你最多可以定义32个环境变量。</p>
<p>与普通变量不同，环境变量不能在<em>t</em>程序中初始化或直接访问。相反，它们是使用<code>envvarSetValue()</code> 和 <code>envvarGetValue()</code>来访问的。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">void setHostIdConnected (int value)
{
  hostIdConnected = value;
  envvarSetValue(HostIdConnected, value);
}</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>函数<code>envvarSetValue()</code>可以设置环境变量的值。实际的更新将需要排队，因此会被延迟，直到当前钩子的执行完成。环境变量已更新的通知随后将传播到已声明相同环境变量并具有相应的 <code>on envvar</code>钩子的所有程序。在此上下文中，更新包括与之前相同的值的设置。</p>
<p>由于环境变量的实际更新被延迟，因此可以在该延迟期间启动相同环境变量的新的更新。在这种情况下，较旧的值会丢失。所以每次你读取环境变量，你得到的都是它的最新值，如果你想确保能看到每次变化，你需要使用至少两个环境变量实施握手方案。</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-python">// Print incoming messages
on envvar Message {
  char msg[128];
  int severity;
  envvarGetValue(Message, msg);
  envvarGetValue(Severity, &amp;severity);
  printf(&quot;Severity: %d - %s\n&quot;, severity, msg);
}</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>函数<code>envvarGetValue()</code>检索环境变量的最后一个已知值。如果环境变量未初始化，结果为未定义。完整的程序列表见<a title="http://github.com/Kvaser/developer-blog" href="http://github.com/Kvaser/developer-blog">http://github.com/Kvaser/developer-blog</a>。</p>
<p>本系列的下一篇文章将阐述如何访问CANlib中的环境变量。</p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h2 class="footnotes">脚注：</h2>
<p><sup><a id="fn.1" class="footnum" href="#fnr.1" name="fn.1">1</a></sup> Kvaser <em>t</em>编程语言文档见<a href="http://www.kvaser.cn/downloads/#?filter=t%20programming">http://www.kvaser.cn/downloads/#?filter=t%20programming</a>.</p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/introduction-to-environment-variables-1-of-3/">环境变量简介（1/3）</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>如何在Visual Studio中设置CANlib</title>
		<link>https://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/</link>
		
		<dc:creator><![CDATA[Troy]]></dc:creator>
		<pubDate>Wed, 08 Jul 2015 23:32:10 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7847</guid>

					<description><![CDATA[<p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/">如何在Visual Studio中设置CANlib</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_video_widget wpb_content_element vc_clearfix   vc_video-aspect-ratio-169 vc_video-el-width-100 vc_video-align-left" >
		<div class="wpb_wrapper">
			
			<div class="wpb_video_wrapper"><a href="http://v.youku.com/v_show/id_XMTUzNDAxMjEzMg==">http://v.youku.com/v_show/id_XMTUzNDAxMjEzMg==</a></div>
		</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 class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h2 class="p1"></h2>
<p><em>这是CANlib开发3篇系列文章的第一篇：</em></p>
<div class="wpb_text_column wpb_content_element " style="color: #646b6e;">
<div class="wpb_wrapper">
<ol>
<li><i>在Visual Studio中设置</i></li>
<li><span style="text-decoration: underline;"><em><a href="http://www.kvaser.cn/developer-blog/setting-up-canlib-basic-api-calls/">基础API调用</a></em></span></li>
<li><span style="text-decoration: underline;"><a href="http://www.kvaser.cn/developer-blog/setting-up-canlib-event-driven/%20"><em>事件驱动应用程序</em></a></span></li>
</ol>
</div>
</div>
<h2 class="p1">简介</h2>
<p class="p1">欢迎浏览关于如何设置 Visual Studio C＃项目以使用 Kvaser CANlib 软件开发工具包。</p>
<p class="p1">在开始之前，请确保您已经安装了 CANlib 和我们最新的驱动程序。除了 Visual Studio C＃工具之外，还需要安装 Microsoft Visual C ++ 可重新分发包。该包必需安装，因为托管的 CANlib 动态链接库是使用 C ++ 创建的。</p>
<h2 class="p1">示例</h2>
<p class="p1">对于我们的示例，我们将创建一个 Windows C＃ 控制台应用程序。为此，我们从“启动页面”（Start Page）选择“新建项目”（New Project）来启动“新建项目”向导。打开向导后，请在“模板”（Templates）目录中的 “Visual C＃” 下选择 “Windows桌面”（Windows Desktop）。从模板列表中选择“控制台应用程序”（Console Application）。您也可以从可用模板列表上方的下拉菜单中选择所需的 .NET 框架。我们需要创建应用程序名称并选择项目的保存位置。最后，点击 OK，Visual Studio将创建解决方案目录和关联的项目文件。</p>
<p class="p1">项目打开后，右键单击解决方案资源管理器（Solution Explorer）中的项目文件。选择属性（Properties），然后在应用程序（Application）选项卡下，确保目标框架（Target Framework）是所需的框架版本。接下来，我们转到 Build 选项卡来设置 “目标平台”（Platform Target）。</p>
<p class="p1">首先将配置更改为 “所有配置”（ All Configurations）。</p>
<p class="p1">然后选择所需的目标平台，目标平台不能设置为针对任何类型的CPU。</p>
<p class="p1">CANlib 动态链接库是使用需要特定目标平台的 C ++ 创建的，因此，您必须为 x86 或 x64 系统的项目选择一个目标平台。此示例中我们将选择 x64。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/07/matrix.png" class="vc_single_image-img  attachment-full" alt="Visual Composer CANlib Compatibility Matrix" title="Matrix" srcset="https://www.kvaser.cn/wp-content/uploads/2015/07/matrix.png 960w, https://www.kvaser.cn/wp-content/uploads/2015/07/matrix-324x182.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/07/matrix-416x234.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/07/matrix-300x169.png 300w, https://www.kvaser.cn/wp-content/uploads/2015/07/matrix-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></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 class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<h2 class="p1"></h2>
<p class="p1">保存这些设置，然后右键单击解决方案资源管理器（Solution Explorer）中的引用（References）。选择“添加引用”（Add Reference），然后浏览选择与你的项目的目标平台和框架相匹配的CANlib DLL文件。在CANlib安装目录中，这些文件由目标平台按照框架版本分类到不同的子目录中。首先我们需要在CANlib安装目录中浏览dotnet。由于我们在本例中选择使用4.5.1 Framework和x64系统，所以我们将选择x64文件夹，然后选择fw40来查看与我们项目兼容的可用CANlib DLL。 对于这个项目，我们是与Kvaser CAN接口通信，所以我们将选择canlibCLSNET.dll。 选择完成后，它将出现在解决方案资源管理器的引用中。</p>
<p class="p1">我们现在可以添加使用CANlib API的代码。 为了证明项目设置正确，我们将首先添加几行来访问canlibCLSNET.dll中的函数。 我们首先使用“using”指令将canlibCLSNET命名空间添加到项目中。 然后在主程序中，我们将添加对canInitializeLibrary函数的调用。现在我们可以尝试构建项目。 由于项目构建无误，那么我们可以通过运行项目来确保所有运行时组件都存在。 我们的示例能够启动，并且运行时没有任何异常，所以我们知道我们的库已经成功链接到项目。</p>
<h2 class="p1">更多信息</h2>
<p class="p1">有关CANlib的更多信息，您可以查看我们的<span style="text-decoration: underline;"><a href="http://www.kvaser.cn/canlib-webhelp/">CANlib SDK帮助</a></span>，并查看我们的<span style="text-decoration: underline;"><a href="http://www.kvaser.cn/support/developer-blog/">技术博客</a></span>中的系列文章。</p>

		</div>
	</div>
</div></div></div></div>
</div><p>The post <a href="https://www.kvaser.cn/developer-blog/setting-up-canlib-for-visual-studio/">如何在Visual Studio中设置CANlib</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>CANlib通道号还是卡号？</title>
		<link>https://www.kvaser.cn/developer-blog/canlib-channel-number-card-number/</link>
		
		<dc:creator><![CDATA[Magnus Carlsson]]></dc:creator>
		<pubDate>Wed, 06 May 2015 17:00:00 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7403</guid>

					<description><![CDATA[<p>本文是共包括4部分的技术博客的最后一部分。此博客介绍通过kvmlib，使用第二代Kvaser Memorator数据记录仪，配置和读取已记录的数据</p>
<p>The post <a href="https://www.kvaser.cn/developer-blog/canlib-channel-number-card-number/">CANlib通道号还是卡号？</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>当与CANlib交互时，许多功能需要通道的句柄支持。这个通道通常被称为“CANlib通道”，因为这是其在Kvaser硬件中的标签。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/03/devicesinkvaserhardware.png" class="vc_single_image-img  attachment-large" alt="" title="devicesInKvaserHardware" srcset="https://www.kvaser.cn/wp-content/uploads/2015/03/devicesinkvaserhardware.png 694w, https://www.kvaser.cn/wp-content/uploads/2015/03/devicesinkvaserhardware-324x179.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/03/devicesinkvaserhardware-416x230.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/03/devicesinkvaserhardware-300x166.png 300w" sizes="(max-width: 694px) 100vw, 694px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>如果要从Eagle设备上的第一个通道发送CAN报文，我们应该打开CANlib通道3 <code>canopenChannel(3, canOPEN_EXCLUSIVE)</code>。 CANlib中的某些功能与设备上的实际通道无关，因此你可以使用任何设备通道都同样成功。例如，要打开或关闭设备LED，可以调用<code>kvFlashLeds(hnd, kvLED_ACTION_ALL_LEDS_ON)</code>，其中<code>hnd</code>可以是CANlib通道3或4的句柄。那么为什么需要卡号？从历史的角度来看，没有通过Kvaser CANlib与Kvaser设备的所有交互都直接指向驱动程序。在这种情况下，不能使用CANlib通道号，因此需要新的数字来指示，例如“第二个设备”使用驱动程序<code>kcany</code>。这个新号码被称为“卡号”，并在选择设备通道时显示在Kvaser硬件中。</p>

		</div>
	</div>
<div class="single-image"><img decoding="async" src="https://www.kvaser.cn/wp-content/uploads/2015/03/cardnumberinkvaserhardware.png" class="vc_single_image-img  attachment-large" alt="" title="cardNumberInKvaserHardware" srcset="https://www.kvaser.cn/wp-content/uploads/2015/03/cardnumberinkvaserhardware.png 694w, https://www.kvaser.cn/wp-content/uploads/2015/03/cardnumberinkvaserhardware-324x179.png 324w, https://www.kvaser.cn/wp-content/uploads/2015/03/cardnumberinkvaserhardware-416x230.png 416w, https://www.kvaser.cn/wp-content/uploads/2015/03/cardnumberinkvaserhardware-300x166.png 300w" sizes="(max-width: 694px) 100vw, 694px" /></div>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>在上图中，我们可以看到，作为通道设备5的Kvaser Memorator Pro 5xHS连接到CANlib通道6和卡号2。 CANlib通道和卡号都是从零开始(正如程序员通常所做的那样)，但是因随机生成机制，卡号在Kvaser硬件中显示(仅在Kvaser硬件中)得看起来像是从1开始。所以我们的设备实际上连接到了CANlib通道6卡号1，这意味着我们需要发出命令<code>hydra_flash.exe -C1 imagefile.img</code>以升级此设备的固件。如今，其已成为历史，因为我们通过调用CANlib (<code>canGetChannelData(channel, canCHANNELDATA_CARD_NUMBER, buffer, bufsize)</code>)函数可获得索引卡号0。这也是Python模块kvDevice的工作内容，其可在使用卡号的其它库(如kvmlib)进行交互时位你提供协助。不过，了解了其工作原理会很有意思。</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/canlib-channel-number-card-number/">CANlib通道号还是卡号？</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<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>
