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

}

热点内容
无权限访问工作组的计算机 发布:2024-09-21 04:26:31 浏览:475
为什么ipad需要密码解锁 发布:2024-09-21 04:06:22 浏览:210
mariadb存储过程 发布:2024-09-21 03:56:05 浏览:514
压缩壳脱壳机 发布:2024-09-21 03:14:33 浏览:93
热血街篮为什么是服务器维护中 发布:2024-09-21 03:08:19 浏览:937
喇叭怎么配置功放 发布:2024-09-21 03:06:50 浏览:751
为什么安卓的内存没有苹果的内存 发布:2024-09-21 03:06:50 浏览:231
swift解压 发布:2024-09-21 02:31:47 浏览:705
移动中心怎么配置安卓系统 发布:2024-09-21 02:27:16 浏览:607
安卓手机旧版app怎么下载 发布:2024-09-21 02:12:35 浏览:800