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

}

熱點內容
pc游戲編程人機博弈源碼 發布:2025-03-13 04:51:45 瀏覽:603
手機原生配置低怎麼玩流暢 發布:2025-03-13 04:35:31 瀏覽:735
分線器安卓供電口有什麼用 發布:2025-03-13 04:19:54 瀏覽:136
埠訪問關系 發布:2025-03-13 03:49:50 瀏覽:789
運用零基預演算法 發布:2025-03-13 03:45:30 瀏覽:791
安卓伺服器搭建web 發布:2025-03-13 03:40:26 瀏覽:317
銅板演算法 發布:2025-03-13 03:40:25 瀏覽:621
ins怎麼保存圖片安卓 發布:2025-03-13 03:38:14 瀏覽:214
什麼資料庫快 發布:2025-03-13 03:34:38 瀏覽:52
喋血復仇加入伺服器失敗怎麼回事 發布:2025-03-13 03:33:37 瀏覽:698