最終更新:
moonlight_aska 2017年10月29日(日) 15:59:19履歴
- UartActivity.java
- PeripheralManagerServiceクラスのインスタンスを生成する.
- PeripheralManagerService#openUartDeviceメソッドで, ポートを指定してUartDeviceクラスのオブジェクトを取得する.
- UartDevice#setBaudrateメソッドで, ポーレートを設定する.
- UartDevice#setDataSizeメソッドで, データサイズを設定する.
- UartDevice#setParityメソッドで, パリティの有無を設定する.
- UartDevice#setStopBitsメソッドで, ストップビットを設定する.
- UartDevice#setHardwareFlowControlメソッドで,ハードウェアフロー制御の有無を設定する.
- UartDevice#writeメソッドで, データを送信する.
- UartDevice#closeメソッドで, デバイスを解放する.
package com.moonlight_aska.androidthings.uart;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.things.pio.PeripheralManagerService;
import com.google.android.things.pio.UartDevice;
import java.io.IOException;
public class UartActivity extends Activity {
private final static String TAG = "UartActivity";
private final static String UART_NAME = "UART0";
private UartDevice mUart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManagerService manager = new PeripheralManagerService();
try {
// UARTデバイスオープン
mUart = manager.openUartDevice(UART_NAME);
// UARTのパラメータ設定
setUartParam();
// データ送信
byte[] data = {'S', 'e', 'n', 'd', ' ', 't', 'e', 's','t', '.'};
int bytes = mUart.write(data, data.length);
Log.d(TAG, "Wrote " + bytes + " bytes");
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
// UARTのパラメータ設定
private void setUartParam() throws IOException {
// ボーレート
mUart.setBaudrate(115200);
// データサイズ
mUart.setDataSize(8);
// パリティ
mUart.setParity(UartDevice.PARITY_NONE);
// ストップビット
mUart.setStopBits(1);
// フロー制御
mUart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_NONE);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mUart != null) {
try {
// UARTデバイスクローズ
mUart.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
} finally {
mUart = null;
}
}
}
}
- 動作例
PC側画面結果

タグ
コメントをかく