當前位置:首頁 » 編程語言 » 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();
}

}

熱點內容
大話西遊2華山論劍什麼時候開的伺服器 發布:2024-09-21 08:00:15 瀏覽:530
編程馬鞍數 發布:2024-09-21 07:48:32 瀏覽:726
新建文件夾dos命令 發布:2024-09-21 07:44:13 瀏覽:131
舞蹈解壓介紹 發布:2024-09-21 07:40:04 瀏覽:973
qq如何顯示密碼 發布:2024-09-21 07:22:26 瀏覽:566
安卓導航如何調大mlc音量 發布:2024-09-21 06:59:21 瀏覽:34
洛奇解壓中發生錯誤 發布:2024-09-21 06:53:30 瀏覽:293
解壓球是什麼 發布:2024-09-21 06:53:29 瀏覽:539
linux重啟nginx 發布:2024-09-21 06:45:27 瀏覽:786
編譯環境英語 發布:2024-09-21 06:19:59 瀏覽:865