PrimareSerialConnector.java 文件源码

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

项目:openhab-hdl 作者:
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;
        }
    }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号