啥是串口
串口即串行接口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。串行接口 (Serial Interface) 是指数据一位一位地顺序传送,其特点是通信线路简单,只要一对传输线就可以实现双向通信(可以直接利用电话线作为传输线),从而大大降低了成本,特别适用于远距离通信,但传送速度较慢。
在Android中,我们可以调用Unix的动态连接库(.so扩展名文件)来集成串口通信,这种调用的方式称为JNI(Java Native Interface,即Java本地接口)。
使用说明,简单几步英文版
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { compile 'com.github.kongqw:AndroidSerialPort:1.0.1' }
查看串口
SerialPortFinder serialPortFinder = new SerialPortFinder(); ArrayList<Device> devices = serialPortFinder.getDevices();
打开串口
mSerialPortManager = new SerialPortManager();
添加打开串口监听
mSerialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() { @Override public void onSuccess(File device) { } @Override public void onFail(File device, Status status) { } });
添加数据通信监听
mSerialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() { @Override public void onDataReceived(byte[] bytes) { } @Override public void onDataSent(byte[] bytes) { } });
打开串口
参数1:串口
参数2:波特率
返回:串口打开是否成功
boolean openSerialPort = mSerialPortManager.openSerialPort(device.getFile(), 115200);
发送数据
参数:发送数据 byte[]
返回:发送是否成功
boolean sendBytes = mSerialPortManager.sendBytes(sendContentBytes);
关闭串口
mSerialPortManager.closeSerialPort();
PS:传输协议需自行封装
其它例子(不推荐,供参考):
首先我们初始化SerialPortFinder,并创建SerialPort,打开串口:
mSerialPortFinder = new SerialPortFinder(); // 得到所有设备文件地址的数组 // 实际上该操作并不需要,这里只是示例打印出所有的设备信息 String[] entryValues = mSerialPortFinder.getAllDevicesPath(); try { // 打开/dev/ttyUSB0路径设备的串口 mSerialPort = new SerialPort(new File("/dev/ttyUSB0"), 9600, 0); } catch (IOException e) { System.out.println("找不到该设备文件"); }
这样,我们可以从SerialPort对象中得到输入流,并开启一个子线程进行读取该设备传入的串口数据:
final InputStream inputStream = mSerialPort.getInputStream(); /* 开启一个线程进行读取 */ new Thread(new Runnable() { @Override public void run() { try { byte[] buffer = new byte[1024]; int size = inputStream.read(buffer); byte[] readBytes = new byte[size]; System.arraycopy(buffer, 0, readBytes, 0, size); System.out.println("received data => " + new String(readBytes)); } catch (IOException e) { e.printStackTrace(); } } }).start();
发送数据时,我们只需要从SerialPort对象中得到OutputStream对象,写入字节数组数据即可:
// 开启子线程进行发送数据 new Thread(new Runnable() { @Override public void run() { String content = "Hello World"; byte[] bytes = content.getBytes(); OutputStream out = mSerialPort.getOutputStream(); // 写入数据 try { out.write(bytes); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }).start();
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2305
- 用户1336
- 访客11455720
每日一句
Talent without working hard is nothing.
没有努力,天份不代表什么。
没有努力,天份不代表什么。
MySQL 数据库优化
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
免ROOT实现模拟点击任意位置
Mobaxterm终端神器
CreateProcessW要注意的细节问题
Autonomous NAT Traversal
【教程】win10 彻底卸载edge浏览器
eclipse工程基于Xposed的一个简单Hook
排名前5的开源在线机器学习
Mac OS最简单及(Karabiner)快捷键设置
发一款C++编写的麻将
VMware NAT端口映射外网访问虚拟机linux
独家发布最新可用My-AutoPost——wordpress 采集器
新会员