提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
关于Android系统的串口通信,更多的是在上层app直接调用c,c++来打开串口实现串口的读写操作,但因客户需求,需要在系统层直接来完成对外设的控制,故写此篇文章帮助自己记录。
不能正常通讯的先看看编译文件.rc是否串口没有正常赋权限,第二个是看防火墙有没有关
adb发指令关闭防火墙
setenforce 0
我这里的需求是需要监听外接屏的两个按键,一个控制升降杆下降,一个控制升降杆上升
这里就很方便了,底层驱动配置好按键上报,只需要在监听按下的键值那里写业务代码即可
路径:V:\Projects\RK3566_A\frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager
import android.hardware.SerialPort;
import android.hardware.SerialManager;
import org.json.JSONObject;
import java.nio.ByteBuffer;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;
import org.json.JSONObject;
import org.json.JSONException;
private static final String TTYS3_PATH = "dev/ttyS3";
private static final int BAUD_RATE = 115200;
private SerialManager mSerialManager;
private ByteBuffer mInputBuffer;
private ByteBuffer mOutputBuffer;
public SerialPort mSerialPort;
public InputStream mInputStream;
public OutputStream mOutputStream;
这段记得写在interceptKeyBeforeDispatching方法里
//初始化
mSerialManager =(SerialManager)mContext.getSystemService(Context.SERIAL_SERVICE);
try {
mSerialPort = mSerialManager.openSerialPort(TTYS3_PATH, 115200);
} catch (IOException e) {
}
mInputBuffer = ByteBuffer.allocate(1024);
mOutputBuffer = ByteBuffer.allocate(1024);
//新建发送串口指令函数
public void sendSerialPort(byte[] data){
Log.d(TAG,"sendSerialPort data = "+data);
try {
mOutputBuffer.clear();
mOutputBuffer.put(data);
mSerialPort.write(mOutputBuffer, data.length);
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "COM1 OPEN FAILED!");
}
}
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -990,6 +990,7 @@
for example, /dev/ttyUSB0
-->
<string-array translatable="false" name="config_serialPorts">
+ <item>dev/ttyS3</item>
</string-array>
// First we always handle the home key here, so applications
// can never break it, although if keyguard is on, we do let
// it handle it, because that gives us the correct 5 second
// timeout.
if (keyCode == KeyEvent.KEYCODE_HOME) {
DisplayHomeButtonHandler handler = mDisplayHomeButtonHandlers.get(displayId);
if (handler == null) {
handler = new DisplayHomeButtonHandler(displayId);
mDisplayHomeButtonHandlers.put(displayId, handler);
}
return handler.handleHomeButton(focusedToken, event);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
try {
JSONObject root = new JSONObject();
root.put("callSn", "calltest0001");
root.put("type", 6);
sendSerialPort(root.toString().getBytes(Charset.forName("GBK")));
Log.e(TAG,"LQX send_down19");
} catch (JSONException e) {
// do nothing.
}
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
try {
JSONObject root = new JSONObject();
root.put("callSn", "calltest0001");
root.put("type", 5);
sendSerialPort(root.toString().getBytes(Charset.forName("GBK")));
Log.e(TAG,"LQX send_up20");
} catch (JSONException e) {
// do nothing.
}
}
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -271,6 +271,9 @@
<!-- Permission to make accessibility service access Bubbles -->
<uses-permission android:name="android.permission.ADD_TRUSTED_DISPLAY" />
+ <!-- Permission to serial port -->
+ <uses-permission android:name="android.permission.SERIAL_PORT" />
+
以上就是今天要讲的内容,本文仅仅简单介绍了串口在framework中的简单使用,如有遗漏,欢迎补充!