bigbat 发表于 2020-2-5 22:37

树莓派windows iot 开发(四) UART接口

<div class='showpostmsg'><p align="left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">目录</span></span></p>

<hr />
<p align="left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><a href="https://home.eeworld.com.cn/thread-1108717-1-1.html" target="_blank" title="树莓派windows iot 开发(一)">树莓派windows iot 开发(一)</a></span></span></p>

<p align="left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><a href="https://home.eeworld.com.cn/thread-1108724-1-1.html" target="_blank" title="树莓派windows iot 开发(二) USB摄像机">树莓派windows iot 开发(二) USB摄像机</a></span></span></p>

<p align="left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><a href="https://home.eeworld.com.cn/thread-1108877-1-1.html" target="_blank" title="树莓派windows iot 开发(三) 闪烁的LED">树莓派windows iot 开发(三) 闪烁的LED</a></span></span></p>

<p align="left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><a href="https://home.eeworld.com.cn/thread-1109072-1-1.html" target="_blank" title="树莓派windows iot 开发(四) UART接口">树莓派windows iot 开发(四) UART接口</a></span></span></p>

<p align="left">&nbsp;</p>

<p align="left">&nbsp;</p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">树莓派windows iot开发(四)UART接口</span></span></p>

<hr />
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><span lang="EN-US">UART</span>设备是一种历史悠久的通讯外设。树莓派也支持<span lang="EN-US">UART</span>设备,但接口是<span lang="EN-US">TTL</span>接口电平,如果你使用的是标准的<span lang="EN-US">RS232</span>接口需要使用<span lang="EN-US">TTL</span>转<span lang="EN-US">RS232</span>串口模块。</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"></span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">设备接口定义:<span lang="EN-US">8PIN</span>和<span lang="EN-US">10PIN</span>为<span lang="EN-US">UART</span>接口。</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">实验前:将<span lang="EN-US">TTL</span>转换板的引脚与设备连接。</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">使用VS2019打开Samples\ SerialUART\CS\ SerialSample.sln项目。<br />
程序运行后,打开设备可以看到设备连接的信息。从屏幕中可以看到树莓派中只有一个UART设备,在设备的列表中有</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&quot;\\?\ACPI#BCM2836#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}&quot; </span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">字串就是信息。这个和我们看到的COM1这样的名字不一样。</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">使用串口助手发送&ldquo;Hello&rdquo;,下面我们分析一下代码。</span></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cpp"></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); left: 0px; top: -15px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">这是一段简化的代码。</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">serialPort = await SerialDevice.FromIdAsync(entry.Id);</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">其中的参数entry.Id是文本类型的参数,就是在设备列表中出现的字串</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&quot;\\?\ACPI#BCM2836#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}&quot;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">如果是已选定的设备直接将字串传给SerialDevice对象。</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp; serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serialPort.BaudRate = 9600;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serialPort.Parity = SerialParity.None;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serialPort.StopBits = SerialStopBitCount.One;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serialPort.DataBits = 8;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">serialPort对象就是UART设备,参数为&ldquo;9600,N,8,1&rdquo;</span></span></p>

<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">数据的接收。</span></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cs"></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); left: 0px; top: -15px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">可以看到Listen中有一个异步死循环。这就是事件的检查函数。但是不用担心这个循环只是把事件挂钩到函数上就会退出来。然后程序就继续了。</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">发送数据就要简单些了。</span></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cs"></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); left: 0px; top: -15px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">从代码中可以看到需要两个步骤,</span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">一、将数据设置到<span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px"><span lang="EN-US"><span style="color:#24292e">dataWriteObject</span></span></span></span></span><span style="orphans:2; -webkit-text-stroke-width:0px"><span style="overflow-wrap:normal"><span style="float:none"><span style="word-spacing:0px"><span lang="EN-US"><span style="background:#f6f8fa"><span style="color:#24292e">.</span></span></span></span></span></span></span><span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px"><span lang="EN-US"><span style="color:#6f42c1">WriteString</span></span></span></span></span><span style="orphans:2; -webkit-text-stroke-width:0px"><span style="overflow-wrap:normal"><span style="float:none"><span style="word-spacing:0px"><span lang="EN-US"><span style="background:#f6f8fa"><span style="color:#24292e">(</span></span></span></span></span></span></span><span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px"><span lang="EN-US"><span style="color:#24292e">sendText</span></span></span></span></span><span style="orphans:2; -webkit-text-stroke-width:0px"><span style="overflow-wrap:normal"><span style="float:none"><span style="word-spacing:0px"><span lang="EN-US"><span style="background:#f6f8fa"><span style="color:#24292e">.</span></span></span></span></span></span></span><span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px"><span lang="EN-US"><span style="color:#24292e">Text</span></span></span></span></span><span style="orphans:2; -webkit-text-stroke-width:0px"><span style="overflow-wrap:normal"><span style="float:none"><span style="word-spacing:0px"><span lang="EN-US"><span style="background:#f6f8fa"><span style="color:#24292e">); </span></span></span></span></span></span></span></span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">二、启动发送函数,<span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px"><span lang="EN-US"><span style="color:#d73a49">await</span></span></span></span></span><span style="orphans:2; -webkit-text-stroke-width:0px"><span style="overflow-wrap:normal"><span style="float:none"><span style="word-spacing:0px"> <span lang="EN-US"><span style="color:#6f42c1"><span style="box-sizing:border-box; -webkit-text-stroke-width:0px"><span style="orphans:2"><span style="word-spacing:0px">WriteAsync</span></span></span></span></span><span lang="EN-US"><span style="background:#f6f8fa"><span style="color:#24292e">(); </span></span></span></span></span></span></span></span></span></p>

<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"><span lang="EN-US">windows iot</span>的系统是事件驱动的多任务系统,设备的驱动中会启动相应的线程。这要比自己写任务的系统简单一些。<br />
<br />
<b><font color="#5e7384">此内容由EEWORLD论坛网友bigbat原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b></span></span></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

bigbat 发表于 2020-2-6 10:13

<p>大家好象对windows iot没啥兴趣呀!因为我从事过很多工控机的项目,所以觉得windows系统不只是桌面系统,其实有些系统用windows开发比较好的。</p>

okhxyyo 发表于 2020-2-6 13:01

bigbat 发表于 2020-2-6 10:13
大家好象对windows iot没啥兴趣呀!因为我从事过很多工控机的项目,所以觉得windows系统不只是桌面系统,其 ...

挺不错的。感觉最近肺炎的事情闹的,大家研究的心思也少

freebsder 发表于 2020-2-6 13:03

挺好的,兴趣慢慢建立,毕竟Windows属于闷声发大财的主。当年wince可没安卓这么多关注。win iot还是平台少了,东西是真不错。
页: [1]
查看完整版本: 树莓派windows iot 开发(四) UART接口