private void connectSerial() throws Exception {
logger.debug("Initializing serial port {}", serialPortName);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier
.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
} catch (UnsupportedCommOperationException unimportant) {
// We might have a perfectly usable PTY even if above operations are unsupported
};
inStream = new DataInputStream(serialPort.getInputStream());
outStream = serialPort.getOutputStream();
outStream.flush();
if (inStream.markSupported()) {
inStream.reset();
}
logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
dataListener = new DataListener();
dataListener.start();
logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
sendInitMessages();
} catch (NoSuchPortException e) {
logger.error("No such port: {}",serialPortName);
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
if (portList.hasMoreElements()) {
StringBuilder sb = new StringBuilder();
while(portList.hasMoreElements()){
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
sb.append(String.format("%s ",portId.getName()));
}
logger.error("The following communications ports are available: {}",
sb.toString().trim());
} else {
logger.error("There are no communications ports available");
}
logger.error("You may consider OpenHAB startup parameter [ -Dgnu.io.rxtx.SerialPorts={} ]",
serialPortName);
throw e;
}
}
java类gnu.io.CommPort的实例源码
PrimareSerialConnector.java 文件源码
项目:openhab-hdl
阅读 17
收藏 0
点赞 0
评论 0
RXTXCommPortConnectionFactory.java 文件源码
项目:fax4j
阅读 15
收藏 0
点赞 0
评论 0
/**
* Creates and returns the resource.
*
* @return The resource
*/
@Override
protected CommPortAdapter createResourceImpl()
{
//get COMM port name
String portName=this.getPortName();
//get timeout
int timeout=this.getTimeOut();
CommPort commPort=null;
try
{
//get COMM port identifier
CommPortIdentifier commPortIdentifier=this.getPortIdentifier(portName);
//open connection
commPort=commPortIdentifier.open("fax4j",timeout);
}
catch(Exception exception)
{
throw new FaxException("Unable to connect to COMM port: "+portName,exception);
}
//create adapter wrapper
CommPortAdapter adapter=new RXTXCommPortAdapter(commPort);
return adapter;
}
RXTXCommPortAdapter.java 文件源码
项目:fax4j
阅读 16
收藏 0
点赞 0
评论 0
/**
* This is the class constructor.
*
* @param commPort
* The COMM port
*/
public RXTXCommPortAdapter(CommPort commPort)
{
super();
//validate COMM port object provided
if(commPort==null)
{
throw new FaxException("COMM port object not provided.");
}
this.commPort=commPort;
}
RXTXCommPortAdapter.java 文件源码
项目:fax4j
阅读 16
收藏 0
点赞 0
评论 0
/**
* Closes the connection.
*/
@Override
protected void closeImpl()
{
//remove reference
CommPort port=this.commPort;
this.commPort=null;
//close connection
port.close();
}
IT100BridgeHandler.java 文件源码
项目:openhab2-addons
阅读 22
收藏 0
点赞 0
评论 0
@Override
public void openConnection() {
try {
logger.debug("openConnection(): Connecting to IT-100 ");
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);
setConnected(true);
} catch (NoSuchPortException noSuchPortException) {
logger.error("openConnection(): No Such Port Exception: {}", noSuchPortException.getMessage());
setConnected(false);
} catch (PortInUseException portInUseException) {
logger.error("openConnection(): Port in Use Exception: {}", portInUseException.getMessage());
setConnected(false);
} catch (UnsupportedCommOperationException unsupportedCommOperationException) {
logger.error("openConnection(): Unsupported Comm Operation Exception: {}",
unsupportedCommOperationException.getMessage());
setConnected(false);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.error("openConnection(): Unsupported Encoding Exception: {}",
unsupportedEncodingException.getMessage());
setConnected(false);
} catch (IOException ioException) {
logger.error("openConnection(): IO Exception: {}", ioException.getMessage());
setConnected(false);
}
}
RxtxChannel.java 文件源码
项目:netty4study
阅读 17
收藏 0
点赞 0
评论 0
@Override
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
final CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(remote.value());
final CommPort commPort = cpi.open(getClass().getName(), 1000);
deviceAddress = remote;
serialPort = (SerialPort) commPort;
}
SerialWriter.java 文件源码
项目:kkMulticopterFlashTool
阅读 17
收藏 0
点赞 0
评论 0
private void openPort() throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
CommPort commPort = portIdentifier.open("LightController",2000);
if ( commPort instanceof SerialPort )
{
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baud,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
this.out = serialPort.getOutputStream();
}
}
RS232.java 文件源码
项目:uia.comm4j
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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;
}
FpgaSerial.java 文件源码
项目:fpga-shovel-and-pickaxe
阅读 15
收藏 0
点赞 0
评论 0
private void connect (String portName, int threshold) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
appender.append("Error: Port is currently in use");
}
else
{
//from RXTX examples
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if (commPort instanceof SerialPort)
{
//set up serial port
serialPort = (SerialPort) commPort;
serialPort.enableReceiveTimeout(threshold);
serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_2,SerialPort.PARITY_NONE);
//start async reader
sr = new SerialReader(serialPort, server);
serailThread = new Thread(sr);
serailThread.start();
}
else
{
appender.append("Error: Only serial ports are handled by this example.");
}
}
}
Serial.java 文件源码
项目:fpga-shovel-and-pickaxe
阅读 32
收藏 0
点赞 0
评论 0
public void connect ( String portName, int threshold, int newLineAfter) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
serialPort = (SerialPort) commPort;
serialPort.enableReceiveTimeout(threshold);
serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_2,SerialPort.PARITY_NONE);
sr = new SerialReader(serialPort, newLineAfter, System.currentTimeMillis());
serialThread = new Thread(sr);
serialThread.start();
}
else
{
// arrayco
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}