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

}

热点内容
服务器有外网ip 发布:2025-05-19 14:02:02 浏览:832
电脑上c语言编程软件 发布:2025-05-19 13:55:17 浏览:123
php56windows 发布:2025-05-19 13:54:23 浏览:716
如何查看攻击过服务器的ip地址 发布:2025-05-19 13:45:44 浏览:941
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:336
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:380
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:613
java用什么软件写 发布:2025-05-18 03:56:19 浏览:32
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:945