本文整理匯總了Java中gnu.io.SerialPortEventListener類的典型用法代碼示例。如果您正苦於以下問題:Java SerialPortEventListener類的具體用法?Java SerialPortEventListener怎麽用?Java SerialPortEventListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SerialPortEventListener類屬於gnu.io包,在下文中一共展示了SerialPortEventListener類的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initialize
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
public void initialize() {
System.setProperty("gnu.io.rxtx.SerialPorts", getComPortName());
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if(currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (currPortId.getName().equals(txtComPortName.getText())) {
System.out.println(txtComPortName.getText());
portId = currPortId;
break;
}
}
}
if (portId == null) {
JOptionPane.showMessageDialog(null," Portuna bağlı cihaz yok!","Hata",JOptionPane.ERROR_MESSAGE);
System.out.println("Porta bağlı cihaz yok!");
return;
}
System.out.println(portId);
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
serialPort.addEventListener((SerialPortEventListener) this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
示例2: setSerialEventHandler
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* Set the serial event handler.
*
* @param serialPortEventListenser
*/
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
try {
// Add the serial port event listener
serialPort.addEventListener(serialPortEventListenser);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException tooManyListenersException) {
logger.error("setSerialEventHandler(): Too Many Listeners Exception: {}",
tooManyListenersException.getMessage());
}
}
示例3: addListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* 添加監聽器
*
* @param port
* 串口對象
* @param listener
* 串口監聽器
* @throws TooManyListeners
* 監聽類對象過多
*/
public static void addListener(SerialPort port,
SerialPortEventListener listener) throws TooManyListeners {
try {
// 給串口添加監聽器
port.addEventListener(listener);
// 設置當有數據到達時喚醒監聽接收線程
port.notifyOnDataAvailable(true);
// 設置當通信中斷時喚醒中斷線程
port.notifyOnBreakInterrupt(true);
} catch (TooManyListenersException e) {
throw new TooManyListeners();
}
}
示例4: setSerialEventHandler
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* Set the serial event handler
*/
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
try {
// Add the serial port event listener
serialPort.addEventListener(serialPortEventListenser);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException tooManyListenersException) {
logger.error("open(): Too Many Listeners Exception: ", tooManyListenersException);
}
}
示例5: addEventListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
public boolean addEventListener(SerialPortEventListener serialPortEventListener){
if (serialPort != null) {
try {
serialPort.addEventListener(serialPortEventListener);
serialPort.notifyOnDataAvailable(true);
return true;
} catch (TooManyListenersException e) {
System.err.println(e.toString());
}
}
return false;
}
示例6: addListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
public void addListener(SerialPortEventListener serialPortEventListener) {
try {
serialPort.addEventListener(serialPortEventListener);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
示例7: connect
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* 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;
}
示例8: addDataAvailableEventHandler
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* Register event handler for data available event
*
* @param eventHandler Event handler
*/
public void addDataAvailableEventHandler(
SerialPortEventListener eventHandler) {
try {
// Add the serial port event listener
serialPort.addEventListener(eventHandler);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException ex) {
System.err.println(ex.getMessage());
}
}
示例9: addEventListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
public void addEventListener ( SerialPortEventListener listener ) {
try {
sPort.addEventListener( listener );
} catch ( TooManyListenersException e ) {
sPort.close( );
logger.warn( e.getMessage( ) , e );
}
}
示例10: setSerialEventHandler
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* Set the serial event handler
*/
private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
try {
// Add the serial port event listener
serialPort.addEventListener(serialPortEventListenser);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException tooManyListenersException) {
logger.error("open(): Too Many Listeners Exception: ", tooManyListenersException);
}
}
示例11: addPortEventListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* F�gt den Listener der Instanz hinzu. Es kann dabei nur einen Listener geben, sonst wird eine Exception geworfen.
*
* @param spel
* @throws TooManyListenersException
*/
public void addPortEventListener(SerialPortEventListener spel) throws TooManyListenersException{
serialPort.addEventListener(spel);
}
示例12: addEventListener
import gnu.io.SerialPortEventListener; //導入依賴的package包/類
/**
* Add a port listener to the serial port. Only one listener is allowed at
* the same time. The port must be opened before a listener can be added.
*
* @param listener is the listener to add.
* @throws TooManyListenersException is thrown if another listener was
* previously added.
*/
public void addEventListener(SerialPortEventListener listener) throws TooManyListenersException
{
serialPort.addEventListener(listener);
}