当前位置:首页 » 编程语言 » 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();
}

}

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:573
制作脚本网站 发布:2025-10-20 08:17:34 浏览:863
python中的init方法 发布:2025-10-20 08:17:33 浏览:561
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:743
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:663
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:983
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:233
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:92
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:785
python股票数据获取 发布:2025-10-20 07:39:44 浏览:691