当前位置:首页 » 编程语言 » 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-03-14 00:10:32 浏览:471
tcl编译器 发布:2025-03-13 23:52:59 浏览:321
linuxnamed 发布:2025-03-13 23:45:29 浏览:361
阿里云30元服务器 发布:2025-03-13 23:21:25 浏览:350
pythonstatvfs 发布:2025-03-13 23:14:55 浏览:953
火车上有密码多少 发布:2025-03-13 23:14:10 浏览:865
解压火柴 发布:2025-03-13 22:46:39 浏览:336
开机密码在哪里存着 发布:2025-03-13 22:27:22 浏览:952
光流场算法 发布:2025-03-13 21:35:51 浏览:896
免编程轴控 发布:2025-03-13 21:19:24 浏览:781