package com.intelligt.modbus.jlibmodbus.serial;

import java.util.List;

/*
 * Copyright (C) 2016 "Invertor" Factory", JSC
 * [http://www.sbp-invertor.ru]
 *
 * This file is part of JLibModbus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors: Vladislav Y. Kochedykov, software engineer.
 * email: vladislav.kochedykov@gmail.com
 */
public class SerialUtils {

    static private SerialPortAbstractFactory factory;

    static {
        factory = new SerialPortFactoryJSSC();
    }

    /**
     * @param factory - a concrete serial port factory instance
     * @since 1.2.1
     */
    static public void setSerialPortFactory(SerialPortAbstractFactory factory) {
        SerialUtils.factory = factory;
    }

    static public SerialPort createSerial(SerialParameters sp) throws SerialPortException {
        return factory.createSerial(sp);
    }

    static public List<String> getPortIdentifiers() {
        return factory.getPortIdentifiers();
    }

    static public String getConnectorVersion() {
        return factory.getVersion();
    }
}

@@ -932,253 +932,1720 @@
 ry;%0A
-%0A
     
+final 
 static 
-%7B%0A        factory = new SerialPortFactoryJSSC();%0A    %7D%0A%0A    /**%0A     * @param factory - a concrete s
+private SerialPortFactoryJSSC FACTORY_JSSC = new SerialPortFactoryJSSC();%0A    final static private SerialPortFactoryRXTX FACTORY_RXTX = new SerialPortFactoryRXTX();%0A    final static private SerialPortFactoryJSerialComm FACTORY_JSERIAL_COMM = new SerialPortFactoryJSerialComm();%0A    final static private SerialPortFactoryPJC FACTORY_PJC = new SerialPortFactoryPJC();%0A    final static private SerialPortFactoryAT FACTORY_AT = new SerialPortFactoryAT();%0A    @Deprecated%0A    final static private SerialPortFactoryJavaComm FACTORY_JAVA_COMM = new SerialPortFactoryJavaComm();%0A%0A    final static private SerialPortAbstractFactory%5B%5D SERIAL_FACTORIES = new SerialPortAbstractFactory%5B%5D %7B%0A            FACTORY_JSERIAL_COMM, FACTORY_JSSC, FACTORY_RXTX, FACTORY_PJC, FACTORY_AT, FACTORY_JAVA_COMM%7D;%0A%0A%0A    /*%0A    static public boolean isThingsDevice(Context context) %7B%0A        final PackageManager pm = context.getPackageManager();%0A        return pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED);%0A    %7D%0A    */%0A%0A    static %7B%0A        for (S
 erial
- port factory instance%0A     * @since 1.2.1%0A     */%0A    static public void setSerialPortFactory(SerialPortAbstractFactory factory
+PortAbstractFactory f : SERIAL_FACTORIES) %7B%0A            if (f.available()) %7B%0A                setSerialPortFactory(f);%0A                break;%0A            %7D%0A        %7D%0A    %7D%0A%0A    static public void setSerialPortFactoryJSSC() %7B%0A        setSerialPortFactory(FACTORY_JSSC);%0A    %7D%0A%0A    static public void setSerialPortFactoryRXTX() %7B%0A        setSerialPortFactory(FACTORY_RXTX);%0A    %7D%0A%0A    static public void setSerialPortFactoryJSerialComm() %7B%0A        setSerialPortFactory(FACTORY_JSERIAL_COMM);%0A    %7D%0A%0A    static public void setSerialPortFactoryPureJavaComm() %7B%0A        setSerialPortFactory(FACTORY_PJC);%0A    %7D%0A%0A    static public void setSerialPortFactoryAndroidThings(
 ) %7B%0A
@@ -2656,37 +2656,168 @@
     
+set
 Serial
-Utils.factory = factory
+PortFactory(FACTORY_AT);%0A    %7D%0A%0A    @Deprecated%0A    static public void setSerialPortFactoryJavaComm() %7B%0A        SerialUtils.factory = FACTORY_JAVA_COMM
 ;%0A  
@@ -2928,23 +2928,38 @@
  return 
-f
+getSerialPortF
 actory
+()
 .createS
@@ -3027,16 +3027,43 @@
 ifiers()
+ throws SerialPortException
  %7B%0A     
@@ -3068,31 +3068,46 @@
      return 
-f
+getSerialPortF
 actory
+()
 .getPortIden
@@ -3188,23 +3188,38 @@
  return 
-f
+getSerialPortF
 actory
+()
 .getVers
@@ -3230,11 +3230,350 @@
 );%0A    %7D
+%0A%0A    /**%0A     * @param factory - a concrete serial port factory instance%0A     * @since 1.2.1%0A     */%0A    static public void setSerialPortFactory(SerialPortAbstractFactory factory) %7B%0A        SerialUtils.factory = factory;%0A    %7D%0A%0A    static public SerialPortAbstractFactory getSerialPortFactory() %7B%0A        return SerialUtils.factory;%0A    %7D
 %0A%7D%0A

Add the automatic selection of a serial port connector.