Java 通过串口读取数据
List<BeiTaiBelter> getBelter(CommPortIdentifier portId, byte[] byteData,
int Lenth) throws PortInUseException,
UnsupportedCommOperationException, TooManyListenersException,
IOException, InterruptedException {
SerialPort serialPort=null;
try {
/**
* java读取串口 程序调试可以读取到数据,
* 正常运行读取不到数据(程序走到这一步(serialPort = (SerialPort) portId.open(portId.getName(), 3000);)
* 不在往下执行 死在这个地方) 求高手为我解决这个问题
*/
serialPort = (SerialPort) portId.open(portId.getName(), 3000);
} catch (PortInUseException e) {
System.out.println("duankou bei dddd");
e.printStackTrace();
}
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
// 波特率// 数据位数 // 停止位 // 奇偶位
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream inputStream = serialPort.getInputStream();
OutputStream outputStream = serialPort.getOutputStream();
outputStream.write(byteData);
Thread.sleep(1000);
List<BeiTaiBelter> list = new ArrayList<BeiTaiBelter>();
while (inputStream.available() > 0) {
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
list = beiTaiHardwareUtil.getBeiTaiBelterList(data, list);
}
inputStream.close();
outputStream.close();
serialPort.close();
return list;
}