Rockchip UART (Universal Asynchronous Receiver/Transmitter) 基于16550A串口标准,完整模块支持以下
功能:
Rockchip UART对常用的波特率,如115200、460800、921600、1500000、3000000、4000000等确保稳定
支持。对于一些特殊的波特率(特殊的波特率我们不提供支持
),可能需要修改工作时钟分频策略才能支持。
风火轮科技开发一款rk3588s的主板youyeetoo R1,该主板提供40pin扩展管脚,其中提供3路uart管脚使用。管脚分布如下图所示:
管脚为
这里笔者以UART5管脚为例。将UART5的RX管脚与串口模块的TX管脚连接,UART5的TX管脚与串口模块的RX管脚连接。再将板子的GND与串口模块的GND管脚相连。
Android系统的权限管理是十分严格的,使用c++或者java操作Android根文件系统里的文件或资源需要对应的权限。这里编写的程序是基于 system app的作为模板进行编写。system app请参考创建system app这一章。使用Android源码官方的串口API,除了要是system app以外,还需要申请串口权限。申请方法如下:
<uses-permission android:name="android.permission.SERIAL_PORT"
tools:ignore="ProtectedPermissions" />
package com.youyeetoo.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.hardware.SerialManager;
import android.hardware.SerialPort;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.nio.ByteBuffer;
public class MainActivity extends AppCompatActivity implements Runnable, TextView.OnEditorActionListener {
private static final String TAG = "SerialChat";
private TextView mLog;
private EditText mEditText;
private ByteBuffer mInputBuffer;
private ByteBuffer mOutputBuffer;
private SerialManager mSerialManager;
private SerialPort mSerialPort;
private boolean mPermissionRequestPending;
private static final int MESSAGE_LOG = 1;
@SuppressLint({"WrongConstant", "MissingInflatedId"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSerialManager = (SerialManager)getSystemService("serial");
mLog = (TextView)findViewById(R.id.log);
mEditText = (EditText)findViewById(R.id.message);
mEditText.setOnEditorActionListener(this);
if (false) {
mInputBuffer = ByteBuffer.allocateDirect(1024);
mOutputBuffer = ByteBuffer.allocateDirect(1024);
} else {
mInputBuffer = ByteBuffer.allocate(1024);
mOutputBuffer = ByteBuffer.allocate(1024);
}
}
@Override
public void onResume() {
super.onResume();
String[] ports = mSerialManager.getSerialPorts();
if (ports != null && ports.length > 0) {
try {
mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);
if (mSerialPort != null) {
new Thread(this).start();
}
} catch (IOException e) {
}
}
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
if (mSerialPort != null) {
try {
mSerialPort.close();
} catch (IOException e) {
}
mSerialPort = null;
}
super.onDestroy();
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (/* actionId == EditorInfo.IME_ACTION_DONE && */ mSerialPort != null) {
try {
String text = v.getText().toString();
Log.d(TAG, "write: " + text);
byte[] bytes = text.getBytes();
mOutputBuffer.clear();
mOutputBuffer.put(bytes);
mSerialPort.write(mOutputBuffer, bytes.length);
} catch (IOException e) {
Log.e(TAG, "write failed", e);
}
v.setText("");
return true;
}
Log.d(TAG, "onEditorAction " + actionId + " event: " + event);
return false;
}
@Override
public void run() {
Log.d(TAG, "run");
int ret = 0;
byte[] buffer = new byte[1024];
while (ret >= 0) {
try {
Log.d(TAG, "calling read");
mInputBuffer.clear();
ret = mSerialPort.read(mInputBuffer);
Log.d(TAG, "read returned " + ret);
mInputBuffer.get(buffer, 0, ret);
} catch (IOException e) {
Log.e(TAG, "read failed", e);
break;
}
if (ret > 0) {
Message m = Message.obtain(mHandler, MESSAGE_LOG);
String text = new String(buffer, 0, ret);
Log.d(TAG, "chat: " + text);
m.obj = text;
mHandler.sendMessage(m);
}
}
Log.d(TAG, "thread out");
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_LOG:
mLog.setText(mLog.getText() + (String)msg.obj);
break;
}
}
};
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
>
<TextView android:id="@+id/log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="12sp"
android:textColor="#000000"
/>
</ScrollView>
<EditText android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>