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

}

熱點內容
api開發php 發布:2025-09-16 22:06:15 瀏覽:593
mysql自動備份linux 發布:2025-09-16 21:58:33 瀏覽:941
怎麼用自己的伺服器ip做域名 發布:2025-09-16 21:49:57 瀏覽:914
vc為什麼能編譯不能用 發布:2025-09-16 21:48:03 瀏覽:741
編譯原理沖突圖的定義 發布:2025-09-16 21:26:45 瀏覽:807
安卓手機芯哪個牌子好 發布:2025-09-16 21:26:33 瀏覽:205
centos編譯安裝git 發布:2025-09-16 21:19:55 瀏覽:973
安卓系統如何使用手機優盤 發布:2025-09-16 21:14:01 瀏覽:330
在手機上注冊公積金如何設置密碼 發布:2025-09-16 21:07:01 瀏覽:822
無控制器存儲 發布:2025-09-16 21:02:44 瀏覽:715