串口协议:c转java文件

<div id="article_content" class="article_content clearfix">
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-211130ba7a.css">
                            <div id="content_views" class="markdown_views">
                    <!-- flowchart 箭头图标 勿删 -->
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                                            <p>运行平台: <br>
CPU:全志V40 <br>
Android版本:6.0.1</p>

<hr>

<p>关于安卓设备上使用串口,谷歌官方在github上有提供代码实例,里面有JNI的代码和串口API的java文件,工程的地址如下: <br>
<a href="https://github.com/cepr/android-serialport-api">https://github.com/cepr/android-serialport-api</a></p>

<p>官方给出的代码是eclipse工程,SDK的版本也比较旧。我使用的Android Studio,如果直接使用google提供的so库文件是访问不了串口的,我平台需要使用的SDK版本是26,而github上这个开源的代码比较旧了,跟我SDK版本也不匹配,好在google有提供相应的JNI文件,从github上下载这个工程下来后,在android-serialport-api文件夹里面可以找到jni文件夹,里面包含有SerialPort.c,如下: <br>
<img src="https://img-blog.csdn.net/20180530182702415?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>有了这个文件后需要使用CMAKE去编译它,至于怎么安装和使用CMAKE我找到一篇文章: <br>
<a href="https://blog.csdn.net/qq_25817651/article/details/53135685">https://blog.csdn.net/qq_25817651/article/details/53135685</a> <br>
基本上只要看下怎么安装CMAKE就好了,下面我会继续讲下怎么使用。CMAKE安装好后下面先建立一个JNI工程,这个工程只是为了去获取CMakeLists.txt这个编译文件,先创建好工程,如下: <br>
<img src="https://img-blog.csdn.net/20180605192941140?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
<img src="https://img-blog.csdn.net/20180605193017905?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
<img src="https://img-blog.csdn.net/20180605193058863?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
这里直接Next就可以了,在工程下面可以看到有个CMakeLists.txt的文件,直接把这个CMakeLists.txt复制出来到工程目录下的app文件夹就可以关闭工程了,如果不添加这个文件,后面自己工程编译jni文件的时候会提示找打不到这个CMake文件 <br>
<img src="https://img-blog.csdn.net/20180605193741686?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
复制出来上面那个文件后,回到自己需要添加串口的工程,把这个文件复制到”项目名/app/”下,之后创建jni目录: <br>
<img src="https://img-blog.csdn.net/20180606135825437?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
<img src="https://img-blog.csdn.net/2018060520475025?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
并复制  android-serialport-api-master\android-serialport-api\project\jni 下的“SerialPort.c”文件到自己创建的jni目录下,注意在android studio中显示的是“cpp”目录。 <br>
<img src="https://img-blog.csdn.net/20180605195058620?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
<img src="https://img-blog.csdn.net/20180606140042779?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>在build.gradle(Module:app)里面添加如下内容,SDK版本根据自己需要进行修改: <br>
<img src="https://img-blog.csdn.net/20180605205611672?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>上面的步奏添加完后,进行同步后就会在工程页面出现我们前面添加的CMakeLists.txt,打开 CMakeLists.txt 做如下修改: <br>
<img src="https://img-blog.csdn.net/20180606142938232?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
<img src="https://img-blog.csdn.net/2018060520592717?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>

<p>然后编译,在下面这个目录下会生成对应的串口so文件: <br>
<img src="https://img-blog.csdn.net/2018060614372142?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
这个so文件就能够直接用起来了,把这个so文件放到libs文件下的“armeabi-v7a”里面,接下来写下如何使用这个so文件,google官方的例程里面有提供java的API文件: <br>
<img src="https://img-blog.csdn.net/20180606145537464?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
这个文件夹的名字必须为“android_serialport_api”,把这个文件夹复制到自己的工程下面 <br>
<img src="https://img-blog.csdn.net/20180607101854819?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
加载库文件修改为新生成的so文件 <br>
<img src="https://img-blog.csdn.net/20180607102029841?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2l0ZG9fanVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""> <br>
接下来就可以直接在工程里面使用串口了,代码如下:</p>

<pre class="prettyprint" name="code"><code class="hljs java has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        <span class="hljs-comment">//series</span>
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">/* 打开串口 */</span>
            mSerialPort = <span class="hljs-keyword">new</span> SerialPort(<span class="hljs-keyword">new</span> File(<span class="hljs-string">"/dev/"</span> + <span class="hljs-string">"ttyS2"</span>), <span class="hljs-number">115200</span>, <span class="hljs-number">0</span>);
            <span class="hljs-comment">//  mOutputStream = (FileOutputStream) mSerialPort.getOutputStream();</span>
            mInputStream = (FileInputStream) mSerialPort.getInputStream();

            <span class="hljs-comment">/* Create a receiving thread */</span>
            mWriteThread = <span class="hljs-keyword">new</span> WriteThread();<span class="hljs-comment">/* 创建串口处理线程 */</span>
            mWriteThread.start();

        } <span class="hljs-keyword">catch</span> (SecurityException e) {
            e.printStackTrace();
        } <span class="hljs-keyword">catch</span> (IOException e) {
            e.printStackTrace();
        }
    }<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li></ul></pre>                                    </div><div><div></div></div>
                <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                                </div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值