當前位置:首頁 » 編程語言 » java串口

java串口

發布時間: 2022-01-15 20:38:48

1. java實現串口通信代碼

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱
{
if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打開串口的超時時間為1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無
InputStream in = serialPort.getInputStream();//得到輸入流
OutputStream out = serialPort.getOutputStream();//得到輸出流

//進行輸入輸出操作

//操作結束後
in.close();
out.close();
serialPort.close();//關閉串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:516
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:19
linux485 發布:2025-07-05 14:38:28 瀏覽:299
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:751
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:426
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:689
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:234
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:680
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:278
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:828