@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
PeripheralManagerService manager = new PeripheralManagerService();
mDevice = manager.openUartDevice(UART_DEVICE_NAME);
} catch (IOException e) {
Log.w(TAG, "Unable to access UART device", e);
}
}
2.配置参数:调用setBaudrate()、setDataSize、setParity()和setStopBits等方法设置波特率、数据大小、错误校验和结束位等。
public void configureUartFrame(UartDevice uart) throws IOException {
uart.setBaudrate(115200);
uart.setDataSize(8);
uart.setParity(UartDevice.PARITY_NONE);
uart.setStopBits(1);
}
如果你的设备支持5线UART端口,启动硬件流控制可以提交数据传输的可靠性。通常这也意味着你可以安全地使用更快的波特率,而丢失传入数据的几率要低得多。
public void setFlowControlEnabled(UartDevice uart, boolean enable) throws IOException {
if (enable) {
// Enable hardware flow control
uart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_AUTO_RTSCTS);
} else {
// Disable flow control
uart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_NONE);
}
}
3.发送数据:使用UART向外设发送缓冲数据,使用write()方法。
public void writeUartData(UartDevice uart) throws IOException {
byte[] buffer = {...};
int count = uart.write(buffer, buffer.length);
Log.d(TAG, "Wrote " + count + " bytes to peripheral");
}
4.监听输入数据:使用read()方法从UART FIFO缓冲中向你的应用读取数据。这个方法接受一个空缓冲来填充输入数据,要读取的最大字节数。
@Override
protected void onStart() {
super.onStart();
// Begin listening for interrupt events
mDevice.registerUartDeviceCallback(mUartCallback);
}
public void readUartBuffer(UartDevice uart) throws IOException {
// Maximum amount of data to read at one time
final int maxCount = ...;
byte[] buffer = new byte[maxCount];
int count;
while ((count = uart.read(buffer, buffer.length)) > 0) {
Log.d(TAG, "Read " + count + " bytes from peripheral");
}
}
@Override
protected void onStop() {
super.onStop();
// Interrupt events no longer necessary
mDevice.unregisterUartDeviceCallback(mUartCallback);
}
private UartDeviceCallback mUartCallback = new UartDeviceCallback() {
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
// Read available data from the UART device
try {
readUartBuffer(uart);
} catch (IOException e) {
Log.w(TAG, "Unable to access UART device", e);
}
// Continue listening for more interrupts
return true;
}
@Override
public void onUartDeviceError(UartDevice uart, int error) {
Log.w(TAG, uart + ": Error event " + error);
}
};
5.关闭连接:当你完成和外部设备的通信,调用close()方法关闭连接并释放资源。此外在现有端口关闭之前,你不能打开相同端口的新连接。
@Override
protected void onDestroy() {
super.onDestroy();
if (mDevice != null) {
try {
mDevice.close();
mDevice = null;
} catch (IOException e) {
Log.w(TAG, "Unable to close UART device", e);
}
}
}
三、案例演示
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LoopbackActivity";
private static final String UART_DEVICE_NAME = "UART0";
private static final int BAUD_RATE = 115200;
private static final int DATA_BITS = 8;
private static final int STOP_BITS = 1;
private static final int CHUNK_SIZE = 512;
private UartDevice mUartDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PeripheralManagerService peripheralManagerService = new PeripheralManagerService();
try {
//通过UART接口名称UART0,打开接口
mUartDevice = peripheralManagerService.openUartDevice(UART_DEVICE_NAME);
//设置波特率、数据大小、校验等参数
mUartDevice.setBaudrate(BAUD_RATE);
mUartDevice.setDataSize(DATA_BITS);
mUartDevice.setParity(UartDevice.PARITY_NONE);
mUartDevice.setStopBits(STOP_BITS);
//注册数据监听,在有数据可读的时候回调
mUartDevice.registerUartDeviceCallback(mUartDeviceCallback);
} catch (IOException e) {
e.printStackTrace();
}
}
private UartDeviceCallback mUartDeviceCallback = new UartDeviceCallback() {
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
try {
//读取PC终端发来的数据 ,并原封返回给PC
byte[] buffer = new byte[CHUNK_SIZE];
int read;
while ((read = mUartDevice.read(buffer, buffer.length)) > 0) {
Log.w(TAG, "read from PC:"+ new String(buffer));
mUartDevice.write(buffer, read);
}
} catch (IOException e) {
Log.w(TAG, "Unable to transfer data over UART", e);
}
return true;
}
@Override
public void onUartDeviceError(UartDevice uart, int error) {
Log.w(TAG, uart + ": Error event " + error);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//在不需要的时候,关闭连接
if (mUartDevice != null) {
try {
mUartDevice.close();
mUartDevice = null;
} catch (IOException e) {
Log.w(TAG, "Unable to close UART device", e);
}
}
}
}
代码库:https://github.com/ThingsDeveloper/uartdemo
3.运行结果
我们是以windows系统演示,故使用了Termite终端,设置串口(相关驱动推荐使用驱动人生自动安装) 连接参数(与代码中一致,相关操作这里就不详细介绍)。