├── .gitignore ├── natives ├── jamierf.zip ├── rxtx-2.1-7-bins-r2.zip ├── librxtxSerial-2.2pre1.so ├── rxtxSerial32_RXTX-2.1-7.dll ├── mfz-rxtx-2.2-20081207-win-x64.zip ├── mfz-rxtx-2.2-20081207-win-x86.zip ├── mfz-rxtx-2.2-20081207-win-ia64.zip ├── mfz-rxtx-2.2-20081207-linux-i386.zip └── mfz-rxtx-2.2-20081207-linux-x86_64.zip ├── legacy ├── rxtx-2.1-7r2.zip ├── rxtx-2.2pre2-src.zip ├── rxtx-2.1-7-bins-r2.zip ├── rxtx_2.1.6.orig.tar.gz ├── rxtx_2.1.7.orig.tar.gz ├── rxtx_2.1.7r2.orig.tar.gz ├── rxtx_2.2pre2.orig.tar.gz └── RxTx20110203_unmodified.zip ├── src └── main │ ├── resources │ └── gnu │ │ └── io │ │ ├── rxtxSerial-arm.so │ │ ├── rxtxSerial-x86.so │ │ ├── rxtxSerial-amd64.dll │ │ ├── rxtxSerial-amd64.so │ │ └── rxtxSerial-x86.dll │ └── java │ ├── gnu │ └── io │ │ ├── RXTXInitializer.java │ │ ├── CommDriver.java │ │ ├── NoSuchPortException.java │ │ ├── PortInUseException.java │ │ ├── UnsupportedCommOperationException.java │ │ ├── CommPortEnumerator.java │ │ ├── SerialPortEventListener.java │ │ ├── CommPortOwnershipListener.java │ │ ├── RXTXVersion.java │ │ ├── CommPort.java │ │ ├── SerialPortEvent.java │ │ ├── SerialPort.java │ │ ├── CommPortIdentifier.java │ │ └── RXTXCommDriver.java │ └── name │ └── prokop │ └── bart │ └── rxtx │ ├── Demo1.java │ └── SystemProperties.java ├── docs └── RaspberryPi.md ├── README.md ├── pom.xml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | -------------------------------------------------------------------------------- /natives/jamierf.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/jamierf.zip -------------------------------------------------------------------------------- /legacy/rxtx-2.1-7r2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx-2.1-7r2.zip -------------------------------------------------------------------------------- /legacy/rxtx-2.2pre2-src.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx-2.2pre2-src.zip -------------------------------------------------------------------------------- /legacy/rxtx-2.1-7-bins-r2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx-2.1-7-bins-r2.zip -------------------------------------------------------------------------------- /legacy/rxtx_2.1.6.orig.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx_2.1.6.orig.tar.gz -------------------------------------------------------------------------------- /legacy/rxtx_2.1.7.orig.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx_2.1.7.orig.tar.gz -------------------------------------------------------------------------------- /legacy/rxtx_2.1.7r2.orig.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx_2.1.7r2.orig.tar.gz -------------------------------------------------------------------------------- /legacy/rxtx_2.2pre2.orig.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/rxtx_2.2pre2.orig.tar.gz -------------------------------------------------------------------------------- /natives/rxtx-2.1-7-bins-r2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/rxtx-2.1-7-bins-r2.zip -------------------------------------------------------------------------------- /natives/librxtxSerial-2.2pre1.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/librxtxSerial-2.2pre1.so -------------------------------------------------------------------------------- /legacy/RxTx20110203_unmodified.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/legacy/RxTx20110203_unmodified.zip -------------------------------------------------------------------------------- /natives/rxtxSerial32_RXTX-2.1-7.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/rxtxSerial32_RXTX-2.1-7.dll -------------------------------------------------------------------------------- /natives/mfz-rxtx-2.2-20081207-win-x64.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/mfz-rxtx-2.2-20081207-win-x64.zip -------------------------------------------------------------------------------- /natives/mfz-rxtx-2.2-20081207-win-x86.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/mfz-rxtx-2.2-20081207-win-x86.zip -------------------------------------------------------------------------------- /natives/mfz-rxtx-2.2-20081207-win-ia64.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/mfz-rxtx-2.2-20081207-win-ia64.zip -------------------------------------------------------------------------------- /src/main/resources/gnu/io/rxtxSerial-arm.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/src/main/resources/gnu/io/rxtxSerial-arm.so -------------------------------------------------------------------------------- /src/main/resources/gnu/io/rxtxSerial-x86.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/src/main/resources/gnu/io/rxtxSerial-x86.so -------------------------------------------------------------------------------- /natives/mfz-rxtx-2.2-20081207-linux-i386.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/mfz-rxtx-2.2-20081207-linux-i386.zip -------------------------------------------------------------------------------- /natives/mfz-rxtx-2.2-20081207-linux-x86_64.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/natives/mfz-rxtx-2.2-20081207-linux-x86_64.zip -------------------------------------------------------------------------------- /src/main/resources/gnu/io/rxtxSerial-amd64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/src/main/resources/gnu/io/rxtxSerial-amd64.dll -------------------------------------------------------------------------------- /src/main/resources/gnu/io/rxtxSerial-amd64.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/src/main/resources/gnu/io/rxtxSerial-amd64.so -------------------------------------------------------------------------------- /src/main/resources/gnu/io/rxtxSerial-x86.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bartprokop/rxtx/HEAD/src/main/resources/gnu/io/rxtxSerial-x86.dll -------------------------------------------------------------------------------- /docs/RaspberryPi.md: -------------------------------------------------------------------------------- 1 | # Tests on Raspberry Pi 2 | 3 | ``` 4 | pi@Malina2:~/fps $ java -cp drivers-1.0.1-SNAPSHOT-jar-with-dependencies.jar name.prokop.bart.rxtx.Demo1 5 | /tmp 6 | 32 7 | arm 8 | Linux 9 | 4.14.98+ 10 | lip 07, 2019 11:34:57 PM gnu.io.RXTXInitializer provideNativeLibraries 11 | INFO: Copied rxtxSerial-arm.so to /tmp/rxtxSerial-arm-2.2.2.so 12 | lip 07, 2019 11:34:58 PM gnu.io.RXTXInitializer 13 | INFO: Native lib Version = RXTX-2.2pre2 14 | lip 07, 2019 11:34:58 PM gnu.io.RXTXInitializer 15 | INFO: Java lib Version = RXTX-2.2pre2 16 | 1 port(ow):/dev/ttyUSB0 17 | ************************************************* 18 | 1 port(ow) szeregowych: 19 | /dev/ttyUSB0 otwieram OK 20 | 21 | 0 port(ow) rownoleglych: 22 | ``` 23 | 24 | ``` 25 | pi@Malina2:~/fps $ java -cp drivers-1.0.1-SNAPSHOT-jar-with-dependencies.jar name.prokop.bart.rxtx.Demo1 26 | /tmp 27 | 32 28 | arm 29 | Linux 30 | 4.14.98+ 31 | lip 07, 2019 11:39:25 PM gnu.io.RXTXInitializer provideNativeLibraries 32 | INFO: Native library rxtxSerial-arm.so already exists at /tmp/rxtxSerial-arm-2.2.2.so 33 | lip 07, 2019 11:39:26 PM gnu.io.RXTXInitializer 34 | INFO: Native lib Version = RXTX-2.2pre2 35 | lip 07, 2019 11:39:26 PM gnu.io.RXTXInitializer 36 | INFO: Java lib Version = RXTX-2.2pre2 37 | 1 port(ow):/dev/ttyUSB0 38 | ************************************************* 39 | 1 port(ow) szeregowych: 40 | /dev/ttyUSB0 otwieram OK 41 | 42 | 0 port(ow) rownoleglych: 43 | ``` 44 | 45 | That's it - Seems proper binary .so file is loaded on Raspberry Pi. Needs more tests. 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rxtx 2 | ==== 3 | 4 | Maven coordinates: 5 | 6 | ``` 7 | 8 | dev.prokop.rxtx 9 | rxtx 10 | 2.2.2 11 | 12 | ``` 13 | 14 | Serial Port rxtx library 15 | 16 | Version 2.1.8 is: 17 | - source code taken from rxtx-2.1-7r2.zip 18 | - added Maven POM 19 | - digitally signed with recognized certificate 20 | - compiled against Java 1.3 (as original rxtx) 21 | 22 | Version 2.2.0.pre2 is: 23 | - source code taken from rxtx-2.2pre2-src.zip 24 | - digitally signed with recognized certificate and timestamped 25 | - compiled against Java 1.3 26 | 27 | Version 2.2.0.20110203 is: 28 | - latest avaiable in CVS, snapshot from 2011.02.03 29 | - digitally signed with recognized certificate and timestamped 30 | - compiled against Java 1.3 31 | 32 | Currently I'm working on version 2.3.0: 33 | - added Findbugs build checks (107 deficiencies reported on Low threshold, 21 on Default, all fixed !!!) 34 | - Java 1.7 35 | - some new features.... 36 | 37 | 38 | This is an attemp to revive the old but very good rxtx library that allows for using serial ports in Java. I was using rxtx 2.1.7 for many years. But as my projects have evolved from IDE build through ant to Maven and JDK from 1.1 to 1.8 this was aparent that rxtx is unmaintained. So i tried to get the sources (now not easilly avaiable) of latest stable version (stable in terms of used in many production system) and continue that great piece of software on github. 39 | 40 | The intention is to release version 2.2.0 with very minimal changes to 2.1.7r2 codebase. Just make it clean Maven project and add digital signature for all classes (because of a need to use it in applets). Version 2.2.1 will undergo some refactoring to meet contemporary Java good practices. 41 | 42 | Resources: 43 | * https://code.google.com/p/create-lab-commons/source/browse/trunk/java/lib/rxtx/ 44 | 45 | > RXTX binary builds provided as a courtesy of Mfizz Inc. (http://mfizz.com/). 46 | > Please see http://mfizz.com/oss/rxtx-for-java for more information. 47 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/RXTXInitializer.java: -------------------------------------------------------------------------------- 1 | package gnu.io; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.util.logging.Level; 9 | import java.util.logging.Logger; 10 | 11 | /** 12 | * 13 | * @author Bart 14 | */ 15 | public class RXTXInitializer { 16 | 17 | private static final Logger LOGGER = Logger.getLogger(RXTXInitializer.class.getName()); 18 | private static final String RESOURCE_NAME; 19 | private final static String JAR_VERSION = RXTXVersion.getExpectedNativeVersion(); 20 | private final static String LIB_VERSION; 21 | private static final String BASE_NAME = "rxtxSerial"; 22 | private static final String OSNAME = System.getProperty("os.name"); 23 | private static final String OSARCH = System.getProperty("os.arch"); 24 | 25 | /** 26 | * Perform a crude check to make sure people don't mix versions of the Jar 27 | * and native lib 28 | * 29 | * Mixing the libs can create a nightmare. 30 | */ 31 | static { 32 | try { 33 | RESOURCE_NAME = resourceName(); 34 | provideNativeLibraries(); 35 | LIB_VERSION = RXTXVersion.nativeGetVersion(); 36 | LOGGER.log(Level.INFO, "Native lib Version = {0}", LIB_VERSION); 37 | LOGGER.log(Level.INFO, "Java lib Version = {0}", JAR_VERSION); 38 | if (!JAR_VERSION.equals(LIB_VERSION)) { 39 | LOGGER.warning("RXTX Version mismatch"); 40 | } 41 | } catch (IOException e) { 42 | throw new IllegalStateException("Cannot initialize native stuff", e); 43 | } 44 | } 45 | 46 | private static String resourceName() { 47 | String retVal = BASE_NAME; 48 | retVal += "-"; 49 | retVal += OSARCH; 50 | if (OSNAME.toLowerCase().contains("windows")) { 51 | retVal += ".dll"; 52 | } else { 53 | retVal += ".so"; 54 | } 55 | return retVal; 56 | } 57 | 58 | private static long copy(InputStream input, OutputStream output) throws IOException { 59 | final byte[] buffer = new byte[1024]; 60 | long count = 0; 61 | int n = 0; 62 | while (-1 != (n = input.read(buffer))) { 63 | output.write(buffer, 0, n); 64 | count += n; 65 | } 66 | return count; 67 | } 68 | 69 | private static void provideNativeLibraries() throws IOException { 70 | File outFile = new File(System.getProperty("java.io.tmpdir")); 71 | String prefix = RESOURCE_NAME.substring(0, RESOURCE_NAME.lastIndexOf(".")); 72 | String suffix = RESOURCE_NAME.substring(RESOURCE_NAME.lastIndexOf(".") + 1, RESOURCE_NAME.length()); 73 | outFile = new File(outFile, prefix + "-" + RXTXVersion.getVersion() + "." + suffix); 74 | if (!outFile.exists()) { 75 | try (InputStream is = RXTXVersion.class.getResourceAsStream(RESOURCE_NAME); 76 | FileOutputStream fos = new FileOutputStream(outFile)) { 77 | copy(is, fos); 78 | LOGGER.log(Level.INFO, "Copied {0} to {1}", new String[]{RESOURCE_NAME, outFile.toString()}); 79 | } 80 | } else { 81 | LOGGER.log(Level.INFO, "Native library {0} already exists at {1}", new String[]{RESOURCE_NAME, outFile.toString()}); 82 | } 83 | System.load(outFile.getAbsolutePath()); 84 | // no longer needed to remove it 85 | // outFile.deleteOnExit(); 86 | } 87 | 88 | public static void main(String... args) { 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/CommDriver.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | /** 61 | * @author Trent Jarvi 62 | * @author Bartłomiej P. Prokop 63 | * @version 2.3 64 | */ 65 | public interface CommDriver { 66 | 67 | default CommPort getCommPort(String portName) { 68 | return getCommPort(portName, CommPortIdentifier.PORT_SERIAL); 69 | } 70 | 71 | @Deprecated 72 | CommPort getCommPort(String portName, int portType); 73 | 74 | void initialize(); 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/NoSuchPortException.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | /** 61 | * The requested Port does not exist 62 | * 63 | * @author Trent Jarvi 64 | * @author by Alexander Graf 65 | * @author Bartłomiej P. Prokop 66 | * @version 2.3 67 | */ 68 | public final class NoSuchPortException extends Exception { 69 | 70 | NoSuchPortException(String str) { 71 | super(str); 72 | } 73 | 74 | public NoSuchPortException() { 75 | super(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/PortInUseException.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | /** 61 | * The port requested is currently in use 62 | * 63 | * @author Trent Jarvi 64 | * @author by Alexander Graf 65 | * @author Bartłomiej P. Prokop 66 | * @version 2.3 67 | */ 68 | public final class PortInUseException extends Exception { 69 | 70 | /** 71 | * the owner of the port requested. 72 | */ 73 | public String currentOwner; 74 | 75 | /** 76 | * create a instance of the Exception and store the current owner 77 | * 78 | * @param str detailed information about the current owner 79 | */ 80 | PortInUseException(String str) { 81 | super(str); 82 | currentOwner = str; 83 | } 84 | 85 | public PortInUseException() { 86 | super(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/UnsupportedCommOperationException.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | /** 61 | * Exception thrown when a method does not support the requested functionality. 62 | * 63 | * @author Trent Jarvi 64 | * @author by Alexander Graf 65 | * @author Bartłomiej P. Prokop 66 | * @version 2.3 67 | */ 68 | public final class UnsupportedCommOperationException extends Exception { 69 | 70 | /** 71 | * create an instances with no message about why the Exception was thrown. 72 | * 73 | * @since JDK1.0 74 | */ 75 | public UnsupportedCommOperationException() { 76 | super(); 77 | } 78 | 79 | /** 80 | * create an instance with a message about why the Exception was thrown. 81 | * 82 | * @param str A detailed message explaining the reason for the Exception. 83 | * @since JDK1.0 84 | */ 85 | public UnsupportedCommOperationException(String str) { 86 | super(str); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/CommPortEnumerator.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.util.Enumeration; 61 | 62 | /** 63 | * @author Trent Jarvi 64 | * @author Bartłomiej P. Prokop 65 | * @version 2.3 66 | */ 67 | class CommPortEnumerator implements Enumeration { 68 | 69 | private CommPortIdentifier index; 70 | 71 | @Override 72 | public CommPortIdentifier nextElement() { 73 | synchronized (CommPortIdentifier.Sync) { 74 | if (index != null) { 75 | index = index.next; 76 | } else { 77 | index = CommPortIdentifier.CommPortIndex; 78 | } 79 | return index; 80 | } 81 | } 82 | 83 | @Override 84 | public boolean hasMoreElements() { 85 | synchronized (CommPortIdentifier.Sync) { 86 | if (index != null) { 87 | return index.next != null; 88 | } else { 89 | return CommPortIdentifier.CommPortIndex != null; 90 | } 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/SerialPortEventListener.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.util.EventListener; 61 | 62 | /** 63 | * Observer interface which is informed about events happening on a 64 | * SerialPort. 65 | * 66 | * First, instances of SerialPortEventListener are registered on a 67 | * port using the SerialPort.addEventListener() method. Then the 68 | * desired events must be enabled on the port by calling 69 | * SerialPort.notifyOn*(true). 70 | * 71 | * If one of the enabled events occurred on a port, the serialEvent 72 | * method of the listener is called with an event object describing the event. 73 | * 74 | * @author Trent Jarvi 75 | * @author Bartłomiej P. Prokop 76 | * @version 2.3 77 | */ 78 | public interface SerialPortEventListener extends EventListener { 79 | 80 | /** 81 | * Receives enabled event notifications from the port the event listener is 82 | * registered to. 83 | * 84 | * @param event an event object describing the event 85 | */ 86 | public abstract void serialEvent(SerialPortEvent event); 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/CommPortOwnershipListener.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.util.*; 61 | 62 | /** 63 | * The CommPortOwnershipListener is an observer interface which 64 | * keeps track of the ownership of a port. 65 | * 66 | * The listener must be registered on a port via 67 | * CommPortIdentiefer.addPortOwnershipListener() first. Then it is 68 | * notified when other applications try to access the port or the ownership 69 | * changes. 70 | * 71 | * @author Trent Jarvi 72 | * @version 2.2 73 | * @since JDK1.0 74 | */ 75 | public interface CommPortOwnershipListener extends EventListener { 76 | 77 | /** 78 | * The port is owned by an application. 79 | */ 80 | public static final int PORT_OWNED = 1; 81 | /** 82 | * The pot is not owned by an application and is ready to be opened. 83 | */ 84 | public static final int PORT_UNOWNED = 2; 85 | /** 86 | * Another application requests to open the port. If this application 87 | * receives this event while holding the port and wants to give ownership to 88 | * the requesting application, then CommPort.close() should be 89 | * called as fast as possible. 90 | */ 91 | public static final int PORT_OWNERSHIP_REQUESTED = 3; 92 | 93 | /** 94 | * Receives change notifications about the port ownership. 95 | * 96 | * @param type one of the PORT_* constants indicating the 97 | * notification type. 98 | */ 99 | //TODO (by Alexander Graf): it would be nice to get the port identifier on 100 | // which the ownership event occured. This would enable us to register a 101 | // single listener implementation on multiple port identifiers 102 | public abstract void ownershipChange(int type); 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/RXTXVersion.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.3 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2009 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.util.logging.Logger; 61 | 62 | /** 63 | * A class to keep the current version in 64 | */ 65 | public class RXTXVersion { 66 | 67 | private static final Logger LOGGER = Logger.getLogger(RXTXVersion.class.getName()); 68 | private static final String RXTX_VERSION = "2.2.2"; 69 | private static final String EXPECTED_NATIVE_VERSION_RPI = "RXTX-2.2pre2"; 70 | private static final String EXPECTED_NATIVE_VERSION_WIN = "RXTX-2.2-20081207 Cloudhopper Build rxtx.cloudhopper.net"; 71 | 72 | static void ensureNativeCodeLoaded() { 73 | try { 74 | Class.forName("gnu.io.RXTXInitializer"); 75 | } catch (ClassNotFoundException cnfe) { 76 | throw new IllegalStateException("RXTX not initialized", cnfe); 77 | } 78 | if (!getExpectedNativeVersion().equals(nativeGetVersion())) { 79 | LOGGER.warning("Native libraries mismatch"); 80 | } 81 | } 82 | 83 | public static String getOsName() { 84 | return System.getProperty("os.name"); 85 | } 86 | 87 | public static String getOsArch() { 88 | return System.getProperty("os.arch"); 89 | } 90 | 91 | /** 92 | * static method to return the current version of RXTX unique to RXTX. 93 | * 94 | * @return a string representing the version "RXTX-1.4-9" 95 | */ 96 | public static String getVersion() { 97 | return RXTX_VERSION; 98 | } 99 | 100 | /** 101 | * static method to return the expected version of native RXTX dll/so. 102 | * 103 | * @return a string representing the version "RXTX-1.4-9" 104 | */ 105 | public static String getExpectedNativeVersion() { 106 | if ("arm".equals(getOsArch())) { 107 | return EXPECTED_NATIVE_VERSION_RPI; 108 | } 109 | return EXPECTED_NATIVE_VERSION_WIN; 110 | } 111 | 112 | public static native String nativeGetVersion(); 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/CommPort.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.io.InputStream; 61 | import java.io.OutputStream; 62 | import java.io.IOException; 63 | import java.util.logging.Level; 64 | import java.util.logging.Logger; 65 | 66 | /** 67 | * Represents an exclusively owned, opened port. 68 | * 69 | * In contrast to 70 | * CommPortIdentifier this class represents an opened port which is 71 | * owned by this application. The application can therefor access the ports 72 | * properties. 73 | * 74 | * The 75 | * CommPort class is the base class for all port types. 76 | * 77 | * @author Trent Jarvi 78 | * @author Bartłomiej P. Prokop 79 | * @version 2.3 80 | */ 81 | public abstract class CommPort { 82 | 83 | private static final Logger LOGGER = Logger.getLogger(CommPort.class.getName()); 84 | 85 | private final String name; 86 | 87 | public abstract void enableReceiveFraming(int f) throws UnsupportedCommOperationException; 88 | 89 | public abstract void disableReceiveFraming(); 90 | 91 | public abstract boolean isReceiveFramingEnabled(); 92 | 93 | public abstract int getReceiveFramingByte(); 94 | 95 | public abstract void disableReceiveTimeout(); 96 | 97 | public abstract void enableReceiveTimeout(int time) throws UnsupportedCommOperationException; 98 | 99 | public abstract boolean isReceiveTimeoutEnabled(); 100 | 101 | public abstract int getReceiveTimeout(); 102 | 103 | public abstract void enableReceiveThreshold(int thresh) throws UnsupportedCommOperationException; 104 | 105 | public abstract void disableReceiveThreshold(); 106 | 107 | public abstract int getReceiveThreshold(); 108 | 109 | public abstract boolean isReceiveThresholdEnabled(); 110 | 111 | public abstract void setInputBufferSize(int size); 112 | 113 | public abstract int getInputBufferSize(); 114 | 115 | public abstract void setOutputBufferSize(int size); 116 | 117 | public abstract int getOutputBufferSize(); 118 | 119 | public CommPort(String name) { 120 | this.name = name; 121 | } 122 | 123 | public void close() { 124 | LOGGER.log(Level.FINE, "Closing port {0}", name); 125 | try { 126 | CommPortIdentifier cp = CommPortIdentifier.getPortIdentifier(this); 127 | if (cp != null) { 128 | CommPortIdentifier.getPortIdentifier(this).internalClosePort(); 129 | } 130 | } catch (NoSuchPortException e) { 131 | } 132 | } 133 | 134 | public abstract InputStream getInputStream() throws IOException; 135 | 136 | public abstract OutputStream getOutputStream() throws IOException; 137 | 138 | public String getName() { 139 | return name; 140 | } 141 | 142 | @Override 143 | public String toString() { 144 | return this.getName(); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/name/prokop/bart/rxtx/Demo1.java: -------------------------------------------------------------------------------- 1 | package name.prokop.bart.rxtx; 2 | 3 | import gnu.io.CommPort; 4 | import gnu.io.CommPortIdentifier; 5 | import gnu.io.NoSuchPortException; 6 | import gnu.io.PortInUseException; 7 | import gnu.io.SerialPort; 8 | import java.io.IOException; 9 | import java.util.Enumeration; 10 | 11 | public class Demo1 { 12 | 13 | public static void main(String... args) { 14 | System.out.println(System.getProperty("java.io.tmpdir")); 15 | System.out.println(System.getProperty("sun.arch.data.model")); 16 | 17 | System.out.println(System.getProperty("os.arch")); 18 | System.out.println(System.getProperty("os.name")); 19 | System.out.println(System.getProperty("os.version")); 20 | 21 | String[] portList = getPortList(); 22 | System.out.print(portList.length); 23 | System.out.print(" port(ow):"); 24 | for (int i = 0; i < portList.length; i++) { 25 | System.out.print(portList[i] + " "); 26 | } 27 | System.out.println(); 28 | 29 | System.out.println("*************************************************"); 30 | 31 | portList = getSerialPortList(); 32 | System.out.print(portList.length); 33 | System.out.println(" port(ow) szeregowych:"); 34 | for (int i = 0; i < portList.length; i++) { 35 | System.out.print(portList[i] + " otwieram "); 36 | try { 37 | CommPort p = getSerialPort(portList[i]); 38 | p.close(); 39 | System.out.print("OK"); 40 | } catch (IOException e) { 41 | System.out.print("nie udało się: " + e.getMessage()); 42 | } 43 | System.out.println(); 44 | } 45 | System.out.println(); 46 | 47 | portList = getParallelPortList(); 48 | System.out.print(portList.length); 49 | System.out.println(" port(ow) rownoleglych:"); 50 | for (int i = 0; i < portList.length; i++) { 51 | System.out.print(" " + portList[i]); 52 | } 53 | System.out.println(); 54 | } 55 | 56 | public static String[] getPortList() { 57 | Enumeration portList = CommPortIdentifier.getPortIdentifiers(); 58 | 59 | int portCount = 0; 60 | while (portList.hasMoreElements()) { 61 | portList.nextElement(); 62 | portCount++; 63 | } 64 | 65 | String[] retVal = new String[portCount]; 66 | 67 | portCount = 0; 68 | portList = CommPortIdentifier.getPortIdentifiers(); 69 | while (portList.hasMoreElements()) { 70 | CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); 71 | retVal[portCount++] = portId.getName(); 72 | } 73 | 74 | return retVal; 75 | } 76 | 77 | /** 78 | * Zwraca liste portow szeregowych 79 | * 80 | * @return Zwraca liste portow szeregowych. Zwracana jest tablica string�w. 81 | * Stringi te mo�na u�y� w funkcji getSerialPort 82 | */ 83 | public static String[] getSerialPortList() { 84 | Enumeration portList; 85 | portList = CommPortIdentifier.getPortIdentifiers(); 86 | 87 | int serialPortCount = 0; 88 | while (portList.hasMoreElements()) { 89 | CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); 90 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 91 | serialPortCount++; 92 | } 93 | } 94 | 95 | String[] retVal = new String[serialPortCount]; 96 | 97 | serialPortCount = 0; 98 | portList = CommPortIdentifier.getPortIdentifiers(); 99 | while (portList.hasMoreElements()) { 100 | CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); 101 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 102 | retVal[serialPortCount++] = portId.getName(); 103 | } 104 | } 105 | 106 | return retVal; 107 | } 108 | 109 | /** 110 | * Zwraca liste portow rownoleglych 111 | * 112 | * @return Zwraca liste portow rownoleglych 113 | */ 114 | public static String[] getParallelPortList() { 115 | Enumeration portList; 116 | portList = CommPortIdentifier.getPortIdentifiers(); 117 | 118 | int serialPortCount = 0; 119 | while (portList.hasMoreElements()) { 120 | CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); 121 | if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) { 122 | serialPortCount++; 123 | } 124 | } 125 | 126 | String[] retVal = new String[serialPortCount]; 127 | 128 | serialPortCount = 0; 129 | portList = CommPortIdentifier.getPortIdentifiers(); 130 | while (portList.hasMoreElements()) { 131 | CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); 132 | if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) { 133 | retVal[serialPortCount++] = portId.getName(); 134 | } 135 | } 136 | 137 | return retVal; 138 | } 139 | 140 | /** 141 | * Zwraca otwarty port szeregowy o zadanej nazwie 142 | * 143 | * @return Zwraca port szeregowy o zadanej nazwie 144 | * @param portName Nazwa portu 145 | * @throws IOException W przypadku, gdy nie uda�o si� otworzy� portu 146 | * szeregowego, wraz z opisem. 147 | */ 148 | public static SerialPort getSerialPort(String portName) throws IOException { 149 | try { 150 | CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName); 151 | //Enumeration portList = CommPortIdentifier.getPortIdentifiers(); 152 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 153 | if (portId.getName().equals(portName)) { 154 | return (SerialPort) portId.open("Bart Prokop Comm Helper", 3000); 155 | } 156 | } 157 | } catch (NoSuchPortException e) { 158 | throw new IOException("NoSuchPortException @ " + portName, e); 159 | } catch (PortInUseException e) { 160 | throw new IOException("PortInUseException @ " + portName, e); 161 | } 162 | 163 | throw new IOException("To nie jest port szeregowy"); 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/SerialPortEvent.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.util.*; 61 | 62 | /** 63 | * @author Trent Jarvi 64 | * @author Bartłomiej P. Prokop 65 | * @version 2.3 66 | */ 67 | public class SerialPortEvent extends EventObject { 68 | 69 | /** 70 | * The DATA_AVAILABLE port event notifies that new data was received on the 71 | * port. When this event type is received, the user will typically schedule 72 | * a read on the input stream. 73 | */ 74 | public static final int DATA_AVAILABLE = 1; 75 | /** 76 | * The OUTPUT_BUFFER_EMPTY port event notifies that all data in the ports 77 | * output buffer was processed. The user might use this event to continue 78 | * writing data to the ports output stream. 79 | */ 80 | public static final int OUTPUT_BUFFER_EMPTY = 2; 81 | /** 82 | * The CTS port event is triggered when the Clear To Send line on the port 83 | * changes its logic level. 84 | */ 85 | public static final int CTS = 3; 86 | /** 87 | * The DSR port event is triggered when the Data Set Ready line on the port 88 | * changes its logic level. 89 | */ 90 | public static final int DSR = 4; 91 | /** 92 | * The RI port event is triggered when the Ring Indicator line on the port 93 | * changes its logic level. 94 | */ 95 | public static final int RI = 5; 96 | /** 97 | * The CD port event is triggered when the Data Carrier Detect line on the 98 | * port changes its logic level. 99 | */ 100 | public static final int CD = 6; 101 | /** 102 | * The OE port event signals an overrun error. This event is triggered, when 103 | * the port hardware receives data and the application does not read it or 104 | * does not read it fast enough. As a result a buffer located in hardware 105 | * and/or the driver overflows and data is lost. 106 | */ 107 | public static final int OE = 7; 108 | /** 109 | * The PE port event signals a parity error. This event is triggered when 110 | * the port is configured to use parity bits and a datum with wrong parity 111 | * value is received. This means it is very likely that a datum with wrong 112 | * value has been received. 113 | */ 114 | public static final int PE = 8; 115 | /** 116 | * The FE port event signals a framing error. 117 | */ 118 | public static final int FE = 9; 119 | /** 120 | * The BI port event signals a break interrupt. 121 | */ 122 | public static final int BI = 10; 123 | 124 | private final boolean oldValue; 125 | private final boolean newValue; 126 | private final int eventType; 127 | /*public int eventType =0; depricated */ 128 | 129 | /** 130 | * Creates a new SerialPortEvent of specified event type. 131 | * 132 | * @param srcport the port which is associated with the event 133 | * @param eventType the type of the event 134 | * @param oldValue the value of the signal before the event 135 | * @param newValue the value of the signal after the event 136 | */ 137 | public SerialPortEvent(SerialPort srcport, int eventType, boolean oldValue, boolean newValue) { 138 | super(srcport); 139 | this.oldValue = oldValue; 140 | this.newValue = newValue; 141 | this.eventType = eventType; 142 | } 143 | 144 | /** 145 | * Returns the type of the event which occurred. 146 | * 147 | * @return the event type 148 | */ 149 | public int getEventType() { 150 | return eventType; 151 | } 152 | 153 | /** 154 | * Returns the value of the event context object after the event occurred. 155 | * For CTS, DSR this is the new logic level of the CTS or DSR line 156 | * respectively. 157 | * 158 | * @return the new value 159 | */ 160 | //TODO (by Alexander Graf): check wich voltage level corresponds to which boolean value 161 | public boolean getNewValue() { 162 | return newValue; 163 | } 164 | 165 | /** 166 | * Returns the value of the event context object before the event occurred. 167 | * For CTS, DSR this is the old logic level of the CTS or DSR line 168 | * respectively. 169 | * 170 | * @return the old value 171 | */ 172 | public boolean getOldValue() { 173 | return oldValue; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | name.prokop.bart.rxtx 7 | rxtx 8 | 2.2.2 9 | jar 10 | 11 | RXTX 12 | Serial port library for Java with embedded natives 13 | https://github.com/bartprokop/rxtx 14 | 15 | 16 | 17 | LGPL v 2.1 18 | http://www.gnu.org/licenses/lgpl.txt 19 | 20 | 21 | 22 | 23 | 24 | Bartłomiej Prokop 25 | prokop.bart@gmail.com 26 | +1 27 | https://github.com/bartprokop 28 | 29 | 30 | 31 | 32 | scm:git:git@github.com:bartprokop/rxtx.git 33 | scm:git:git@github.com:bartprokop/rxtx.git 34 | https://github.com/bartprokop/rxtx 35 | 36 | 37 | 38 | UTF-8 39 | 40 | 41 | 42 | 43 | ossrh 44 | https://oss.sonatype.org/content/repositories/snapshots 45 | 46 | 47 | bart.prokop.name 48 | ftp://ftp.internetdsl.pl/m2-sites/${project.artifactId}-${project.version} 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.wagon 56 | wagon-ftp 57 | 2.4 58 | 59 | 60 | 61 | 62 | maven-compiler-plugin 63 | 3.1 64 | 65 | 1.8 66 | 1.8 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | findbugs 75 | 76 | 77 | 78 | org.codehaus.mojo 79 | findbugs-maven-plugin 80 | 2.5.2 81 | 82 | Max 83 | 84 | Default 85 | true 86 | 87 | 88 | 89 | compile 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | release 101 | 102 | 103 | 104 | maven-source-plugin 105 | 2.2.1 106 | 107 | 108 | attach-sources 109 | 110 | jar 111 | 112 | 113 | 114 | 115 | 116 | maven-javadoc-plugin 117 | 2.9.1 118 | 119 | 120 | attach-javadocs 121 | 122 | jar 123 | 124 | 125 | 126 | 127 | 128 | org.apache.maven.plugins 129 | maven-gpg-plugin 130 | 1.5 131 | 132 | 133 | sign-artifacts 134 | verify 135 | 136 | sign 137 | 138 | 139 | 140 | 141 | 163 | 164 | org.sonatype.plugins 165 | nexus-staging-maven-plugin 166 | 1.6.2 167 | true 168 | 169 | ossrh 170 | https://oss.sonatype.org/ 171 | true 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/SerialPort.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.io.*; 61 | import java.util.*; 62 | 63 | /** 64 | * @author Trent Jarvi 65 | * @author Bartłomiej P. Prokop 66 | * @version 2.3 67 | */ 68 | public abstract class SerialPort extends CommPort { 69 | 70 | public static final int DATABITS_5 = 5; 71 | public static final int DATABITS_6 = 6; 72 | public static final int DATABITS_7 = 7; 73 | public static final int DATABITS_8 = 8; 74 | public static final int PARITY_NONE = 0; 75 | public static final int PARITY_ODD = 1; 76 | public static final int PARITY_EVEN = 2; 77 | public static final int PARITY_MARK = 3; 78 | public static final int PARITY_SPACE = 4; 79 | public static final int STOPBITS_1 = 1; 80 | public static final int STOPBITS_2 = 2; 81 | public static final int STOPBITS_1_5 = 3; 82 | public static final int FLOWCONTROL_NONE = 0; 83 | public static final int FLOWCONTROL_RTSCTS_IN = 1; 84 | public static final int FLOWCONTROL_RTSCTS_OUT = 2; 85 | public static final int FLOWCONTROL_XONXOFF_IN = 4; 86 | public static final int FLOWCONTROL_XONXOFF_OUT = 8; 87 | 88 | public abstract void setSerialPortParams(int b, int d, int s, int p) throws UnsupportedCommOperationException; 89 | 90 | public abstract int getBaudRate(); 91 | 92 | public abstract int getDataBits(); 93 | 94 | public abstract int getStopBits(); 95 | 96 | public abstract int getParity(); 97 | 98 | public abstract void setFlowControlMode(int flowcontrol) throws UnsupportedCommOperationException; 99 | 100 | public abstract int getFlowControlMode(); 101 | 102 | public abstract boolean isDTR(); 103 | 104 | public abstract void setDTR(boolean state); 105 | 106 | public abstract void setRTS(boolean state); 107 | 108 | public abstract boolean isCTS(); 109 | 110 | public abstract boolean isDSR(); 111 | 112 | public abstract boolean isCD(); 113 | 114 | public abstract boolean isRI(); 115 | 116 | public abstract boolean isRTS(); 117 | 118 | public abstract void sendBreak(int duration); 119 | 120 | public abstract void addEventListener(SerialPortEventListener lsnr) throws TooManyListenersException; 121 | 122 | public abstract void removeEventListener(); 123 | 124 | public abstract void notifyOnDataAvailable(boolean enable); 125 | 126 | public abstract void notifyOnOutputEmpty(boolean enable); 127 | 128 | public abstract void notifyOnCTS(boolean enable); 129 | 130 | public abstract void notifyOnDSR(boolean enable); 131 | 132 | public abstract void notifyOnRingIndicator(boolean enable); 133 | 134 | public abstract void notifyOnCarrierDetect(boolean enable); 135 | 136 | public abstract void notifyOnOverrunError(boolean enable); 137 | 138 | public abstract void notifyOnParityError(boolean enable); 139 | 140 | public abstract void notifyOnFramingError(boolean enable); 141 | 142 | public abstract void notifyOnBreakInterrupt(boolean enable); 143 | 144 | public abstract byte getParityErrorChar() throws UnsupportedCommOperationException; 145 | 146 | public abstract boolean setParityErrorChar(byte b) throws UnsupportedCommOperationException; 147 | 148 | public abstract byte getEndOfInputChar() throws UnsupportedCommOperationException; 149 | 150 | public abstract boolean setEndOfInputChar(byte b) throws UnsupportedCommOperationException; 151 | 152 | public abstract boolean setUARTType(String type, boolean test) throws UnsupportedCommOperationException; 153 | 154 | public abstract String getUARTType() throws UnsupportedCommOperationException; 155 | 156 | public abstract boolean setBaudBase(int BaudBase) throws UnsupportedCommOperationException, IOException; 157 | 158 | public abstract int getBaudBase() throws UnsupportedCommOperationException, IOException; 159 | 160 | public abstract boolean setDivisor(int Divisor) throws UnsupportedCommOperationException, IOException; 161 | 162 | public abstract int getDivisor() throws UnsupportedCommOperationException, IOException; 163 | 164 | public abstract boolean setLowLatency() throws UnsupportedCommOperationException; 165 | 166 | public abstract boolean getLowLatency() throws UnsupportedCommOperationException; 167 | 168 | public abstract boolean setCallOutHangup(boolean NoHup) throws UnsupportedCommOperationException; 169 | 170 | public abstract boolean getCallOutHangup() throws UnsupportedCommOperationException; 171 | 172 | public SerialPort(String portName) { 173 | super(portName); 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /src/main/java/name/prokop/bart/rxtx/SystemProperties.java: -------------------------------------------------------------------------------- 1 | package name.prokop.bart.rxtx; 2 | 3 | import java.awt.*; 4 | import javax.swing.*; 5 | import javax.swing.border.EmptyBorder; 6 | import java.util.*; 7 | 8 | /** 9 | * A small GUId app. that shows many system and environment properties. Designed 10 | * to be compatible with Java 1.4+ (hence many requirements like no foreach, no 11 | * generics, no StringBuilder..). 12 | * 13 | * @author Andrew Thompson 14 | * @version 2008-06-29 15 | */ 16 | class SystemProperties { 17 | 18 | static String sentence = "The quick brown fox jumped over the lazy dog."; 19 | static String sep = System.getProperty("line.separator"); 20 | static String fontText 21 | = sentence 22 | + sep 23 | + sentence.toUpperCase() 24 | + sep 25 | + "0123456789 !@#$%^&*()_+ []\\;',./ {}|:\"<>?"; 26 | 27 | static String[] convertObjectToSortedStringArray(Object[] unsorted) { 28 | String[] sorted = new String[unsorted.length]; 29 | for (int ii = 0; ii < sorted.length; ii++) { 30 | sorted[ii] = (String) unsorted[ii]; 31 | } 32 | Arrays.sort(sorted); 33 | return sorted; 34 | } 35 | 36 | static String dataPairToTableRow(String property, Object value) { 37 | String val = valueToString(property, value); 38 | return "" 39 | + "" 40 | + "" 41 | + property 42 | + "" 43 | + "" 44 | + "" 45 | + val 46 | + "" 47 | + ""; 48 | } 49 | 50 | static String valueToString(String property, Object value) { 51 | if (value instanceof Color) { 52 | Color color = (Color) value; 53 | String converted 54 | = "
" 60 | + value.toString() 61 | + "
"; 62 | return converted; 63 | } else if (property.toLowerCase().endsWith("path") 64 | || property.toLowerCase().endsWith("dirs")) { 65 | return delimitedToHtmlList( 66 | (String) value, 67 | System.getProperty("path.separator")); 68 | } else { 69 | return value.toString(); 70 | } 71 | } 72 | 73 | static String delimitedToHtmlList(String values, String delimiter) { 74 | String[] parts = values.split(delimiter); 75 | StringBuffer sb = new StringBuffer(); 76 | sb.append("
    "); 77 | for (int ii = 0; ii < parts.length; ii++) { 78 | sb.append("
  1. "); 79 | sb.append(parts[ii]); 80 | sb.append("
  2. "); 81 | } 82 | return sb.toString(); 83 | } 84 | 85 | static Component getExampleOfFont(String fontFamily) { 86 | Font font = new Font(fontFamily, Font.PLAIN, 24); 87 | JTextArea ta = new JTextArea(); 88 | ta.setFont(font); 89 | ta.setText(fontText); 90 | ta.setEditable(false); 91 | // don't allow these to get focus, as it 92 | // interferes with desired scroll behavior 93 | ta.setFocusable(false); 94 | return ta; 95 | } 96 | 97 | static public JScrollPane getOutputWidgetForContent(String content) { 98 | JEditorPane op = new JEditorPane(); 99 | op.setContentType("text/html"); 100 | op.setEditable(false); 101 | 102 | op.setText(content); 103 | 104 | return new JScrollPane(op); 105 | } 106 | 107 | public static void main(String[] args) { 108 | JTabbedPane tabPane = new JTabbedPane(); 109 | StringBuffer sb; 110 | String header = ""; 111 | 112 | sb = new StringBuffer(header); 113 | Properties prop = System.getProperties(); 114 | String[] propStrings = convertObjectToSortedStringArray( 115 | prop.stringPropertyNames().toArray()); 116 | for (int ii = 0; ii < propStrings.length; ii++) { 117 | sb.append( 118 | dataPairToTableRow( 119 | propStrings[ii], 120 | System.getProperty(propStrings[ii]))); 121 | } 122 | tabPane.addTab( 123 | "System", 124 | getOutputWidgetForContent(sb.toString())); 125 | 126 | sb = new StringBuffer(header); 127 | Map environment = System.getenv(); 128 | String[] envStrings = convertObjectToSortedStringArray( 129 | environment.keySet().toArray()); 130 | for (int ii = 0; ii < envStrings.length; ii++) { 131 | sb.append( 132 | dataPairToTableRow( 133 | envStrings[ii], 134 | environment.get(envStrings[ii]))); 135 | } 136 | tabPane.addTab( 137 | "Environment", 138 | getOutputWidgetForContent(sb.toString())); 139 | 140 | sb = new StringBuffer(header); 141 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 142 | GraphicsDevice[] gs = ge.getScreenDevices(); 143 | for (int j = 0; j < gs.length; j++) { 144 | GraphicsDevice gd = gs[j]; 145 | sb.append( 146 | dataPairToTableRow( 147 | "Device " + j, 148 | gd.toString() 149 | + " " 150 | + gd.getIDstring())); 151 | GraphicsConfiguration[] gc 152 | = gd.getConfigurations(); 153 | for (int i = 0; i < gc.length; i++) { 154 | sb.append( 155 | dataPairToTableRow( 156 | "Config " 157 | + i, 158 | (int) gc[i].getBounds().getWidth() 159 | + "x" 160 | + (int) gc[i].getBounds().getHeight() 161 | + " " 162 | + gc[i].getColorModel() 163 | + ", " 164 | + " Accelerated: " 165 | + gc[i].getImageCapabilities().isAccelerated() 166 | + " True Volatile: " 167 | + gc[i].getImageCapabilities().isTrueVolatile())); 168 | } 169 | } 170 | tabPane.addTab( 171 | "Graphics Environment", 172 | getOutputWidgetForContent(sb.toString())); 173 | 174 | String[] fonts = ge.getAvailableFontFamilyNames(); 175 | JPanel fontTable = new JPanel(new BorderLayout(3, 1)); 176 | // to enable key based scrolling in the font panel 177 | fontTable.setFocusable(true); 178 | JPanel fontNameCol = new JPanel(new GridLayout(0, 1, 2, 2)); 179 | JPanel fontExampleCol = new JPanel(new GridLayout(0, 1, 2, 2)); 180 | fontTable.add(fontNameCol, BorderLayout.WEST); 181 | fontTable.add(fontExampleCol, BorderLayout.CENTER); 182 | for (int ii = 0; ii < fonts.length; ii++) { 183 | fontNameCol.add(new JLabel(fonts[ii])); 184 | fontExampleCol.add(getExampleOfFont(fonts[ii])); 185 | } 186 | tabPane.add("Fonts", new JScrollPane(fontTable)); 187 | 188 | sb = new StringBuffer(header); 189 | 190 | sb.append(""); 191 | sb.append(""); 192 | sb.append(""); 195 | sb.append(""); 198 | sb.append(""); 201 | sb.append(""); 204 | sb.append(""); 205 | sb.append(""); 206 | 207 | Locale[] locales = Locale.getAvailableLocales(); 208 | SortableLocale[] sortableLocale = new SortableLocale[locales.length]; 209 | for (int ii = 0; ii < locales.length; ii++) { 210 | sortableLocale[ii] = new SortableLocale(locales[ii]); 211 | } 212 | Arrays.sort(sortableLocale); 213 | for (int ii = 0; ii < locales.length; ii++) { 214 | String prefix = ""; 215 | String suffix = ""; 216 | Locale locale = sortableLocale[ii].getLocale(); 217 | if (locale.equals(Locale.getDefault())) { 218 | prefix = ""; 219 | suffix = ""; 220 | } 221 | sb.append(dataPairToTableRow( 222 | prefix 223 | + locale.toString() 224 | + suffix, 225 | prefix 226 | + locale.getDisplayLanguage() 227 | + suffix 228 | + "
    "); 193 | sb.append("Code"); 194 | sb.append(""); 196 | sb.append("Language"); 197 | sb.append(""); 199 | sb.append("Country"); 200 | sb.append(""); 202 | sb.append("Variant"); 203 | sb.append("
    " 229 | + prefix 230 | + locale.getDisplayCountry() 231 | + suffix 232 | + "" 233 | + prefix 234 | + locale.getDisplayVariant() 235 | + suffix)); 236 | } 237 | tabPane.add("Locales", 238 | getOutputWidgetForContent(sb.toString())); 239 | 240 | Locale.getDefault(); 241 | int border = 5; 242 | JPanel p = new JPanel(new BorderLayout()); 243 | p.setBorder(new EmptyBorder(border, border, border, border)); 244 | p.add(tabPane, BorderLayout.CENTER); 245 | p.setPreferredSize(new Dimension(400, 400)); 246 | JFrame f = new JFrame("Properties"); 247 | f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 248 | f.getContentPane().add(p, BorderLayout.CENTER); 249 | f.pack(); 250 | f.setMinimumSize(f.getPreferredSize()); 251 | f.setSize(600, 500); 252 | f.setLocationRelativeTo(null); 253 | f.setVisible(true); 254 | } 255 | } 256 | 257 | class SortableLocale implements Comparable { 258 | 259 | Locale locale; 260 | 261 | SortableLocale(Locale locale) { 262 | this.locale = locale; 263 | } 264 | 265 | public String toString() { 266 | return locale.toString(); 267 | } 268 | 269 | public Locale getLocale() { 270 | return locale; 271 | } 272 | 273 | public int compareTo(Object object2) { 274 | SortableLocale locale2 = (SortableLocale) object2; 275 | //Locale locale2 = (Locale)object2; 276 | return locale.toString().compareTo( 277 | locale2.toString()); 278 | } 279 | } 280 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/CommPortIdentifier.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1997-2009 by Trent Jarvi tjarvi@qbang.org and others who 5 | | actually wrote it. See individual source files for more information. 6 | | 7 | | A copy of the LGPL v 2.1 may be found at 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 9 | | here for your convenience. 10 | | 11 | | This library is free software; you can redistribute it and/or 12 | | modify it under the terms of the GNU Lesser General Public 13 | | License as published by the Free Software Foundation; either 14 | | version 2.1 of the License, or (at your option) any later version. 15 | | 16 | | This library is distributed in the hope that it will be useful, 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | | Lesser General Public License for more details. 20 | | 21 | | An executable that contains no derivative of any portion of RXTX, but 22 | | is designed to work with RXTX by being dynamically linked with it, 23 | | is considered a "work that uses the Library" subject to the terms and 24 | | conditions of the GNU Lesser General Public License. 25 | | 26 | | The following has been added to the RXTX License to remove 27 | | any confusion about linking to RXTX. We want to allow in part what 28 | | section 5, paragraph 2 of the LGPL does not permit in the special 29 | | case of linking over a controlled interface. The intent is to add a 30 | | Java Specification Request or standards body defined interface in the 31 | | future as another exception but one is not currently available. 32 | | 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 34 | | 35 | | As a special exception, the copyright holders of RXTX give you 36 | | permission to link RXTX with independent modules that communicate with 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 38 | | regardless of the license terms of these independent modules, and to copy 39 | | and distribute the resulting combined work under terms of your choice, 40 | | provided that every copy of the combined work is accompanied by a complete 41 | | copy of the source code of RXTX (the version of RXTX used to produce the 42 | | combined work), being distributed under the terms of the GNU Lesser General 43 | | Public License plus this exception. An independent module is a 44 | | module which is not derived from or based on RXTX. 45 | | 46 | | Note that people who make modified versions of RXTX are not obligated 47 | | to grant this special exception for their modified versions; it is 48 | | their choice whether to do so. The GNU Lesser General Public License 49 | | gives permission to release a modified version without this exception; this 50 | | exception also makes it possible to release a modified version which 51 | | carries forward this exception. 52 | | 53 | | You should have received a copy of the GNU Lesser General Public 54 | | License along with this library; if not, write to the Free 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 56 | | All trademarks belong to their respective owners. 57 | --------------------------------------------------------------------------*/ 58 | package gnu.io; 59 | 60 | import java.io.FileDescriptor; 61 | import java.util.HashMap; 62 | import java.util.Vector; 63 | import java.util.Enumeration; 64 | import java.util.logging.Logger; 65 | 66 | /** 67 | * A 68 | * CommPortIdentifier represents a port on the system. In contrast 69 | * to 70 | * CommPort this class represents the port as an abstract device 71 | * and deals with the process of discovering, obtaining/managing ownership and 72 | * open the port. When a port is opened, a port type dependent 73 | * CommPort object is obtained and can be used to actually read and 74 | * write the port. 75 | * 76 | * @author Trent Jarvi 77 | * @author Bartłomiej P. Prokop 78 | * @version 2.3 79 | */ 80 | public class CommPortIdentifier { 81 | 82 | private static final Logger LOGGER = Logger.getLogger(CommPortIdentifier.class.getName()); 83 | 84 | /** 85 | * The port is a RS232 serial port. 86 | */ 87 | public static final int PORT_SERIAL = 1; // rs232 Port 88 | public static final int PORT_PARALLEL = 2; // Parallel Port 89 | public static final int PORT_I2C = 3; // i2c Port 90 | public static final int PORT_RS485 = 4; // rs485 Port 91 | public static final int PORT_RAW = 5; // Raw Port 92 | /** 93 | * The name of the corresponding port. On linux and probably other systems 94 | * this will typically look like /dev/ttyx on Windows COMx where x is some 95 | * integer. 96 | */ 97 | private final String portName; 98 | /** 99 | * States whether the port is available to be assigned to a new owner. When 100 | * it is false, the port is currently owned. 101 | */ 102 | private boolean Available = true; 103 | /** 104 | * The name of the current owner (might be null) and 105 | * null if the port is not currently owned. 106 | */ 107 | private String Owner; 108 | /** 109 | * When the port is open this will hold the port object. 110 | */ 111 | private CommPort commPort; 112 | /** 113 | * The driver which is used to access the associated port. 114 | */ 115 | private CommDriver RXTXDriver; 116 | // TODO (by Alexander Graf) commPortIndex and next are part of reinventing 117 | // the wheel by implementing a linked list 118 | static CommPortIdentifier CommPortIndex; 119 | CommPortIdentifier next; 120 | /** 121 | * A PORT_* constant indicating the port hardware type. 122 | */ 123 | private final int portType; 124 | // TODO (by Alexander Graf) the access to this field from all corners of 125 | // rxtx is really ugly and should be rewritten 126 | static final Object Sync = new Object(); 127 | /** 128 | * A list of registered PortOwnershipListeners. 129 | */ 130 | // TODO (by Alexander Graf) the list of listeners should have a plural name 131 | // and should be private. Should use interface type here and Vector is 132 | // deprecated 133 | Vector ownershipListener; 134 | 135 | /** 136 | * static {} aka initialization accept: - perform: load the rxtx driver 137 | * return: - exceptions: Throwable comments: static block to initialize the 138 | * class 139 | */ 140 | static { 141 | try { 142 | CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance(); 143 | RXTXDriver.initialize(); 144 | } catch (Throwable e) { 145 | System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver"); 146 | } 147 | 148 | String OS = System.getProperty("os.name"); 149 | if (OS.toLowerCase().indexOf("linux") == -1) { 150 | LOGGER.warning("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier"); 151 | } 152 | RXTXVersion.ensureNativeCodeLoaded(); 153 | } 154 | 155 | /** 156 | * Creates a new port identifier. This is typically called by a drivers 157 | * initialize() method. 158 | * 159 | * @param portName a name for the port which is used as a per-driver-unique 160 | * identifier. Because the name might be exposed to the user it should be 161 | * unique across all drivers. 162 | * @param commPort (unused by rxtx; set to null) 163 | * @param portType the hardware type of the port 164 | * @param driver the driver which is used to access this port 165 | */ 166 | CommPortIdentifier(String portName, CommPort commPort, int portType, CommDriver driver) { 167 | this.portName = portName; 168 | this.commPort = commPort; 169 | this.portType = portType; 170 | this.next = null; 171 | this.RXTXDriver = driver; 172 | } 173 | 174 | /** 175 | * addPortName accept: Name of the port s, Port type, reverence to 176 | * RXTXCommDriver. perform: place a new CommPortIdentifier in the linked 177 | * list return: none. exceptions: none. comments: 178 | * 179 | * @param s - port name 180 | * @param type - port type 181 | * @param c - reference for ComDriver 182 | */ 183 | public static void addPortName(String s, int type, CommDriver c) { 184 | LOGGER.fine("CommPortIdentifier:addPortName(" + s + ")"); 185 | // TODO (by Alexander Graf) the usage of the constructor with a null 186 | // value for the port is a major design problem. Rxtx currently creates 187 | // the port objects on a per-open basis. This clashes with the design 188 | // idea of the comm API where a port object is created by the driver 189 | // once and given to the CommPortIdentifier constructor. 190 | // This problem is again introduced by the poor design of the comm APIs 191 | // SPI. 192 | AddIdentifierToList(new CommPortIdentifier(s, null, type, c)); 193 | } 194 | 195 | /** 196 | * AddIdentifierToList() accept: The cpi to add to the list. perform: 197 | * return: exceptions: comments: 198 | */ 199 | //TODO (Alexander Graf) This method/class seems to reinvent the wheel 200 | //by implementing its own linked list functionallity 201 | private static void AddIdentifierToList(CommPortIdentifier cpi) { 202 | LOGGER.fine("CommPortIdentifier:AddIdentifierToList()"); 203 | synchronized (Sync) { 204 | if (CommPortIndex == null) { 205 | CommPortIndex = cpi; 206 | LOGGER.fine("CommPortIdentifier:AddIdentifierToList() null"); 207 | } else { 208 | CommPortIdentifier index = CommPortIndex; 209 | while (index.next != null) { 210 | index = index.next; 211 | LOGGER.fine("CommPortIdentifier:AddIdentifierToList() index.next"); 212 | } 213 | index.next = cpi; 214 | } 215 | } 216 | } 217 | 218 | /** 219 | * Registers an ownership listener. The listener will be informed when other 220 | * applications open or close the port which is represented by this 221 | * CommPortIdentifier. When the port is currently owned by this 222 | * application the listener is informed when other applications request 223 | * ownership of the port. 224 | * 225 | * @param c the ownership listener to register 226 | */ 227 | public void addPortOwnershipListener(CommPortOwnershipListener c) { 228 | LOGGER.fine("CommPortIdentifier:addPortOwnershipListener()"); 229 | 230 | /* is the Vector instantiated? */ 231 | if (ownershipListener == null) { 232 | ownershipListener = new Vector(); 233 | } 234 | 235 | if (!ownershipListener.contains(c)) { 236 | ownershipListener.addElement(c); 237 | } 238 | } 239 | 240 | /** 241 | * Returns a textual representation of the current owner of the port. An 242 | * owner is an application which is currently using the port (in the sense 243 | * that it opened the port and has not closed it yet). 244 | * 245 | * To check if a port is owned use the isCurrentlyOwned method. 246 | * Do not rely on this method to return null. It can't be guaranteed that 247 | * owned ports have a non null owner. 248 | * 249 | * @return the port owner or null if the port is not currently owned 250 | */ 251 | public String getCurrentOwner() { 252 | return Owner; 253 | } 254 | 255 | /** 256 | * Returns the name of the port which is represented by this 257 | * CommPortIdentifier. Be aware of the fact that there are 258 | * different conventions for port names on different operating systems (for 259 | * example a port name on linux looks like '/dev/tty1', on Windows like 260 | * 'COM1'). Therefor the application should not parse this string in a 261 | * probably non cross platform compatible way. 262 | * 263 | * @return the name of the associated port 264 | */ 265 | public String getName() { 266 | return portName; 267 | } 268 | 269 | /** 270 | * Returns the associated port identifier of a port with the given name. If 271 | * no port with the given name can be found a 272 | * NoSuchPortException is thrown. 273 | * 274 | * @param s the name of the port whose identifier is looked up 275 | * @return the identifier of the port 276 | * @throws NoSuchPortException when no port with the given name was found 277 | */ 278 | static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException { 279 | LOGGER.fine("CommPortIdentifier:getPortIdentifier(" + s + ")"); 280 | CommPortIdentifier index; 281 | 282 | synchronized (Sync) { 283 | index = CommPortIndex; 284 | while (index != null && !index.portName.equals(s)) { 285 | index = index.next; 286 | } 287 | if (index == null) { 288 | /* This may slow things down but if you pass the string for the port after 289 | a device is plugged in, you can find it now. 290 | 291 | http://bugzilla.qbang.org/show_bug.cgi?id=48 292 | */ 293 | getPortIdentifiers(); 294 | index = CommPortIndex; 295 | while (index != null && !index.portName.equals(s)) { 296 | index = index.next; 297 | } 298 | } 299 | } 300 | if (index != null) { 301 | return index; 302 | } else { 303 | LOGGER.fine("not found!" + s); 304 | throw new NoSuchPortException(); 305 | } 306 | } 307 | 308 | /** 309 | * Gets communication port identifier 310 | * 311 | * @param p - port for which identifier object is returned 312 | * @return - port identifier object 313 | * @throws NoSuchPortException - in case the non existing serial port found 314 | */ 315 | static public CommPortIdentifier getPortIdentifier(CommPort p) throws NoSuchPortException { 316 | LOGGER.fine("CommPortIdentifier:getPortIdentifier(CommPort)"); 317 | 318 | CommPortIdentifier c; 319 | synchronized (Sync) { 320 | c = CommPortIndex; 321 | while (c != null && c.commPort != p) { 322 | c = c.next; 323 | } 324 | } 325 | if (c != null) { 326 | return (c); 327 | } 328 | 329 | LOGGER.fine("not found!" + p.getName()); 330 | throw new NoSuchPortException(); 331 | } 332 | 333 | /** 334 | * Returns an enumeration of port identifiers which represent the 335 | * communication ports currently available on the system. 336 | * 337 | * @return enumeration of available communication ports 338 | */ 339 | static public Enumeration getPortIdentifiers() { 340 | 341 | LOGGER.fine("static CommPortIdentifier:getPortIdentifiers()"); 342 | 343 | //Do not allow anybody get any ports while we are re-initializing 344 | //because the CommPortIndex points to invalid instances during that time 345 | synchronized (Sync) { 346 | //Remember old ports in order to restore them for ownership events later 347 | HashMap oldPorts = new HashMap(); 348 | CommPortIdentifier p = CommPortIndex; 349 | while (p != null) { 350 | oldPorts.put(p.portName, p); 351 | p = p.next; 352 | } 353 | CommPortIndex = null; 354 | try { 355 | //Initialize RXTX: This leads to detecting all ports 356 | //and writing them into our CommPortIndex through our method 357 | //{@link #addPortName(java.lang.String, int, gnu.io.CommDriver)} 358 | //This works while lock on Sync is held 359 | CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance(); 360 | RXTXDriver.initialize(); 361 | //Restore old CommPortIdentifier objects where possible, 362 | //in order to support proper ownership event handling. 363 | //Clients might still have references to old identifiers! 364 | CommPortIdentifier curPort = CommPortIndex; 365 | CommPortIdentifier prevPort = null; 366 | while (curPort != null) { 367 | CommPortIdentifier matchingOldPort = (CommPortIdentifier) oldPorts.get(curPort.portName); 368 | if (matchingOldPort != null && matchingOldPort.portType == curPort.portType) { 369 | //replace new port by old one 370 | matchingOldPort.RXTXDriver = curPort.RXTXDriver; 371 | matchingOldPort.next = curPort.next; 372 | if (prevPort == null) { 373 | CommPortIndex = matchingOldPort; 374 | } else { 375 | prevPort.next = matchingOldPort; 376 | } 377 | prevPort = matchingOldPort; 378 | } else { 379 | prevPort = curPort; 380 | } 381 | curPort = curPort.next; 382 | } 383 | } catch (Throwable e) { 384 | System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver"); 385 | System.err.flush(); 386 | } 387 | } 388 | return new CommPortEnumerator(); 389 | } 390 | 391 | /** 392 | * Returns the port hardware type of the associated port. The port type is 393 | * encoded as one of the available PORT_* constants. 394 | * 395 | * @return the port type 396 | */ 397 | public int getPortType() { 398 | return portType; 399 | } 400 | 401 | /** 402 | * Determines whether the associated port is in use by an application 403 | * (including this application). 404 | * 405 | * @return true if an application is using the port, false if the port is 406 | * not currently owned. 407 | */ 408 | public synchronized boolean isCurrentlyOwned() { 409 | return (!Available); 410 | } 411 | 412 | /** 413 | * open 414 | * 415 | * @param f - file descriptor 416 | * @return - communication port 417 | * @throws UnsupportedCommOperationException - always as is not implemented 418 | * yet 419 | */ 420 | // TODO (by Alexander Graf) this method is for legacy support of the 421 | //comm API and is obviosly unsupported by rxtx 422 | // It should be deprecated (and possibly removed some day) when we don't 423 | // care about compatibility to the comm API 424 | public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException { 425 | LOGGER.fine("CommPortIdentifier:open(FileDescriptor)"); 426 | throw new UnsupportedCommOperationException(); 427 | } 428 | 429 | private native String native_psmisc_report_owner(String PortName); 430 | 431 | /** 432 | * open() accept: application making the call and milliseconds to block 433 | * during open. perform: open the port if possible return: CommPort if 434 | * successful exceptions: PortInUseException if in use. comments: 435 | */ 436 | private boolean HideOwnerEvents; 437 | 438 | /** 439 | * Opens the port. When the port is in use by another application an 440 | * ownership request is propagated. When the current owner does not close 441 | * the port within timeLimit milliseconds, the open operation 442 | * will fail with a PortInUseException. 443 | * 444 | * The owner should contain the name of the application which 445 | * opens the port. Later it can be read by other applications to distinguish 446 | * the owner of the port. 447 | * 448 | * @param TheOwner owner the name of the application which is opening the port 449 | * @param i timeLimit the time limit to obtain ownership in milliseconds 450 | * @return the port object of the successfully opened port 451 | * @throws gnu.io.PortInUseException when the exclusive ownership of the 452 | * port could not be obtained 453 | */ 454 | // TODO (by Alexander Graf) is there really done an ownership request that 455 | // can reach applications outside of this virtual machine, probably not 456 | // not written in java? I can't see where this request is done. 457 | public CommPort open(String TheOwner, int i) throws gnu.io.PortInUseException { 458 | 459 | LOGGER.fine("CommPortIdentifier:open(" + TheOwner + ", " + i + ")"); 460 | 461 | boolean isAvailable; 462 | synchronized (this) { 463 | isAvailable = this.Available; 464 | if (isAvailable) { 465 | //assume ownership inside the synchronized block 466 | this.Available = false; 467 | this.Owner = TheOwner; 468 | } 469 | } 470 | if (!isAvailable) { 471 | long waitTimeEnd = System.currentTimeMillis() + i; 472 | //fire the ownership event outside the synchronized block 473 | fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED); 474 | long waitTimeCurr; 475 | synchronized (this) { 476 | while (!Available && (waitTimeCurr = System.currentTimeMillis()) < waitTimeEnd) { 477 | try { 478 | wait(waitTimeEnd - waitTimeCurr); 479 | } catch (InterruptedException e) { 480 | Thread.currentThread().interrupt(); 481 | break; 482 | } 483 | } 484 | isAvailable = this.Available; 485 | if (isAvailable) { 486 | //assume ownership inside the synchronized block 487 | this.Available = false; 488 | this.Owner = TheOwner; 489 | } 490 | } 491 | } 492 | if (!isAvailable) { 493 | throw new gnu.io.PortInUseException(getCurrentOwner()); 494 | } 495 | //At this point, the CommPortIdentifier is owned by us. 496 | try { 497 | if (commPort == null) { 498 | commPort = RXTXDriver.getCommPort(portName, portType); 499 | } 500 | if (commPort != null) { 501 | fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED); 502 | return commPort; 503 | } else { 504 | String err_msg; 505 | try { 506 | err_msg = native_psmisc_report_owner(portName); 507 | } catch (Throwable t) { 508 | err_msg = "Port " + portName + " already owned... unable to open."; 509 | } 510 | throw new gnu.io.PortInUseException(err_msg); 511 | } 512 | } finally { 513 | if (commPort == null) { 514 | //something went wrong reserving the commport -> unown the port 515 | synchronized (this) { 516 | this.Available = true; 517 | this.Owner = null; 518 | } 519 | } 520 | } 521 | } 522 | 523 | /** 524 | * removes PortOwnership listener 525 | * 526 | * @param c - listener to remove 527 | */ 528 | public void removePortOwnershipListener(CommPortOwnershipListener c) { 529 | LOGGER.fine("CommPortIdentifier:removePortOwnershipListener()"); 530 | /* why is this called twice? */ 531 | if (ownershipListener != null) { 532 | ownershipListener.removeElement(c); 533 | } 534 | } 535 | 536 | /** 537 | * clean up the Ownership information and send the event 538 | */ 539 | void internalClosePort() { 540 | synchronized (this) { 541 | LOGGER.fine("CommPortIdentifier:internalClosePort()"); 542 | Owner = null; 543 | Available = true; 544 | // TODO (by Alexander Graf) to set the commPort to null is 545 | // incompatible with the idea behind the architecture of the 546 | // constructor, where commPort is assigned by the driver once 547 | // in a virtual machines lifetime. 548 | commPort = null; 549 | /* this tosses null pointer?? */ 550 | notifyAll(); 551 | } 552 | fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED); 553 | } 554 | 555 | void fireOwnershipEvent(int eventType) { 556 | LOGGER.fine("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )"); 557 | if (ownershipListener != null) { 558 | CommPortOwnershipListener c; 559 | //TODO (by Alexander Graf) for statement abuse ... 560 | for (Enumeration e = ownershipListener.elements(); 561 | e.hasMoreElements(); 562 | c.ownershipChange(eventType)) { 563 | c = (CommPortOwnershipListener) e.nextElement(); 564 | } 565 | } 566 | } 567 | } 568 | -------------------------------------------------------------------------------- /src/main/java/gnu/io/RXTXCommDriver.java: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. 3 | | RXTX is a native interface to serial ports in java. 4 | | Copyright 1998 Kevin Hester, kevinh@acm.org 5 | | Copyright 2000-2010 Trent Jarvi tjarvi@qbang.org and others who 6 | | actually wrote it. See individual source files for more information. 7 | | 8 | | A copy of the LGPL v 2.1 may be found at 9 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is 10 | | here for your convenience. 11 | | 12 | | This library is free software; you can redistribute it and/or 13 | | modify it under the terms of the GNU Lesser General Public 14 | | License as published by the Free Software Foundation; either 15 | | version 2.1 of the License, or (at your option) any later version. 16 | | 17 | | This library is distributed in the hope that it will be useful, 18 | | but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | | Lesser General Public License for more details. 21 | | 22 | | An executable that contains no derivative of any portion of RXTX, but 23 | | is designed to work with RXTX by being dynamically linked with it, 24 | | is considered a "work that uses the Library" subject to the terms and 25 | | conditions of the GNU Lesser General Public License. 26 | | 27 | | The following has been added to the RXTX License to remove 28 | | any confusion about linking to RXTX. We want to allow in part what 29 | | section 5, paragraph 2 of the LGPL does not permit in the special 30 | | case of linking over a controlled interface. The intent is to add a 31 | | Java Specification Request or standards body defined interface in the 32 | | future as another exception but one is not currently available. 33 | | 34 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface 35 | | 36 | | As a special exception, the copyright holders of RXTX give you 37 | | permission to link RXTX with independent modules that communicate with 38 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, 39 | | regardless of the license terms of these independent modules, and to copy 40 | | and distribute the resulting combined work under terms of your choice, 41 | | provided that every copy of the combined work is accompanied by a complete 42 | | copy of the source code of RXTX (the version of RXTX used to produce the 43 | | combined work), being distributed under the terms of the GNU Lesser General 44 | | Public License plus this exception. An independent module is a 45 | | module which is not derived from or based on RXTX. 46 | | 47 | | Note that people who make modified versions of RXTX are not obligated 48 | | to grant this special exception for their modified versions; it is 49 | | their choice whether to do so. The GNU Lesser General Public License 50 | | gives permission to release a modified version without this exception; this 51 | | exception also makes it possible to release a modified version which 52 | | carries forward this exception. 53 | | 54 | | You should have received a copy of the GNU Lesser General Public 55 | | License along with this library; if not, write to the Free 56 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 57 | | All trademarks belong to their respective owners. 58 | --------------------------------------------------------------------------*/ 59 | /* Martin Pool added support for explicitly-specified 60 | * lists of ports, October 2000. */ 61 | /* Joseph Goldstone reorganized to support registered ports, 62 | * known ports, and scanned ports, July 2001 */ 63 | package gnu.io; 64 | 65 | import java.io.File; 66 | import java.io.FileInputStream; 67 | import java.io.IOException; 68 | import java.net.URL; 69 | import java.util.Enumeration; 70 | import java.util.Properties; 71 | import java.util.StringTokenizer; 72 | import java.util.logging.Logger; 73 | 74 | /** 75 | * This is the JavaComm for Linux driver. 76 | */ 77 | public class RXTXCommDriver implements CommDriver { 78 | 79 | private final static Logger logger = Logger.getLogger(RXTXCommDriver.class.getName()); 80 | 81 | static { 82 | RXTXVersion.ensureNativeCodeLoaded(); 83 | } 84 | 85 | /** 86 | * Get the Serial port prefixes for the running OS 87 | */ 88 | private String deviceDirectory; 89 | 90 | private native boolean registerKnownPorts(int PortType); 91 | 92 | private native boolean isPortPrefixValid(String dev); 93 | 94 | private native boolean testRead(String dev, int type); 95 | 96 | private native String getDeviceDirectory(); 97 | 98 | // FIXME: This method is never called. 99 | private final String[] getValidPortPrefixes(String CandidatePortPrefixes[]) { 100 | /* 101 | 256 is the number of prefixes ( COM, cua, ttyS, ...) not 102 | the number of devices (ttyS0, ttyS1, ttyS2, ...) 103 | 104 | On a Linux system there are about 400 prefixes in 105 | deviceDirectory. 106 | registerScannedPorts() assigns CandidatePortPrefixes to 107 | something less than 50 prefixes. 108 | 109 | Trent 110 | */ 111 | 112 | String ValidPortPrefixes[] = new String[256]; 113 | logger.fine("\nRXTXCommDriver:getValidPortPrefixes()"); 114 | if (CandidatePortPrefixes == null) { 115 | logger.fine("\nRXTXCommDriver:getValidPortPrefixes() No ports prefixes known for this System.\nPlease check the port prefixes listed for " + RXTXVersion.getOsName() + " in RXTXCommDriver:registerScannedPorts()\n"); 116 | } 117 | int i = 0; 118 | for (int j = 0; j < CandidatePortPrefixes.length; j++) { 119 | if (isPortPrefixValid(CandidatePortPrefixes[j])) { 120 | ValidPortPrefixes[i++] 121 | = CandidatePortPrefixes[j]; 122 | } 123 | } 124 | String[] returnArray = new String[i]; 125 | System.arraycopy(ValidPortPrefixes, 0, returnArray, 0, i); 126 | if (ValidPortPrefixes[0] == null) { 127 | 128 | logger.fine("\nRXTXCommDriver:getValidPortPrefixes() No ports matched the list assumed for this\nSystem in the directory " + deviceDirectory + ". Please check the ports listed for \"" + RXTXVersion.getOsName() + "\" in\nRXTXCommDriver:registerScannedPorts()\nTried:"); 129 | for (int j = 0; j < CandidatePortPrefixes.length; j++) { 130 | logger.fine("\t" 131 | + CandidatePortPrefixes[i]); 132 | } 133 | 134 | } else { 135 | logger.fine("\nRXTXCommDriver:getValidPortPrefixes()\nThe following port prefixes have been identified as valid on " + RXTXVersion.getOsName() + ":\n"); 136 | /* 137 | for(int j=0;jFrom the NullDriver.java CommAPI sample. 266 | * 267 | * added printerport stuff 268 | * Holger Lehmann 269 | * July 12, 1999 270 | * IBM 271 | 272 | * Added ttyM for Moxa boards 273 | * Removed obsolete device cuaa 274 | * Peter Bennett 275 | * January 02, 2000 276 | * Bencom 277 | 278 | */ 279 | /** 280 | * Determine the OS and where the OS has the devices located 281 | */ 282 | @Override 283 | public void initialize() { 284 | deviceDirectory = getDeviceDirectory(); 285 | 286 | /* 287 | First try to register ports specified in the properties 288 | file. If that doesn't exist, then scan for ports. 289 | */ 290 | for (int PortType = CommPortIdentifier.PORT_SERIAL; PortType <= CommPortIdentifier.PORT_PARALLEL; PortType++) { 291 | if (!registerSpecifiedPorts(PortType)) { 292 | if (!registerKnownPorts(PortType)) { 293 | registerScannedPorts(PortType); 294 | } 295 | } 296 | } 297 | } 298 | 299 | private void addSpecifiedPorts(String names, int PortType) { 300 | final String pathSep = System.getProperty("path.separator", ":"); 301 | final StringTokenizer tok = new StringTokenizer(names, pathSep); 302 | 303 | logger.fine("\nRXTXCommDriver:addSpecifiedPorts()"); 304 | 305 | while (tok.hasMoreElements()) { 306 | String PortName = tok.nextToken(); 307 | 308 | if (testRead(PortName, PortType)) { 309 | CommPortIdentifier.addPortName(PortName, PortType, this); 310 | } 311 | } 312 | } 313 | 314 | /* 315 | * Register ports specified in the file "gnu.io.rxtx.properties" 316 | * Key system properties: 317 | * gnu.io.rxtx.SerialPorts 318 | * gnu.io.rxtx.ParallelPorts 319 | * 320 | * Tested only with sun jdk1.3 321 | * The file gnu.io.rxtx.properties may reside in the java extension dir, 322 | * or it can be anywhere in the classpath. 323 | * 324 | * Example: /usr/local/java/jre/lib/ext/gnu.io.rxtx.properties 325 | * 326 | * The file contains the following key properties: 327 | * 328 | * gnu.io.rxtx.SerialPorts=/dev/ttyS0:/dev/ttyS1: 329 | * gnu.io.rxtx.ParallelPorts=/dev/lp0: 330 | * 331 | */ 332 | private boolean registerSpecifiedPorts(int PortType) { 333 | String val = null; 334 | Properties props = null; 335 | String file_loc = null; 336 | // Old style: properties file must be in JRE folder 337 | String ext_dirs = System.getProperty("java.ext.dirs"); 338 | String[] dirArray = ext_dirs.split(System.getProperty("path.separator")); 339 | for (int i = 0; i < dirArray.length; i++) { 340 | String file_name = dirArray[i] + System.getProperty("file.separator") + "gnu.io.rxtx.properties"; 341 | File file = new File(file_name); 342 | if (file.exists()) { 343 | file_loc = file_name; 344 | break; 345 | } 346 | } 347 | if (file_loc != null) { 348 | FileInputStream in = null; 349 | try { 350 | in = new FileInputStream(file_loc); 351 | props = new Properties(); 352 | props.load(in); 353 | } catch (Exception e) { 354 | 355 | logger.fine("Error encountered while reading " + file_loc + ": " + e); 356 | 357 | } finally { 358 | if (in != null) { 359 | try { 360 | in.close(); 361 | } catch (IOException e) { 362 | } 363 | } 364 | } 365 | } 366 | if (props == null) { 367 | // New style: properties file anywhere in classpath 368 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); 369 | try { 370 | Enumeration resources = loader.getResources("gnu.io.rxtx.properties"); 371 | while (resources.hasMoreElements()) { 372 | URL propertyURL = (URL) resources.nextElement(); 373 | props = new Properties(); 374 | props.load(propertyURL.openStream()); 375 | break; 376 | } 377 | } catch (IOException e) { 378 | 379 | logger.fine("Error encountered while getting gnu.io.rxtx.properties from the classpath: " + e); 380 | 381 | } 382 | } 383 | 384 | logger.fine("checking for system-known ports of type " + PortType); 385 | 386 | logger.fine("checking registry for ports of type " + PortType); 387 | if (props != null) { 388 | switch (PortType) { 389 | case CommPortIdentifier.PORT_SERIAL: 390 | if ((val = props.getProperty("gnu.io.rxtx.SerialPorts")) == null) { 391 | val = props.getProperty("gnu.io.SerialPorts"); 392 | } 393 | break; 394 | 395 | case CommPortIdentifier.PORT_PARALLEL: 396 | if ((val = props.getProperty("gnu.io.rxtx.ParallelPorts")) == null) { 397 | val = props.getProperty("gnu.io.ParallelPorts"); 398 | } 399 | break; 400 | default: 401 | 402 | logger.fine("unknown port type " + PortType + " passed to RXTXCommDriver.registerSpecifiedPorts()"); 403 | 404 | } 405 | } else { 406 | 407 | logger.fine("The file: gnu.io.rxtx.properties doesn't exist."); 408 | 409 | } 410 | if (val != null) { 411 | addSpecifiedPorts(val, PortType); 412 | return true; 413 | } else { 414 | return false; 415 | } 416 | } 417 | 418 | /* 419 | * Look for all entries in deviceDirectory, and if they look like they should 420 | * be serial ports on this OS and they can be opened then register 421 | * them. 422 | * 423 | */ 424 | private void registerScannedPorts(int PortType) { 425 | String[] CandidateDeviceNames; 426 | 427 | logger.fine("scanning device directory " + deviceDirectory + " for ports of type " + PortType); 428 | 429 | if (RXTXVersion.getOsName().equals("Windows CE")) { 430 | String[] temp 431 | = {"COM1:", "COM2:", "COM3:", "COM4:", 432 | "COM5:", "COM6:", "COM7:", "COM8:"}; 433 | CandidateDeviceNames = temp; 434 | } else if (RXTXVersion.getOsName().toLowerCase().indexOf("windows") != -1) { 435 | String[] temp = new String[259]; 436 | for (int i = 1; i <= 256; i++) { 437 | temp[i - 1] = "COM" + i; 438 | } 439 | for (int i = 1; i <= 3; i++) { 440 | temp[i + 255] = "LPT" + i; 441 | } 442 | CandidateDeviceNames = temp; 443 | } else if (RXTXVersion.getOsName().equals("Solaris") || RXTXVersion.getOsName().equals("SunOS")) { 444 | /* Solaris uses a few different ways to identify ports. 445 | They could be /dev/term/a /dev/term0 /dev/cua/a /dev/cuaa 446 | the /dev/???/a appears to be on more systems. 447 | 448 | The uucp lock files should not cause problems. 449 | */ 450 | /* 451 | File dev = new File( "/dev/term" ); 452 | String deva[] = dev.list(); 453 | dev = new File( "/dev/cua" ); 454 | String devb[] = dev.list(); 455 | String[] temp = new String[ deva.length + devb.length ]; 456 | for(int j =0;j 0) { 484 | term[l++] = "term/"; 485 | } 486 | /* 487 | dev = new File( "/dev/cua0" ); 488 | if( dev.list().length > 0 ) 489 | term[l++] = "cua/"; 490 | */ 491 | String[] temp = new String[l]; 492 | for (l--; l >= 0; l--) { 493 | temp[l] = term[l]; 494 | } 495 | CandidateDeviceNames = temp; 496 | } else { 497 | File dev = new File(deviceDirectory); 498 | String[] temp = dev.list(); 499 | CandidateDeviceNames = temp; 500 | } 501 | if (CandidateDeviceNames == null) { 502 | 503 | logger.fine("RXTXCommDriver:registerScannedPorts() no Device files to check "); 504 | 505 | return; 506 | } 507 | 508 | String CandidatePortPrefixes[] = {}; 509 | switch (PortType) { 510 | case CommPortIdentifier.PORT_SERIAL: 511 | 512 | logger.fine("scanning for serial ports for os " + RXTXVersion.getOsName()); 513 | 514 | /* There are _many_ possible ports that can be used 515 | on Linux. See below in the fake Linux-all-ports 516 | case for a list. You may add additional ports 517 | here but be warned that too many will significantly 518 | slow down port enumeration. Linux 2.6 has udev 519 | support which should be faster as only ports the 520 | kernel finds should be exposed in /dev 521 | 522 | See also how to override port enumeration and 523 | specifying port in INSTALL. 524 | 525 | taj 526 | */ 527 | if (RXTXVersion.getOsName().equals("Linux")) { 528 | String[] Temp = { 529 | "ttyS", // linux Serial Ports 530 | "ttyAMA", // for Raspberry Pi 2 531 | "ttySA", // for the IPAQs 532 | "ttyUSB", // for USB frobs 533 | "rfcomm", // bluetooth serial device 534 | "ttyircomm", // linux IrCommdevices (IrDA serial emu) 535 | }; 536 | CandidatePortPrefixes = Temp; 537 | } else if (RXTXVersion.getOsName().equals("Linux-all-ports")) { 538 | /* if you want to enumerate all ports ~5000 539 | possible, then replace the above with this 540 | */ 541 | String[] Temp = { 542 | "comx", // linux COMMX synchronous serial card 543 | "holter", // custom card for heart monitoring 544 | "modem", // linux symbolic link to modem. 545 | "rfcomm", // bluetooth serial device 546 | "ttyircomm", // linux IrCommdevices (IrDA serial emu) 547 | "ttycosa0c", // linux COSA/SRP synchronous serial card 548 | "ttycosa1c", // linux COSA/SRP synchronous serial card 549 | "ttyACM",// linux CDC ACM devices 550 | "ttyC", // linux cyclades cards 551 | "ttyCH",// linux Chase Research AT/PCI-Fast serial card 552 | "ttyD", // linux Digiboard serial card 553 | "ttyE", // linux Stallion serial card 554 | "ttyF", // linux Computone IntelliPort serial card 555 | "ttyH", // linux Chase serial card 556 | "ttyI", // linux virtual modems 557 | "ttyL", // linux SDL RISCom serial card 558 | "ttyM", // linux PAM Software's multimodem boards 559 | // linux ISI serial card 560 | "ttyMX",// linux Moxa Smart IO cards 561 | "ttyP", // linux Hayes ESP serial card 562 | "ttyR", // linux comtrol cards 563 | // linux Specialix RIO serial card 564 | "ttyS", // linux Serial Ports 565 | "ttySI",// linux SmartIO serial card 566 | "ttySR",// linux Specialix RIO serial card 257+ 567 | "ttyT", // linux Technology Concepts serial card 568 | "ttyUSB",//linux USB serial converters 569 | "ttyV", // linux Comtrol VS-1000 serial controller 570 | "ttyW", // linux specialix cards 571 | "ttyX" // linux SpecialX serial card 572 | }; 573 | CandidatePortPrefixes = Temp; 574 | } else if (RXTXVersion.getOsName().toLowerCase().indexOf("qnx") != -1) { 575 | String[] Temp = { 576 | "ser" 577 | }; 578 | CandidatePortPrefixes = Temp; 579 | } else if (RXTXVersion.getOsName().equals("Irix")) { 580 | String[] Temp = { 581 | "ttyc", // irix raw character devices 582 | "ttyd", // irix basic serial ports 583 | "ttyf", // irix serial ports with hardware flow 584 | "ttym", // irix modems 585 | "ttyq", // irix pseudo ttys 586 | "tty4d",// irix RS422 587 | "tty4f",// irix RS422 with HSKo/HSki 588 | "midi", // irix serial midi 589 | "us" // irix mapped interface 590 | }; 591 | CandidatePortPrefixes = Temp; 592 | } else if (RXTXVersion.getOsName().equals("FreeBSD")) //FIXME this is probably wrong 593 | { 594 | String[] Temp = { 595 | "ttyd", //general purpose serial ports 596 | "cuaa", //dialout serial ports 597 | "ttyA", //Specialix SI/XIO dialin ports 598 | "cuaA", //Specialix SI/XIO dialout ports 599 | "ttyD", //Digiboard - 16 dialin ports 600 | "cuaD", //Digiboard - 16 dialout ports 601 | "ttyE", //Stallion EasyIO (stl) dialin ports 602 | "cuaE", //Stallion EasyIO (stl) dialout ports 603 | "ttyF", //Stallion Brumby (stli) dialin ports 604 | "cuaF", //Stallion Brumby (stli) dialout ports 605 | "ttyR", //Rocketport dialin ports 606 | "cuaR", //Rocketport dialout ports 607 | "stl" //Stallion EasyIO board or Brumby N 608 | }; 609 | CandidatePortPrefixes = Temp; 610 | } else if (RXTXVersion.getOsName().equals("NetBSD")) // FIXME this is probably wrong 611 | { 612 | String[] Temp = { 613 | "tty0" // netbsd serial ports 614 | }; 615 | CandidatePortPrefixes = Temp; 616 | } else if (RXTXVersion.getOsName().equals("Solaris") || RXTXVersion.getOsName().equals("SunOS")) { 617 | String[] Temp = { 618 | "term/", 619 | "cua/" 620 | }; 621 | CandidatePortPrefixes = Temp; 622 | } else if (RXTXVersion.getOsName().equals("HP-UX")) { 623 | String[] Temp = { 624 | "tty0p",// HP-UX serial ports 625 | "tty1p" // HP-UX serial ports 626 | }; 627 | CandidatePortPrefixes = Temp; 628 | } else if (RXTXVersion.getOsName().equals("UnixWare") || RXTXVersion.getOsName().equals("OpenUNIX")) { 629 | String[] Temp = { 630 | "tty00s", // UW7/OU8 serial ports 631 | "tty01s", 632 | "tty02s", 633 | "tty03s" 634 | }; 635 | CandidatePortPrefixes = Temp; 636 | } else if (RXTXVersion.getOsName().equals("OpenServer")) { 637 | String[] Temp = { 638 | "tty1A", // OSR5 serial ports 639 | "tty2A", 640 | "tty3A", 641 | "tty4A", 642 | "tty5A", 643 | "tty6A", 644 | "tty7A", 645 | "tty8A", 646 | "tty9A", 647 | "tty10A", 648 | "tty11A", 649 | "tty12A", 650 | "tty13A", 651 | "tty14A", 652 | "tty15A", 653 | "tty16A", 654 | "ttyu1A", // OSR5 USB-serial ports 655 | "ttyu2A", 656 | "ttyu3A", 657 | "ttyu4A", 658 | "ttyu5A", 659 | "ttyu6A", 660 | "ttyu7A", 661 | "ttyu8A", 662 | "ttyu9A", 663 | "ttyu10A", 664 | "ttyu11A", 665 | "ttyu12A", 666 | "ttyu13A", 667 | "ttyu14A", 668 | "ttyu15A", 669 | "ttyu16A" 670 | }; 671 | CandidatePortPrefixes = Temp; 672 | } else if (RXTXVersion.getOsName().equals("Compaq's Digital UNIX") || RXTXVersion.getOsName().equals("OSF1")) { 673 | String[] Temp = { 674 | "tty0" // Digital Unix serial ports 675 | }; 676 | CandidatePortPrefixes = Temp; 677 | } else if (RXTXVersion.getOsName().equals("BeOS")) { 678 | String[] Temp = { 679 | "serial" // BeOS serial ports 680 | }; 681 | CandidatePortPrefixes = Temp; 682 | } else if (RXTXVersion.getOsName().equals("Mac OS X")) { 683 | String[] Temp = { 684 | // Keyspan USA-28X adapter, USB port 1 685 | "cu.KeyUSA28X191.", 686 | // Keyspan USA-28X adapter, USB port 1 687 | "tty.KeyUSA28X191.", 688 | // Keyspan USA-28X adapter, USB port 2 689 | "cu.KeyUSA28X181.", 690 | // Keyspan USA-28X adapter, USB port 2 691 | "tty.KeyUSA28X181.", 692 | // Keyspan USA-19 adapter 693 | "cu.KeyUSA19181.", 694 | // Keyspan USA-19 adapter 695 | "tty.KeyUSA19181." 696 | }; 697 | CandidatePortPrefixes = Temp; 698 | } else if (RXTXVersion.getOsName().toLowerCase().indexOf("windows") != -1) { 699 | String[] Temp = { 700 | "COM" // win32 serial ports 701 | //"//./COM" // win32 serial ports 702 | }; 703 | CandidatePortPrefixes = Temp; 704 | } else { 705 | logger.fine("No valid prefixes for serial ports have been entered for " + RXTXVersion.getOsName() + " in RXTXCommDriver.java. This may just be a typo in the method registerScanPorts()."); 706 | } 707 | break; 708 | 709 | case CommPortIdentifier.PORT_PARALLEL: 710 | logger.fine("scanning for parallel ports for os " + RXTXVersion.getOsName()); 711 | /** 712 | * Get the Parallel port prefixes for the running os Holger 713 | * Lehmann July 12, 1999 IBM 714 | */ 715 | if (RXTXVersion.getOsName().equals("Linux") /* 716 | || osName.equals("NetBSD") FIXME 717 | || osName.equals("HP-UX") FIXME 718 | || osName.equals("Irix") FIXME 719 | || osName.equals("BeOS") FIXME 720 | || osName.equals("Compaq's Digital UNIX") FIXME 721 | */) { 722 | String[] temp = { 723 | "lp" // linux printer port 724 | }; 725 | CandidatePortPrefixes = temp; 726 | } else if (RXTXVersion.getOsName().equals("FreeBSD")) { 727 | String[] temp = { 728 | "lpt" 729 | }; 730 | CandidatePortPrefixes = temp; 731 | } else if (RXTXVersion.getOsName().toLowerCase().indexOf("windows") != -1) { 732 | String[] temp = { 733 | "LPT" 734 | }; 735 | CandidatePortPrefixes = temp; 736 | } else /* printer support is green */ { 737 | String[] temp = {}; 738 | CandidatePortPrefixes = temp; 739 | } 740 | break; 741 | default: 742 | logger.fine("Unknown PortType " + PortType + " passed to RXTXCommDriver.registerScannedPorts()"); 743 | } 744 | registerValidPorts(CandidateDeviceNames, CandidatePortPrefixes, PortType); 745 | } 746 | 747 | 748 | /* 749 | *

    From the NullDriver.java CommAPI sample. 750 | */ 751 | /** 752 | * @param PortName The name of the port the OS recognizes 753 | * @param PortType CommPortIdentifier.PORT_SERIAL or PORT_PARALLEL 754 | * @return CommPort getCommPort() will be called by CommPortIdentifier from 755 | * its openPort() method. PortName is a string that was registered earlier 756 | * using the CommPortIdentifier.addPortName() method. getCommPort() returns 757 | * an object that extends either SerialPort or ParallelPort. 758 | */ 759 | @Override 760 | public CommPort getCommPort(String PortName, int PortType) { 761 | logger.fine("RXTXCommDriver:getCommPort(" + PortName + "," + PortType + ")"); 762 | try { 763 | switch (PortType) { 764 | case CommPortIdentifier.PORT_SERIAL: 765 | if (RXTXVersion.getOsName().toLowerCase().indexOf("windows") == -1) { 766 | return new RXTXPort(PortName); 767 | } else { 768 | return new RXTXPort(deviceDirectory + PortName); 769 | } 770 | // case CommPortIdentifier.PORT_PARALLEL: 771 | // return new LPRPort(PortName); 772 | default: 773 | logger.fine("unknown PortType " + PortType + " passed to RXTXCommDriver.getCommPort()"); 774 | 775 | } 776 | } catch (PortInUseException e) { 777 | logger.fine("Port " + PortName + " in use by another application"); 778 | } 779 | return null; 780 | } 781 | 782 | /* Yikes. Trying to call println from C for odd reasons */ 783 | public void Report(String arg) { 784 | System.out.println(arg); 785 | } 786 | } 787 | --------------------------------------------------------------------------------