當前位置:首頁 » 編程語言 » 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-09-18 03:02:02 瀏覽:339
centos7apachephp7 發布:2025-09-18 03:01:47 瀏覽:651
安卓如何實現點擊彈出列表 發布:2025-09-18 02:47:25 瀏覽:52
python文件函數 發布:2025-09-18 02:47:23 瀏覽:568
pythonwrap 發布:2025-09-18 02:46:32 瀏覽:326
伺服器與計算機有什麼區別 發布:2025-09-18 02:07:26 瀏覽:930
python不支持的數據類型有 發布:2025-09-18 01:50:23 瀏覽:643
長江存儲科技招聘 發布:2025-09-18 01:44:48 瀏覽:767
斯維爾加密狗 發布:2025-09-18 01:31:00 瀏覽:253
手把手win7搭建web伺服器 發布:2025-09-18 01:25:53 瀏覽:730