SerialPortConnection.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:xbee-api 作者:
@SuppressWarnings("unchecked")
public void openSerialPort(String port, String appName, int timeout, int baudRate, int dataBits, int stopBits, int parity, int flowControl) throws PortInUseException, UnsupportedCommOperationException, TooManyListenersException, IOException, XBeeException {
    // Apparently you can't query for a specific port, but instead must iterate
    Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();

    CommPortIdentifier portId = null;

    boolean found = false;

    while (portList.hasMoreElements()) {

        portId = portList.nextElement();

        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

            //log.debug("Found port: " + portId.getName());

            if (portId.getName().equals(port)) {
                //log.debug("Using Port: " + portId.getName());
                found = true;
                break;
            }
        }
    }

    if (!found) {
        throw new XBeeException("Could not find port: " + port);
    }

    serialPort = (SerialPort) portId.open(appName, timeout);

    serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

    // activate the DATA_AVAILABLE notifier
    serialPort.notifyOnDataAvailable(true);

    // activate the OUTPUT_BUFFER_EMPTY notifier
    //serialPort.notifyOnOutputEmpty(true);

    serialPort.addEventListener(this);

    inputStream = serialPort.getInputStream();
    outputStream = new BufferedOutputStream(serialPort.getOutputStream());
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号