SerialConnection.java 文件源码

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

项目:ArduinoLight 作者:
/**
 * Tries to establish a serial connection with the given portId and baudRate and set the _serialOutputStream.
 * If the connection could be established, _open is set to true.
 * @param portId a CommPortIdentifier-object, used for identifying and connecting to a port.
 * @param baudRate This has to match the settings in the arduino-code. Recommended value: 256000.
 * @throws PortInUseException, IllegalArgumentException
 * @throws IllegalStateException if there is already a connection active
 */
public synchronized void open(CommPortIdentifier portId, int baudRate) throws PortInUseException
{
    if (_open)
        throw new IllegalStateException("This SerialConnection is already opened.");

    try
    {
        _serialPort = (SerialPort) portId.open(_APPNAME, _TIME_OUT); //throws PortInUse
        _serialPort.setSerialPortParams(baudRate,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        _serialOutputStream = new BufferedOutputStream(_serialPort.getOutputStream());
        _open = true;
        ShutdownHandler.getInstance().addShutdownListener(this);
        DebugConsole.print("SerialConnection", "open", "Connecting successful!");
    }
    catch (UnsupportedCommOperationException | IOException ex)
    {
        DebugConsole.print("SerialConnection", "open", ex.toString());
        throw new IllegalArgumentException(ex);
    }

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号