当前位置:首页 » 编程语言 » 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-07-05 16:10:30 浏览:525
微博数据库设计 发布:2025-07-05 15:30:55 浏览:24
linux485 发布:2025-07-05 14:38:28 浏览:304
php用的软件 发布:2025-07-05 14:06:22 浏览:754
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:430
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:695
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:240
java的开发流程 发布:2025-07-05 12:45:11 浏览:684
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:282
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:833