/**
* Connect.
* @param portName RS232 port name.
* @param baudrate Baud rate.
* @param dataBits Data bits.
* @param stopBits Stop bits.
* @param parity Parity.
* @return Success or not.
* @throws Exception
*/
public boolean connect(String portName, int baudrate, int dataBits, int stopBits, int parity) throws Exception {
this.monitor = this.protocol.createMonitor(this.aliasName);
this.monitor.setController(this);
if (this.started) {
return true;
}
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
return false;
}
CommPort commPort = portIdentifier.open(getClass().getName(), 2000);
this.serialPort = (SerialPort) commPort;
this.serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity);
this.in = this.serialPort.getInputStream();
this.out = this.serialPort.getOutputStream();
this.serialPort.addEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent evt) {
messageReceived();
}
});
this.serialPort.notifyOnDataAvailable(true);
this.started = true;
return true;
}
RS232.java 文件源码
java
阅读 22
收藏 0
点赞 0
评论 0
项目:uia.comm4j
作者:
评论列表
文章目录