本文整理匯總了Java中gnu.io.NoSuchPortException類的典型用法代碼示例。如果您正苦於以下問題:Java NoSuchPortException類的具體用法?Java NoSuchPortException怎麽用?Java NoSuchPortException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NoSuchPortException類屬於gnu.io包,在下文中一共展示了NoSuchPortException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connect
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@PostConstruct
public void connect() throws TooManyListenersException, NoSuchPortException {
log.info("Connection PostConstruct callback: connecting ...");
serial = new NRSerialPort(serialPortProperties.getPortName(), serialPortProperties.getBaudRate());
serial.connect();
if (serial.isConnected()) {
log.info("Connection opened!");
} else {
throw new RuntimeException("Is not possible to open connection in " + serialPortProperties.getPortName() + " port");
}
serial.addEventListener(this);
serial.notifyOnDataAvailable(true);
input = new BufferedReader(new InputStreamReader(serial.getInputStream()));
}
開發者ID:diegosep,項目名稱:spring-serial-port-connector,代碼行數:17,代碼來源:AbstractSpringSerialPortConnector.java
示例2: open
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@Override
public void open() {
try{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned())
System.out.println("Error: Port is currently in use");
else {
CommPort port = portIdentifier.open(this.getClass().getName(), getTimeout());//for now class name
if (port instanceof SerialPort){
serial = (SerialPort) port;
serial.setSerialPortParams(baudrate, SerialPort.DATABITS_8
, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serial.enableReceiveTimeout(timeout);
this.in = serial.getInputStream();
this.out = serial.getOutputStream();
isOpened = true;
}
else
System.out.println("Error: Only serial ports are handled");
}
}
catch (PortInUseException | UnsupportedCommOperationException | NoSuchPortException | IOException e) {
if(serial != null)
serial.close();
serial = null;
e.printStackTrace();
}
}
示例3: getPort
import gnu.io.NoSuchPortException; //導入依賴的package包/類
public static FMilaSerialPort getPort(int speed, String port) throws IOException {
SerialPort serialPort;
try {
CommPortIdentifier comm = CommPortIdentifier.getPortIdentifier(port);
if (comm.isCurrentlyOwned()) {
throw new IOException("StationInUseError");
}
serialPort = comm.open("4mila", 2000);
serialPort.setSerialPortParams(speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.disableReceiveTimeout();
serialPort.disableReceiveFraming();
serialPort.disableReceiveThreshold();
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnOutputEmpty(true);
}
catch (PortInUseException | UnsupportedCommOperationException | NoSuchPortException e) {
throw new IOException(e.getMessage(), e);
}
return new RXTXSerialPort(serialPort);
}
示例4: newLink
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@Override
public LinkDelegate newLink(SerialLinkConfig config)
throws NoSuchPortException, PortInUseException,
UnsupportedCommOperationException, IOException {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(config.getPort());
checkState(!portIdentifier.isCurrentlyOwned(),
"Port %s is currently in use", config.getPort());
final SerialPort serialPort = serialPort(config, portIdentifier);
StreamConnection connection = new StreamConnection(
serialPort.getInputStream(), serialPort.getOutputStream(),
config.getProto());
ConnectionBasedLink connectionBasedLink = new ConnectionBasedLink(
connection, config.getProto());
@SuppressWarnings("resource")
Link link = config.isQos() ? new QosLink(connectionBasedLink)
: connectionBasedLink;
waitForArdulink(config, connectionBasedLink);
return new LinkDelegate(link) {
@Override
public void close() throws IOException {
super.close();
serialPort.close();
}
};
}
示例5: openPort
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@Override
synchronized protected void openPort() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
if (port != null)
return;
log.info("Opening port: " + portName);
CommPortIdentifier ident = CommPortIdentifier.getPortIdentifier(portName);
port = ident.open("ModbusRtuClient on " + portName, 2000);
port.setOutputBufferSize(buffer.length);
port.setInputBufferSize(buffer.length);
try {
port.setSerialPortParams(baudRate, dataBits, stopBits, parity);
port.enableReceiveTimeout(timeout);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
close();
throw e;
}
log.info("Port opened: " + port.getName());
}
示例6: connect
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@Override
public void connect(String device) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(device);
CommPort commPort = portIdentifier
.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
out.flush();
if (in.markSupported()) {
in.reset();
}
readerThread = new SerialReader(in);
readerThread.start();
}
示例7: open
import gnu.io.NoSuchPortException; //導入依賴的package包/類
/**
* {@inheritDoc}
**/
public void open() {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
setSerialEventHandler(this);
connected = true;
} catch (NoSuchPortException noSuchPortException) {
logger.error("open(): No Such Port Exception: ", noSuchPortException);
connected = false;
} catch (PortInUseException portInUseException) {
logger.error("open(): Port in Use Exception: ", portInUseException);
connected = false;
} catch (UnsupportedCommOperationException unsupportedCommOperationException) {
logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
connected = false;
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
connected = false;
} catch (IOException ioException) {
logger.error("open(): IO Exception: ", ioException);
connected = false;
}
}
示例8: getSerialPort
import gnu.io.NoSuchPortException; //導入依賴的package包/類
private CommPortIdentifier getSerialPort() {
if (port != null) {
try {
return CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e) {
e.printStackTrace();
}
return null;
}
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
return currPortId;
}
}
}
return null;
}
示例9: connect
import gnu.io.NoSuchPortException; //導入依賴的package包/類
@Override
public void connect(String device)
throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(device);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
out.flush();
if (in.markSupported()) {
in.reset();
}
readerThread = new SerialReader(in);
readerThread.start();
}
示例10: open
import gnu.io.NoSuchPortException; //導入依賴的package包/類
/**
* {@inheritDoc}
**/
public void open() {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
setSerialEventHandler(this);
connected = true;
} catch (NoSuchPortException noSuchPortException) {
logger.error("open(): No Such Port Exception: ", noSuchPortException);
connected = false;
} catch (PortInUseException portInUseException) {
logger.error("open(): Port in Use Exception: ", portInUseException);
connected = false;
} catch (UnsupportedCommOperationException unsupportedCommOperationException) {
logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
connected = false;
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
connected = false;
} catch (IOException ioException) {
logger.error("open(): IO Exception: ", ioException);
connected = false;
}
}
示例11: SerialRXTXComm
import gnu.io.NoSuchPortException; //導入依賴的package包/類
public SerialRXTXComm(CommPortIdentifier portIdentifier, Layer3Base layer3) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException, TooManyListenersException{
if (portIdentifier.isCurrentlyOwned()) {
throw new IOException("Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),
TIME_OUT);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
inStream = serialPort.getInputStream();
outStream = serialPort.getOutputStream();
new SerialReceiver().start();
/*serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);*/
} else {
throw new IOException("This is not a serial port!.");
}
}
this.layer2 = new Layer2Serial(this, layer3);
}
示例12: connect
import gnu.io.NoSuchPortException; //導入依賴的package包/類
public void connect(String portName) throws NoSuchPortException {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
示例13: SerialTransport
import gnu.io.NoSuchPortException; //導入依賴的package包/類
public SerialTransport(String serialPortName) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException
{
CommPortIdentifier usablePort = CommPortIdentifier.getPortIdentifier(serialPortName);
commPort = (SerialPort)usablePort.open("SerialTransport", 0);
commPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
commPort.setDTR(true);
commPort.setRTS(true);
commPort.setEndOfInputChar((byte)0x0D);
}
示例14: openPort
import gnu.io.NoSuchPortException; //導入依賴的package包/類
/**
* 打開串口
*
* @param portName
* 端口名稱
* @param baudrate
* 波特率
* @return 串口對象
* @throws SerialPortParameterFailure
* 設置串口參數失敗
* @throws NotASerialPort
* 端口指向設備不是串口類型
* @throws NoSuchPort
* 沒有該端口對應的串口設備
* @throws PortInUse
* 端口已被占用
*/
public static final SerialPort openPort(String portName, int baudrate)
throws SerialPortParameterFailure, NotASerialPort, NoSuchPort,
PortInUse {
try {
// 通過端口名識別端口
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
// 打開端口,並給端口名字和一個timeout(打開操作的超時時間)
CommPort commPort = portIdentifier.open(portName, 2000);
// 判斷是不是串口
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
try {
// 設置一下串口的波特率等參數
serialPort.setSerialPortParams(baudrate,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
throw new SerialPortParameterFailure();
}
return serialPort;
} else {
// 不是串口
throw new NotASerialPort();
}
} catch (NoSuchPortException e1) {
throw new NoSuchPort();
} catch (PortInUseException e2) {
throw new PortInUse();
}
}
示例15: connect
import gnu.io.NoSuchPortException; //導入依賴的package包/類
public void connect(String port, int baudRate, int timeout) throws IOException {
scan();
try {
connect(CommPortIdentifier.getPortIdentifier(port), baudRate, timeout);
} catch (NoSuchPortException e) {
throw new IOException("No such port", e);
}
}