package test;
import java.util.Enumeration;
import java.util.HashMap;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class GetSerialPorts {
public void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
public static void main(String[] args) {
GetSerialPorts GSP = new GetSerialPorts();
GSP.listPortChoices();
}
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class GetSerialPorts {
private CommPortIdentifier portId;
private SerialPort testPort;
private CommPortIdentifier myPort;
private InputStream is;
private OutputStream os;
public void listPortChoices() {
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
myPort = portId;// 任意取一个串口,比如com1
}
}
public boolean openPort() {
try {
testPort = (SerialPort) myPort.open("COM1", 500);// 注意这里必须换成一个真实的串口
try {
this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.testPort.enableReceiveTimeout(30);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.testPort.setOutputBufferSize(1024);
this.testPort.setInputBufferSize(1024);
try {
this.is = this.testPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.os = this.testPort.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.testPort.notifyOnDataAvailable(true);
this.testPort.notifyOnOutputEmpty(true);
this.testPort.notifyOnBreakInterrupt(true);
// this.printerPort.addEventListener(new PrintPortListener(is));
System.out.println("打开com1机串口成功");
return true;
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* TODO 关闭端口
*
* @param
* @return Map
* @throws
*/
public boolean closePort() {
// TODO Auto-generated method stub
try {
if (null != this.testPort) {
is.close();
os.close();
this.testPort.close();
}
System.out.println("关闭COM1串口成功");
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("关闭COM1串口失败");
return false;
}
}
public static void main(String[] args) {
GetSerialPorts GSP = new GetSerialPorts();
GSP.listPortChoices();
GSP.openPort();
}
}
/**
* TODO 接收端口數據
*
* @param InputStream
* @return String
* @throws
*/
public String readData(InputStream is) {
// 读取缓冲区域
byte[] readBuffer = new byte[4096];
int readDataLength = 0;
try {
readDataLength = is.read(readBuffer);
// for (byte b : readBuffer) {
// System.out.print(b);
// }
// System.out.println();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
// 将真实数据保存到零时数组中
byte[] readTemp = new byte[readDataLength];
for (int i = 0; i < readDataLength; i++) {
readTemp[i] = readBuffer[i];
}
// 将byte数组转换为16进制字符串
String stringTemp = FeelTheBase.bytesToHexString(readTemp);
// System.out.println("指令返回值" + stringTemp);
return stringTemp;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有