啥是串口
串口即串行接口,也称串行通信接口或串行通讯接口(通常指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)
站点信息
- 文章2300
- 用户1336
- 访客10859735
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员