- 论坛徽章:
- 0
|
1.2.2打开串口
示例代码如下:
代码:
try{
// ポートのオープン
port = (SerialPort)portID.open("portApp", 5000);
}catch(PortInUseException ex){
ex.printStackTrace();
}
5000(毫秒)是超时时间。
1.2.3设置串行端口通讯参数
设置串口传输的波特率、数据位、停止位、奇偶校验等参数。
示例代码如下:
代码:
try {
// 通信条件の設定
// 通信速度 9600 baud
// データビット 8bit
// ストップビット 1bit
// パリティ なし
// フローコントロールの設定
// 無制御を使用
port.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException ex) {
ex.printStackTrace();
System.exit(1);
}
1.2.4获取输入(出)流
示例代码如下:
代码:
//受信バッファ
protected BufferedReader comReader;
//送信バッファ
protected BufferedOutputStream comWriter;
try {
// RS-232C 入力用の Reader を生成
comReader = new BufferedReader(
new InputStreamReader(port.getInputStream()));
// RS-232C 出力用の Writer を生成
comWriter=
new BufferedOutputStream(port.getOutputStream());
} catch (IOException ex){
ex.printStackTrace();
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103647/showart_2038384.html |
|