本文整理匯總了Java中gnu.io.CommPort類的典型用法代碼示例。如果您正苦於以下問題:Java CommPort類的具體用法?Java CommPort怎麽用?Java CommPort使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CommPort類屬於gnu.io包,在下文中一共展示了CommPort類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: SerialComm
import gnu.io.CommPort; //導入依賴的package包/類
/**
* Constructor
*
* @param portName
* The name of the serial port
* @param listeners
* The listeners for incoming telegrams
* @throws Exception
*/
public SerialComm(String portName, ArrayList<EnoceanListener> listeners)
throws Exception {
this.listeners = listeners;
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
throw new Exception("Port is currently in use");
}
CommPort commPort = portIdentifier
.open(this.getClass().getName(), 2000); // timeout 2 s.
if (!(commPort instanceof SerialPort)) {
throw new Exception("Only serial port is supported");
}
port = commPort;
SerialPort serialPort = (SerialPort) commPort;
// 57600 bit/s, 8 bits, stop bit length 1, no parity bit
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
}
示例2: connect
import gnu.io.CommPort; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
public void connect() throws EpsonProjectorException {
try {
logger.debug("Open connection to serial port '{}'", serialPortName);
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, 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();
}
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
throw new EpsonProjectorException(e);
}
}
示例3: connect
import gnu.io.CommPort; //導入依賴的package包/類
void connect(String destination) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
if (portIdentifier.isCurrentlyOwned()) {
log.warning("Error: Port for Dynamixel-Communication is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialReader = new SerialReader(in);
serialPort.addEventListener(serialReader);
serialPort.notifyOnDataAvailable(true);
connected = true;
log.info("Connected to Dynamixel!");
} else {
log.warning("Error: Cannot connect to Dynamixel!");
}
}
}
示例4: connect
import gnu.io.CommPort; //導入依賴的package包/類
void connect(String destination) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
if (portIdentifier.isCurrentlyOwned()) {
log.warning("Error: Port for Pan-Tilt-Communication is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialPort.addEventListener(new SerialReader(in));
serialPort.notifyOnDataAvailable(true);
connected = true;
log.info("Connected to Pan-Tilt-Unit!");
} else {
log.warning("Error: Cannot connect to Pan-Tilt-Unit!");
}
}
}
示例5: connect
import gnu.io.CommPort; //導入依賴的package包/類
void connect(String destination) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
if (portIdentifier.isCurrentlyOwned()) {
log.warning("Error: Port for Dynamixel-Communication is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57142, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialPort.addEventListener(new SerialReader(in));
serialPort.notifyOnDataAvailable(true);
log.info("Connected to Dynamixel!");
} else {
log.warning("Error: Cannot connect to Dynamixel!");
}
}
}
示例6: connect
import gnu.io.CommPort; //導入依賴的package包/類
/** Method to connect to an available port */
@SuppressWarnings("static-access")
private void connect(String portName) throws Exception {
CommPortIdentifier commPortIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (commPortIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(References.BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
this.inputStream = serialPort.getInputStream();
this.outputStream = serialPort.getOutputStream();
References.SERIAL_READER = new SerialReader(this.inputStream);
readThread = new Thread(References.SERIAL_READER);
readThread.start();
} else {
System.out.println("Error: Only serial ports allowed");
}
}
}
示例7: open
import gnu.io.CommPort; //導入依賴的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();
}
}
示例8: RpLidarLowLevelDriver
import gnu.io.CommPort; //導入依賴的package包/類
/**
* Initializes serial connection
*
* @param portName Path to serial port
* @param listener Listener for in comming packets
* @throws Exception
*/
public RpLidarLowLevelDriver(final String portName, final RpLidarListener listener) throws Exception {
log.info("Opening port " + portName);
this.listener = listener;
//Configuration for Serial port operations
final CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
final CommPort commPort = portIdentifier.open("FOO", 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.setDTR(false); // lovely undocumented feature where if true the motor stops spinning
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
readThread = new ReadSerialThread();
new Thread(readThread).start();
}
示例9: connect
import gnu.io.CommPort; //導入依賴的package包/類
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
LOG.info("Found the " + portName + " port.");
if (portIdentifier.isCurrentlyOwned()) {
LOG.error("Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
LOG.info("Opened port " + portName);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.FLOWCONTROL_NONE);
printWriter = new PrintWriter(serialPort.getOutputStream());
bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
// For skipping AnA string
skipInput();
} else {
LOG.error("Only serial ports are handled by this example.");
}
}
}
示例10: connect
import gnu.io.CommPort; //導入依賴的package包/類
private void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned())
throw new PortInUseException();
CommPort commPort = portIdentifier.open(this.getClass().getName(), COMPORT_PORT);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(BAUD_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
new Thread(new SerialReader(in)).start();
new Thread(new SerialWriter(out)).start();
} else {
throw new PortUnreachableException("ERROR - Only serial ports are handled by this class");
}
}
示例11: handleEvent
import gnu.io.CommPort; //導入依賴的package包/類
public void handleEvent() {
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(SerialConf.WINDOWS_PORT);
if (portId.isCurrentlyOwned()) {
System.out.println("Port busy!");
} else {
CommPort commPort = portId.open("whatever it's name", SerialConf.TIME_OUT);
if (commPort instanceof SerialPort) {
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(SerialConf.BAUD,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
in = serialPort.getInputStream();
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);
} else {
commPort.close();
System.out.println("the port is not serial!");
}
}
} catch (Exception e) {
serialPort.close();
System.out.println("Error: SerialOpen!!!");
e.printStackTrace();
}
}
示例12: connect
import gnu.io.CommPort; //導入依賴的package包/類
@Override
public void connect() throws SwegonVentilationException {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
logger.debug("Swegon ventilation Serial Port message listener started");
} catch (Exception e) {
throw new SwegonVentilationException(e);
}
}
示例13: connect
import gnu.io.CommPort; //導入依賴的package包/類
@Override
public void connect() throws OpenEnergyMonitorException {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
logger.debug("Open Energy Monitor Serial Port message listener started");
} catch (Exception e) {
throw new OpenEnergyMonitorException(e);
}
}
示例14: connect
import gnu.io.CommPort; //導入依賴的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();
}
示例15: open
import gnu.io.CommPort; //導入依賴的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;
}
}