<?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>t脚本 归档 - Kvaser</title>
	<atom:link href="https://www.kvaser.cn/developer_category/t-script/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.kvaser.cn/developer_category/t-script/</link>
	<description>先进的CAN总线解决方案专家</description>
	<lastBuildDate>Wed, 18 Sep 2024 10:50:46 +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>t脚本 归档 - Kvaser</title>
	<link>https://www.kvaser.cn/developer_category/t-script/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>t编程入门</title>
		<link>https://www.kvaser.cn/developer-blog/getting-started-with-t-programming/</link>
		
		<dc:creator><![CDATA[Martin Henriksson]]></dc:creator>
		<pubDate>Wed, 29 Apr 2015 08:00:30 +0000</pubDate>
				<guid isPermaLink="false">http://www.kvaser.cn/?post_type=developer_blog&#038;p=7440</guid>

					<description><![CDATA[<p>The post <a href="https://www.kvaser.cn/developer-blog/getting-started-with-t-programming/">t编程入门</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">
			<address><em><em>这是该系列文章中的第一部分。</em></em></address>
<hr />

		</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-3"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>新型Kvaser专业产品的脚本功能被命名位t。 t语言是一种面向事件的“c”语言。 t程序在设备上运行，可以从pc或设备上自动加载和启动。以下是t编程入门的简短介绍。</p>

		</div>
	</div>
<div class="vc_row wpb_row vc_inner 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></div></div><div class="wpb_column vc_column_container vc_col-sm-9"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p><a href="http://www.kvaser.cn/wp-content/uploads/2015/03/dsc0971-edit-4.jpg"><img decoding="async" class="alignnone" src="http://www.kvaser.cn/wp-content/uploads/2015/03/dsc0971-edit-4-1024x703.jpg" alt="_DSC0971-Edit-4" /></a></p>

		</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>为了能够测试和运行t程序，你需要一个Kvaser Professional产品（* 1）。所需软件由CANlib开发工具包进行安装，称为Kvaser TRX。</p>
<p>要开始运行并编写你的第一个t程序，首先下载并安装 <span style="text-decoration: underline;"><a href="http://www.kvaser.cn/support/developer/canlib-sdk/">CANlib开发工具包</a></span> (<span style="text-decoration: underline;"><a href="http://www.kvaser.cn/software/7330130980150/V5_9_0/canlib.exe">点击下载</a></span>)，然后启动TRX。</p>

		</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>如图：<a href="http://www.kvaser.cn/wp-content/uploads/2015/03/image11.jpg"><img decoding="async" class="alignnone" src="http://www.kvaser.cn/wp-content/uploads/2015/03/image11.jpg" alt="image1" /></a></p>
<p>在&lt;New&gt;标签中输入：</p>

		</div>
	</div>
<pre class="code-toolbar1 code-wrapper line-numbers"><code class="language-c">on start {
  printf(&quot;Hello World!\n&quot;);
}</code></pre>
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p>这是非常基本的程序，对‘on start’事件做出反应，并在‘Device Output’中输出“Hello World！”。事实上设备发送“Hello World！”给计算机，TRX进行读取。</p>
<p>然后从菜单中选择Run-&gt; Compile。为了能够编译，你首先必须保存t程序，TRX会提示你这样做，它也会提示你保存项目文件。我把它保存为blogpost1。</p>
<p>如果编译成功，下一步将需要一个真实的Kvaser professional 产品（*1）。</p>
<p>我连接上了Kvaser Eagle分析仪（<span style="text-decoration: underline;"><a href="http://www.kvaser.cn/products/kvaser-eagle/">查看产品</a></span>）。你可以看到，TRX可以使用你的设备，因为菜单下的组合框有你的Kvaser设备名称。</p>
<p>要测试t程序，只需打开Run-&gt; Download，这就会把t程序下载到设备。下一步就是真正地运行程序。 Run &#8211; &gt; Run将启动应用程序，由此你就会在Device Output窗口中看到printf输出内容。</p>
<p><a href="http://www.kvaser.cn/wp-content/uploads/2015/03/image21.jpg"><img decoding="async" class="alignnone" src="http://www.kvaser.cn/wp-content/uploads/2015/03/image21.jpg" alt="image2" /></a></p>

		</div>
	</div>

	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			
		</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">
			<hr />
<h3>其它资源：</h3>
<ul>
<li>t语言详细描述见《Kvaser t编程语言规范》（<span style="text-decoration: underline;"><a href="http://www.kvaser.cn/software/733013098032/V1_4_468/kvaser_t_programming_language.pdf">点击查看</a></span>）。</li>
</ul>
<hr />

		</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/getting-started-with-t-programming/">t编程入门</a> appeared first on <a href="https://www.kvaser.cn">Kvaser</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
