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

}

熱點內容
蘋果手機如何存儲word 發布:2025-02-09 02:23:02 瀏覽:780
安卓手機如何有蘋果的emoji 發布:2025-02-09 02:11:02 瀏覽:807
編譯原理已知語言求文法習題 發布:2025-02-09 02:05:15 瀏覽:132
中國首個具有世界影響力的編譯器 發布:2025-02-09 01:56:21 瀏覽:720
tomcat上傳超時 發布:2025-02-09 01:41:42 瀏覽:483
androidactivity豎屏 發布:2025-02-09 01:41:40 瀏覽:377
家庭配置怎麼合理 發布:2025-02-09 01:36:14 瀏覽:807
頭條軍事源碼 發布:2025-02-09 01:31:53 瀏覽:997
androidintent視頻 發布:2025-02-09 01:31:43 瀏覽:858
歐姆龍plc密碼如何設置 發布:2025-02-09 01:24:31 瀏覽:687