# COMApplication **Repository Path**: guchuanhang/comapplication ## Basic Information - **Project Name**: COMApplication - **Description**: Android平台下,演示如何快速集成串口通信. - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-12-26 - **Last Updated**: 2024-11-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README COMApplication 一个演示串口通信的Demo ================= 这里仅简单说明调用方法和调用流程,至于要研究jni下C++工作过程,不适用。 SerialPort ------------ 这里面调用 native 方法,完成串口通信的底层逻辑. 打开串口,拿到InputStream、OutputStream. ReadWriteTempUtil ------------ 这里面对COM的操作进行了封装. * 启动一个ReadThread,实时监听COM InputStream 读取数据. 由于COM通信是阻塞式的并且一次read(),未必可以读取所有数据. 这个时候,需要根据指令类型判断同一个指令的所有数据是否都已经读取完整.然后根据指令文档,来解析数据. 为了便于交互,这里使用EventBus.将接收到的数据,发送出去. * 针对所有的COM指令,封装方法,向OutputStream 写入数据. # 接入方法 * 根据接口文档,定义要发送的指令. eg. setSendRate * 根据接口文档,定义每个指令,要返回的数据大小.eg. getResultCount * 根据接口文档,定义指令解析的方法.eg. parase_pkg * 使用EventBus 传递解析到的数据. 其他 ----------- * 串口的指令定义格式可以参照./测温模组规格书V1.3.pdf * 默认情况下, byte 转 int,会发生出现负值的情况. 这个时候不妨通过 (x+256)%256 拿到 byte 的 正确数值. * int 转 byte. 可以通过移位来进行处理 byte[] resultByte = new byte[4]; resultByte[0] = (byte) (num & 0xFF); resultByte[1] = (byte) (num >> 8 & 0xFF); resultByte[2] = (byte) (num >> 16 & 0xFF); resultByte[3] = (byte) (num >> 24 & 0xFF); * 多个字节数组合并成一个字节数组 可以通过System.arraycopy来进行处理. ## License ``` Copyright 2020, guchuanhang Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```