我正在尝试从COM端口读取/写入一些数据。程序设置串口参数值如下:
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE).
然而,该程序陷入上述代码行。在该行之前,它成功执行。
完整代码如下:
public SendMsg(String className) throws NoSuchPortException, IOException, TooManyListenersException, UnsupportedCommOperationException
{
this.className=className;
//portList = CommPortIdentifier.getPortIdentifiers();
ports = CommPortIdentifier.getPortIdentifiers();
System.out.println("ports name"+ports);
while(ports.hasMoreElements())
{
pID = (CommPortIdentifier)ports.nextElement();
System.out.println("Port Name " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println("Port Name 1 " + pID.getName());
if (pID.getName().equals("COM1"))
{
try {
System.out.println("Port Name 2 " + pID.getName());
System.out.println("COM1 found");
serialPort=(SerialPort)pID.open(className, 1000);
outputStream=serialPort.getOutputStream();
inputStream=serialPort.getInputStream();
//f.serialEvent1(inputStream);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("hai");
serialPort.addEventListener(this);
break;
} catch (PortInUseException ex) {
Logger.getLogger(SendMsg.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}