diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..eb05694 --- /dev/null +++ b/pom.xml @@ -0,0 +1,198 @@ + + + 4.0.0 + org.scream3r + jssc + 2.8.1 + bundle + + jssc + + + UTF-8 + + + + + + org.apache.felix + maven-bundle-plugin + + + + + + + org.apache.felix + maven-scr-plugin + 1.9.0 + + ${project.build.outputDirectory} + + + + generate-scr-scrdescriptor + process-classes + + scr + + + + + + org.apache.felix + maven-bundle-plugin + true + 2.3.7 + + + bundle-manifest + generate-test-sources + + manifest + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + true + true + + -Xlint + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.apache.felix + maven-scr-plugin + [1.0.0,) + + scr + + + + + true + + + + + + + org.apache.felix + maven-bundle-plugin + [0.0,) + + generate-test-sources + manifest + + + + + true + + + + + + + + + com.googlecode.maven-java-formatter-plugin + maven-java-formatter-plugin + 0.3.1 + + ${format.file} + UTF-8 + LF + + **/src/test/** + + + + + + format + + validate + + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + attach-javadocs + + jar + + + + + + + + + + + + generate-sources-and-javadocs + + + + org.apache.maven.plugins + maven-source-plugin + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + + + + + junit + junit + 4.11 + test + jar + + + \ No newline at end of file diff --git a/src/java/jssc/SerialNativeInterface.java b/src/java/jssc/SerialNativeInterface.java deleted file mode 100644 index c5264f5..0000000 --- a/src/java/jssc/SerialNativeInterface.java +++ /dev/null @@ -1,486 +0,0 @@ -/* jSSC (Java Simple Serial Connector) - serial port communication library. - * © Alexey Sokolov (scream3r), 2010-2014. - * - * This file is part of jSSC. - * - * jSSC is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * jSSC is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with jSSC. If not, see . - * - * If you use jSSC in public project you can inform me about this by e-mail, - * of course if you want it. - * - * e-mail: scream3r.org@gmail.com - * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ - */ -package jssc; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; - -/** - * - * @author scream3r - */ -public class SerialNativeInterface { - - private static final String libVersion = "2.8"; //jSSC-2.8.0 Release from 24.01.2014 - private static final String libMinorSuffix = "0"; //since 0.9.0 - - public static final int OS_LINUX = 0; - public static final int OS_WINDOWS = 1; - public static final int OS_SOLARIS = 2;//since 0.9.0 - public static final int OS_MAC_OS_X = 3;//since 0.9.0 - - private static int osType = -1; - - /** - * @since 2.3.0 - */ - public static final long ERR_PORT_BUSY = -1; - /** - * @since 2.3.0 - */ - public static final long ERR_PORT_NOT_FOUND = -2; - /** - * @since 2.3.0 - */ - public static final long ERR_PERMISSION_DENIED = -3; - /** - * @since 2.3.0 - */ - public static final long ERR_INCORRECT_SERIAL_PORT = -4; - - /** - * @since 2.6.0 - */ - public static final String PROPERTY_JSSC_NO_TIOCEXCL = "JSSC_NO_TIOCEXCL"; - /** - * @since 2.6.0 - */ - public static final String PROPERTY_JSSC_IGNPAR = "JSSC_IGNPAR"; - /** - * @since 2.6.0 - */ - public static final String PROPERTY_JSSC_PARMRK = "JSSC_PARMRK"; - - static { - String libFolderPath; - String libName; - - String osName = System.getProperty("os.name"); - String architecture = System.getProperty("os.arch"); - String userHome = System.getProperty("user.home"); - String fileSeparator = System.getProperty("file.separator"); - String tmpFolder = System.getProperty("java.io.tmpdir"); - - //since 2.3.0 -> - String libRootFolder = new File(userHome).canWrite() ? userHome : tmpFolder; - //<- since 2.3.0 - - String javaLibPath = System.getProperty("java.library.path");//since 2.1.0 - - if(osName.equals("Linux")){ - osName = "linux"; - osType = OS_LINUX; - } - else if(osName.startsWith("Win")){ - osName = "windows"; - osType = OS_WINDOWS; - }//since 0.9.0 -> - else if(osName.equals("SunOS")){ - osName = "solaris"; - osType = OS_SOLARIS; - } - else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin" since 2.6.0 - osName = "mac_os_x"; - osType = OS_MAC_OS_X; - }//<- since 0.9.0 - - if(architecture.equals("i386") || architecture.equals("i686")){ - architecture = "x86"; - } - else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0 - architecture = "x86_64"; - } - else if(architecture.equals("arm")) {//since 2.1.0 - String floatStr = "sf"; - if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){ - floatStr = "hf"; - } - else { - try { - Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe"); - BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream())); - String buffer = ""; - while((buffer = reader.readLine()) != null && !buffer.isEmpty()){ - if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){ - floatStr = "hf"; - break; - } - } - reader.close(); - } - catch (Exception ex) { - //Do nothing - } - } - architecture = "arm" + floatStr; - } - - libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName; - libName = "jSSC-" + libVersion + "_" + architecture; - libName = System.mapLibraryName(libName); - - if(libName.endsWith(".dylib")){//Since 2.1.0 MacOSX 10.8 fix - libName = libName.replace(".dylib", ".jnilib"); - } - - boolean loadLib = false; - - if(isLibFolderExist(libFolderPath)){ - if(isLibFileExist(libFolderPath + fileSeparator + libName)){ - loadLib = true; - } - else { - if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ - loadLib = true; - } - } - } - else { - if(new File(libFolderPath).mkdirs()){ - if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ - loadLib = true; - } - } - } - - if (loadLib) { - System.load(libFolderPath + fileSeparator + libName); - String versionBase = getLibraryBaseVersion(); - String versionNative = getNativeLibraryVersion(); - if (!versionBase.equals(versionNative)) { - System.err.println("Warning! jSSC Java and Native versions mismatch (Java: " + versionBase + ", Native: " + versionNative + ")"); - } - } - } - - /** - * Is library folder exists - * - * @param libFolderPath - * - * @since 0.8 - */ - private static boolean isLibFolderExist(String libFolderPath) { - boolean returnValue = false; - File folder = new File(libFolderPath); - if(folder.exists() && folder.isDirectory()){ - returnValue = true; - } - return returnValue; - } - - /** - * Is library file exists - * - * @param libFilePath - * - * @since 0.8 - */ - private static boolean isLibFileExist(String libFilePath) { - boolean returnValue = false; - File folder = new File(libFilePath); - if(folder.exists() && folder.isFile()){ - returnValue = true; - } - return returnValue; - } - - /** - * Extract lib to lib folder - * - * @param libFilePath - * @param osName - * @param libName - * - * @since 0.8 - */ - private static boolean extractLib(String libFilePath, String osName, String libName) { - boolean returnValue = false; - File libFile = new File(libFilePath); - InputStream input = null; - FileOutputStream output = null; - input = SerialNativeInterface.class.getResourceAsStream("/libs/" + osName + "/" + libName); - if(input != null){ - int read; - byte[] buffer = new byte[4096]; - try { - output = new FileOutputStream(libFilePath); - while((read = input.read(buffer)) != -1){ - output.write(buffer, 0, read); - } - output.close(); - input.close(); - returnValue = true; - } - catch (Exception ex) { - try { - output.close(); - if(libFile.exists()){ - libFile.delete(); - } - } - catch (Exception ex_out) { - //Do nothing - } - try { - input.close(); - } - catch (Exception ex_in) { - //Do nothing - } - } - } - return returnValue; - } - - /** - * Get OS type (OS_LINUX || OS_WINDOWS || OS_SOLARIS) - * - * @since 0.8 - */ - public static int getOsType() { - return osType; - } - - /** - * Get jSSC version. The version of library is Base Version + Minor Suffix - * - * @since 0.8 - */ - public static String getLibraryVersion() { - return libVersion + "." + libMinorSuffix; - } - - /** - * Get jSSC Base Version - * - * @since 0.9.0 - */ - public static String getLibraryBaseVersion() { - return libVersion; - } - - /** - * Get jSSC minor suffix. For example in version 0.8.1 - 1 is a minor suffix - * - * @since 0.9.0 - */ - public static String getLibraryMinorSuffix() { - return libMinorSuffix; - } - - /** - * Get jSSC native library version - * - * @return native lib version (for jSSC-2.8.0 should be 2.8 for example) - * - * @since 2.8.0 - */ - public static native String getNativeLibraryVersion(); - - /** - * Open port - * - * @param portName name of port for opening - * @param useTIOCEXCL enable/disable using of TIOCEXCL. Take effect only on *nix based systems - * - * @return handle of opened port or -1 if opening of the port was unsuccessful - */ - public native long openPort(String portName, boolean useTIOCEXCL); - - /** - * Setting the parameters of opened port - * - * @param handle handle of opened port - * @param baudRate data transfer rate - * @param dataBits number of data bits - * @param stopBits number of stop bits - * @param parity parity - * @param setRTS initial state of RTS line (ON/OFF) - * @param setDTR initial state of DTR line (ON/OFF) - * @param flags additional Native settings. Take effect only on *nix based systems - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean setParams(long handle, int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR, int flags); - - /** - * Purge of input and output buffer - * - * @param handle handle of opened port - * @param flags flags specifying required actions for purgePort method - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean purgePort(long handle, int flags); - - /** - * Close port - * - * @param handle handle of opened port - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean closePort(long handle); - - /** - * Set events mask - * - * @param handle handle of opened port - * @param mask events mask - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean setEventsMask(long handle, int mask); - - /** - * Get events mask - * - * @param handle handle of opened port - * - * @return Method returns event mask as a variable of int type - */ - public native int getEventsMask(long handle); - - /** - * Wait events - * - * @param handle handle of opened port - * - * @return Method returns two-dimensional array containing event types and their values - * (events[i][0] - event type, events[i][1] - event value). - */ - public native int[][] waitEvents(long handle); - - /** - * Change RTS line state - * - * @param handle handle of opened port - * @param value true - ON, false - OFF - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean setRTS(long handle, boolean value); - - /** - * Change DTR line state - * - * @param handle handle of opened port - * @param value true - ON, false - OFF - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean setDTR(long handle, boolean value); - - /** - * Read data from port - * - * @param handle handle of opened port - * @param byteCount count of bytes required to read - * - * @return Method returns the array of read bytes - */ - public native byte[] readBytes(long handle, int byteCount); - - /** - * Write data to port - * - * @param handle handle of opened port - * @param buffer array of bytes to write - * - * @return If the operation is successfully completed, the method returns true, otherwise false - */ - public native boolean writeBytes(long handle, byte[] buffer); - - /** - * Get bytes count in buffers of port - * - * @param handle handle of opened port - * - * @return Method returns the array that contains info about bytes count in buffers: - *
element 0 - input buffer
- *
element 1 - output buffer
- * - * @since 0.8 - */ - public native int[] getBuffersBytesCount(long handle); - - /** - * Set flow control mode - * - * @param handle handle of opened port - * @param mask mask of flow control mode - * - * @return If the operation is successfully completed, the method returns true, otherwise false - * - * @since 0.8 - */ - public native boolean setFlowControlMode(long handle, int mask); - - /** - * Get flow control mode - * - * @param handle handle of opened port - * - * @return Mask of setted flow control mode - * - * @since 0.8 - */ - public native int getFlowControlMode(long handle); - - /** - * Get serial port names like an array of String - * - * @return unsorted array of String with port names - */ - public native String[] getSerialPortNames(); - - /** - * Getting lines states - * - * @param handle handle of opened port - * - * @return Method returns the array containing information about lines in following order: - *
element 0 - CTS line state
- *
element 1 - DSR line state
- *
element 2 - RING line state
- *
element 3 - RLSD line state
- */ - public native int[] getLinesStatus(long handle); - - /** - * Send Break singnal for setted duration - * - * @param handle handle of opened port - * @param duration duration of Break signal - * @return If the operation is successfully completed, the method returns true, otherwise false - * - * @since 0.8 - */ - public native boolean sendBreak(long handle, int duration); -} diff --git a/src/java/libs/windows/jSSC-2.8_x86.dll b/src/java/libs/windows/jSSC-2.8_x86.dll deleted file mode 100644 index 8ff7541..0000000 Binary files a/src/java/libs/windows/jSSC-2.8_x86.dll and /dev/null differ diff --git a/src/java/libs/windows/jSSC-2.8_x86_64.dll b/src/java/libs/windows/jSSC-2.8_x86_64.dll deleted file mode 100644 index d4e19e0..0000000 Binary files a/src/java/libs/windows/jSSC-2.8_x86_64.dll and /dev/null differ diff --git a/src/cpp/_nix_based/jssc.cpp b/src/main/cpp/_nix_based/jssc.cpp similarity index 100% rename from src/cpp/_nix_based/jssc.cpp rename to src/main/cpp/_nix_based/jssc.cpp diff --git a/src/cpp/jssc_SerialNativeInterface.h b/src/main/cpp/jssc_SerialNativeInterface.h similarity index 100% rename from src/cpp/jssc_SerialNativeInterface.h rename to src/main/cpp/jssc_SerialNativeInterface.h diff --git a/src/cpp/windows/jssc.c++ b/src/main/cpp/windows/jssc.c++ similarity index 100% rename from src/cpp/windows/jssc.c++ rename to src/main/cpp/windows/jssc.c++ diff --git a/src/main/java/jssc/SerialNativeAccess.java b/src/main/java/jssc/SerialNativeAccess.java new file mode 100644 index 0000000..f8c1636 --- /dev/null +++ b/src/main/java/jssc/SerialNativeAccess.java @@ -0,0 +1,271 @@ +/* jSSC (Java Simple Serial Connector) - serial port communication library. + * © Alexey Sokolov (scream3r), 2010-2014. + * + * This file is part of jSSC. + * + * jSSC is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * jSSC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with jSSC. If not, see . + * + * If you use jSSC in public project you can inform me about this by e-mail, + * of course if you want it. + * + * e-mail: scream3r.org@gmail.com + * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ + */ +package jssc; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * + * @author scream3r, vogt31337@googlemail.com + */ + +public class SerialNativeAccess { + private int osType = -1; + private static SerialNativeInterface sni = new SerialNativeInterface(); + private static SerialNativeAccess instance = null; + + public static SerialNativeAccess getInstance() { + if (SerialNativeAccess.instance == null) { + SerialNativeAccess.instance = new SerialNativeAccess(); + } + return SerialNativeAccess.instance; + } + + public SerialNativeAccess() { + String libFolderPath; + String libName; + + String osName = System.getProperty("os.name"); + String architecture = System.getProperty("os.arch"); + String userHome = System.getProperty("user.home"); + String fileSeparator = System.getProperty("file.separator"); + String tmpFolder = System.getProperty("java.io.tmpdir"); + + //since 2.3.0 -> + String libRootFolder = new File(userHome).canWrite() ? userHome : tmpFolder; + //<- since 2.3.0 + + String javaLibPath = System.getProperty("java.library.path");//since 2.1.0 + + if(osName.equals("Linux")){ + osName = "linux"; + osType = SerialNativeInterface.OS_LINUX; + } + else if(osName.startsWith("Win")){ + osName = "windows"; + osType = SerialNativeInterface.OS_WINDOWS; + }//since 0.9.0 -> + else if(osName.equals("SunOS")){ + osName = "solaris"; + osType = SerialNativeInterface.OS_SOLARIS; + } + else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin" since 2.6.0 + osName = "mac_os_x"; + osType = SerialNativeInterface.OS_MAC_OS_X; + }//<- since 0.9.0 + + if(architecture.equals("i386") || architecture.equals("i686")){ + architecture = "x86"; + } + else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0 + architecture = "x86_64"; + } + else if(architecture.equals("arm")) {//since 2.1.0 + String floatStr = "sf"; + if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){ + floatStr = "hf"; + } + else { + try { + Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe"); + BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream())); + String buffer = ""; + while((buffer = reader.readLine()) != null && !buffer.isEmpty()){ + if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){ + floatStr = "hf"; + break; + } + } + reader.close(); + } + catch (Exception ex) { + //Do nothing + } + } + architecture = "arm" + floatStr; + } + + libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName; + libName = "jSSC-" + SerialNativeInterface.libVersion + "_" + architecture; + libName = System.mapLibraryName(libName); + + if(libName.endsWith(".dylib")){//Since 2.1.0 MacOSX 10.8 fix + libName = libName.replace(".dylib", ".jnilib"); + } + + boolean loadLib = false; + + if(isLibFolderExist(libFolderPath)){ + if(isLibFileExist(libFolderPath + fileSeparator + libName)){ + loadLib = true; + } + else { + if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ + loadLib = true; + } + } + } + else { + if(new File(libFolderPath).mkdirs()){ + if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ + loadLib = true; + } + } + } + + if (loadLib) { + System.load(libFolderPath + fileSeparator + libName); + String versionBase = SerialNativeInterface.libVersion; + String versionNative = sni.getNativeLibraryVersion(); + if (!versionBase.equals(versionNative)) { + System.err.println("Warning! jSSC Java and Native versions mismatch (Java: " + versionBase + ", Native: " + versionNative + ")"); + } + } + } + + /** + * Is library folder exists + * + * @param libFolderPath + * + * @since 0.8 + */ + private static boolean isLibFolderExist(String libFolderPath) { + boolean returnValue = false; + File folder = new File(libFolderPath); + if(folder.exists() && folder.isDirectory()){ + returnValue = true; + } + return returnValue; + } + + /** + * Is library file exists + * + * @param libFilePath + * + * @since 0.8 + */ + private static boolean isLibFileExist(String libFilePath) { + boolean returnValue = false; + File folder = new File(libFilePath); + if(folder.exists() && folder.isFile()){ + returnValue = true; + } + return returnValue; + } + + /** + * Extract lib to lib folder + * + * @param libFilePath + * @param osName + * @param libName + * + * @since 0.8 + */ + private static boolean extractLib(String libFilePath, String osName, String libName) { + boolean returnValue = false; + File libFile = new File(libFilePath); + InputStream input = null; + FileOutputStream output = null; + input = SerialNativeInterface.class.getResourceAsStream("/libs/" + osName + "/" + libName); + if(input != null){ + int read; + byte[] buffer = new byte[4096]; + try { + output = new FileOutputStream(libFilePath); + while((read = input.read(buffer)) != -1){ + output.write(buffer, 0, read); + } + output.close(); + input.close(); + returnValue = true; + } + catch (Exception ex) { + try { + output.close(); + if(libFile.exists()){ + libFile.delete(); + } + } + catch (Exception ex_out) { + //Do nothing + } + try { + input.close(); + } + catch (Exception ex_in) { + //Do nothing + } + } + } + return returnValue; + } + + /** + * Get OS type (OS_LINUX || OS_WINDOWS || OS_SOLARIS) + * + * @since 0.8 + */ + public int getOsType() { + return osType; + } + + public SerialNativeInterface getInterface() { + return sni; + } + + /** + * Get jSSC version. The version of library is Base Version + Minor Suffix + * + * @since 0.8 + */ +// public String getLibraryVersion() { +// return libVersion + "." + libMinorSuffix; +// } + + /** + * Get jSSC Base Version + * + * @since 0.9.0 + */ +// public String getLibraryBaseVersion() { +// return libVersion; +// } + + /** + * Get jSSC minor suffix. For example in version 0.8.1 - 1 is a minor suffix + * + * @since 0.9.0 + */ +// public String getLibraryMinorSuffix() { +// return libMinorSuffix; +// } +} \ No newline at end of file diff --git a/src/main/java/jssc/SerialNativeInterface.java b/src/main/java/jssc/SerialNativeInterface.java new file mode 100644 index 0000000..6bd8922 --- /dev/null +++ b/src/main/java/jssc/SerialNativeInterface.java @@ -0,0 +1,241 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package jssc; + +/** + * + * @author mvogt + */ +public class SerialNativeInterface { + static final String libVersion = "2.8"; //jSSC-2.8.0 Release from 24.01.2014 + static final String libMinorSuffix = "0"; //since 0.9.0 + + public static final int OS_LINUX = 0; + public static final int OS_WINDOWS = 1; + public static final int OS_SOLARIS = 2;//since 0.9.0 + public static final int OS_MAC_OS_X = 3;//since 0.9.0 + + /** + * @since 2.3.0 + */ + public static final long ERR_PORT_BUSY = -1; + /** + * @since 2.3.0 + */ + public static final long ERR_PORT_NOT_FOUND = -2; + /** + * @since 2.3.0 + */ + public static final long ERR_PERMISSION_DENIED = -3; + /** + * @since 2.3.0 + */ + public static final long ERR_INCORRECT_SERIAL_PORT = -4; + + /** + * @since 2.6.0 + */ + public static final String PROPERTY_JSSC_NO_TIOCEXCL = "JSSC_NO_TIOCEXCL"; + /** + * @since 2.6.0 + */ + public static final String PROPERTY_JSSC_IGNPAR = "JSSC_IGNPAR"; + /** + * @since 2.6.0 + */ + public static final String PROPERTY_JSSC_PARMRK = "JSSC_PARMRK"; + + /** + * Get jSSC library version + * + * @return lib version (for jSSC-2.8.0 should be 2.8 for example) + * + * @since 2.8.0 + */ + public native String getNativeLibraryVersion(); + + /** + * Open port + * + * @param portName name of port for opening + * @param useTIOCEXCL enable/disable using of TIOCEXCL. Take effect only on *nix based systems + * + * @return handle of opened port or -1 if opening of the port was unsuccessful + */ + public native long openPort(String portName, boolean useTIOCEXCL); + + /** + * Setting the parameters of opened port + * + * @param handle handle of opened port + * @param baudRate data transfer rate + * @param dataBits number of data bits + * @param stopBits number of stop bits + * @param parity parity + * @param setRTS initial state of RTS line (ON/OFF) + * @param setDTR initial state of DTR line (ON/OFF) + * @param flags additional Native settings. Take effect only on *nix based systems + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean setParams(long handle, int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR, int flags); + + /** + * Purge of input and output buffer + * + * @param handle handle of opened port + * @param flags flags specifying required actions for purgePort method + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean purgePort(long handle, int flags); + + /** + * Close port + * + * @param handle handle of opened port + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean closePort(long handle); + + /** + * Set events mask + * + * @param handle handle of opened port + * @param mask events mask + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean setEventsMask(long handle, int mask); + + /** + * Get events mask + * + * @param handle handle of opened port + * + * @return Method returns event mask as a variable of int type + */ + public native int getEventsMask(long handle); + + /** + * Wait events + * + * @param handle handle of opened port + * + * @return Method returns two-dimensional array containing event types and their values + * (events[i][0] - event type, events[i][1] - event value). + */ + public native int[][] waitEvents(long handle); + + /** + * Change RTS line state + * + * @param handle handle of opened port + * @param value true - ON, false - OFF + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean setRTS(long handle, boolean value); + + /** + * Change DTR line state + * + * @param handle handle of opened port + * @param value true - ON, false - OFF + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean setDTR(long handle, boolean value); + + /** + * Read data from port + * + * @param handle handle of opened port + * @param byteCount count of bytes required to read + * + * @return Method returns the array of read bytes + */ + public native byte[] readBytes(long handle, int byteCount); + + /** + * Write data to port + * + * @param handle handle of opened port + * @param buffer array of bytes to write + * + * @return If the operation is successfully completed, the method returns true, otherwise false + */ + public native boolean writeBytes(long handle, byte[] buffer); + + /** + * Get bytes count in buffers of port + * + * @param handle handle of opened port + * + * @return Method returns the array that contains info about bytes count in buffers: + *
element 0 - input buffer
+ *
element 1 - output buffer
+ * + * @since 0.8 + */ + public native int[] getBuffersBytesCount(long handle); + + /** + * Set flow control mode + * + * @param handle handle of opened port + * @param mask mask of flow control mode + * + * @return If the operation is successfully completed, the method returns true, otherwise false + * + * @since 0.8 + */ + public native boolean setFlowControlMode(long handle, int mask); + + /** + * Get flow control mode + * + * @param handle handle of opened port + * + * @return Mask of setted flow control mode + * + * @since 0.8 + */ + public native int getFlowControlMode(long handle); + + /** + * Get serial port names like an array of String + * + * @return unsorted array of String with port names + */ + public native String[] getSerialPortNames(); + + /** + * Getting lines states + * + * @param handle handle of opened port + * + * @return Method returns the array containing information about lines in following order: + *
element 0 - CTS line state
+ *
element 1 - DSR line state
+ *
element 2 - RING line state
+ *
element 3 - RLSD line state
+ */ + public native int[] getLinesStatus(long handle); + + /** + * Send Break singnal for setted duration + * + * @param handle handle of opened port + * @param duration duration of Break signal + * @return If the operation is successfully completed, the method returns true, otherwise false + * + * @since 0.8 + */ + public native boolean sendBreak(long handle, int duration); +} diff --git a/src/java/jssc/SerialPort.java b/src/main/java/org/scream3r/jssc/SerialPort.java similarity index 97% rename from src/java/jssc/SerialPort.java rename to src/main/java/org/scream3r/jssc/SerialPort.java index e5f43b4..613dce3 100644 --- a/src/java/jssc/SerialPort.java +++ b/src/main/java/org/scream3r/jssc/SerialPort.java @@ -22,11 +22,12 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; -import java.nio.charset.Charset; +import jssc.SerialNativeAccess; +import jssc.SerialNativeInterface; /** * @@ -117,7 +118,7 @@ public class SerialPort { public SerialPort(String portName) { this.portName = portName; - serialInterface = new SerialNativeInterface(); + serialInterface = SerialNativeAccess.getInstance().getInterface(); } /** @@ -262,9 +263,9 @@ public class SerialPort { */ public boolean setEventsMask(int mask) throws SerialPortException { checkPortOpened("setEventsMask()"); - if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 + if(SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_LINUX || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_SOLARIS || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 linuxMask = mask; if(mask > 0){ maskAssigned = true; @@ -296,9 +297,9 @@ public class SerialPort { */ public int getEventsMask() throws SerialPortException { checkPortOpened("getEventsMask()"); - if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 + if(SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_LINUX || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_SOLARIS || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 return linuxMask; } return serialInterface.getEventsMask(portHandle); @@ -1039,9 +1040,9 @@ public class SerialPort { * @since 0.8 */ private EventThread getNewEventThread() { - if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS || - SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 + if(SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_LINUX || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_SOLARIS || + SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 return new LinuxEventThread(); } return new EventThread(); diff --git a/src/java/jssc/SerialPortEvent.java b/src/main/java/org/scream3r/jssc/SerialPortEvent.java similarity index 99% rename from src/java/jssc/SerialPortEvent.java rename to src/main/java/org/scream3r/jssc/SerialPortEvent.java index 01ec732..badb837 100644 --- a/src/java/jssc/SerialPortEvent.java +++ b/src/main/java/org/scream3r/jssc/SerialPortEvent.java @@ -22,7 +22,7 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; /** * diff --git a/src/java/jssc/SerialPortEventListener.java b/src/main/java/org/scream3r/jssc/SerialPortEventListener.java similarity index 97% rename from src/java/jssc/SerialPortEventListener.java rename to src/main/java/org/scream3r/jssc/SerialPortEventListener.java index 9a2441a..4fdbb1e 100644 --- a/src/java/jssc/SerialPortEventListener.java +++ b/src/main/java/org/scream3r/jssc/SerialPortEventListener.java @@ -22,7 +22,7 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; /** * diff --git a/src/java/jssc/SerialPortException.java b/src/main/java/org/scream3r/jssc/SerialPortException.java similarity index 95% rename from src/java/jssc/SerialPortException.java rename to src/main/java/org/scream3r/jssc/SerialPortException.java index 18aca2f..8b3ad64 100644 --- a/src/java/jssc/SerialPortException.java +++ b/src/main/java/org/scream3r/jssc/SerialPortException.java @@ -22,14 +22,16 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; /** * - * @author scream3r + * @author scream3r, vogt31337@googlemail.com */ public class SerialPortException extends Exception { + final static long serialVersionUID = 5843574354687324684l; + final public static String TYPE_PORT_ALREADY_OPENED = "Port already opened"; final public static String TYPE_PORT_NOT_OPENED = "Port not opened"; final public static String TYPE_CANT_SET_MASK = "Can't set mask"; diff --git a/src/java/jssc/SerialPortList.java b/src/main/java/org/scream3r/jssc/SerialPortList.java similarity index 97% rename from src/java/jssc/SerialPortList.java rename to src/main/java/org/scream3r/jssc/SerialPortList.java index 5af9a95..05094a3 100644 --- a/src/java/jssc/SerialPortList.java +++ b/src/main/java/org/scream3r/jssc/SerialPortList.java @@ -22,16 +22,18 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; import java.io.File; import java.util.Comparator; import java.util.TreeSet; import java.util.regex.Pattern; +import jssc.SerialNativeAccess; +import jssc.SerialNativeInterface; /** * - * @author scream3r + * @author scream3r, vogt31337@googlemail.com */ public class SerialPortList { @@ -40,8 +42,8 @@ public class SerialPortList { private static final String PORTNAMES_PATH; static { - serialInterface = new SerialNativeInterface(); - switch (SerialNativeInterface.getOsType()) { + serialInterface = SerialNativeAccess.getInstance().getInterface(); + switch (SerialNativeAccess.getInstance().getOsType()) { case SerialNativeInterface.OS_LINUX: { PORTNAMES_REGEXP = Pattern.compile("(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}"); PORTNAMES_PATH = "/dev/"; @@ -290,7 +292,7 @@ public class SerialPortList { if(searchPath == null || pattern == null || comparator == null){ return new String[]{}; } - if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){ + if(SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_WINDOWS){ return getWindowsPortNames(pattern, comparator); } return getUnixBasedPortNames(searchPath, pattern, comparator); diff --git a/src/java/jssc/SerialPortTimeoutException.java b/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java similarity index 93% rename from src/java/jssc/SerialPortTimeoutException.java rename to src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java index 802535c..5fb013b 100644 --- a/src/java/jssc/SerialPortTimeoutException.java +++ b/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java @@ -22,14 +22,16 @@ * e-mail: scream3r.org@gmail.com * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ */ -package jssc; +package org.scream3r.jssc; /** * - * @author scream3r + * @author scream3r, vogt31337@googlemail.com */ public class SerialPortTimeoutException extends Exception { + final static long serialVersionUID = -1584357967321684324l; + private String portName; private String methodName; private int timeoutValue; diff --git a/src/java/libs/linux/libjSSC-2.8_armhf.so b/src/main/resources/libs/linux/libjSSC-2.8_armhf.so similarity index 100% rename from src/java/libs/linux/libjSSC-2.8_armhf.so rename to src/main/resources/libs/linux/libjSSC-2.8_armhf.so diff --git a/src/java/libs/linux/libjSSC-2.8_armsf.so b/src/main/resources/libs/linux/libjSSC-2.8_armsf.so similarity index 100% rename from src/java/libs/linux/libjSSC-2.8_armsf.so rename to src/main/resources/libs/linux/libjSSC-2.8_armsf.so diff --git a/src/java/libs/linux/libjSSC-2.8_x86.so b/src/main/resources/libs/linux/libjSSC-2.8_x86.so similarity index 100% rename from src/java/libs/linux/libjSSC-2.8_x86.so rename to src/main/resources/libs/linux/libjSSC-2.8_x86.so diff --git a/src/java/libs/linux/libjSSC-2.8_x86_64.so b/src/main/resources/libs/linux/libjSSC-2.8_x86_64.so similarity index 100% rename from src/java/libs/linux/libjSSC-2.8_x86_64.so rename to src/main/resources/libs/linux/libjSSC-2.8_x86_64.so diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_ppc.jnilib b/src/main/resources/libs/mac_os_x/libjSSC-2.8_ppc.jnilib similarity index 100% rename from src/java/libs/mac_os_x/libjSSC-2.8_ppc.jnilib rename to src/main/resources/libs/mac_os_x/libjSSC-2.8_ppc.jnilib diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib b/src/main/resources/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib similarity index 100% rename from src/java/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib rename to src/main/resources/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_x86.jnilib b/src/main/resources/libs/mac_os_x/libjSSC-2.8_x86.jnilib similarity index 100% rename from src/java/libs/mac_os_x/libjSSC-2.8_x86.jnilib rename to src/main/resources/libs/mac_os_x/libjSSC-2.8_x86.jnilib diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib b/src/main/resources/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib similarity index 100% rename from src/java/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib rename to src/main/resources/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib diff --git a/src/java/libs/solaris/libjSSC-2.8_x86.so b/src/main/resources/libs/solaris/libjSSC-2.8_x86.so similarity index 100% rename from src/java/libs/solaris/libjSSC-2.8_x86.so rename to src/main/resources/libs/solaris/libjSSC-2.8_x86.so diff --git a/src/java/libs/solaris/libjSSC-2.8_x86_64.so b/src/main/resources/libs/solaris/libjSSC-2.8_x86_64.so similarity index 100% rename from src/java/libs/solaris/libjSSC-2.8_x86_64.so rename to src/main/resources/libs/solaris/libjSSC-2.8_x86_64.so diff --git a/src/test/java/org/scream3r/jssc/TestSerialPortList.java b/src/test/java/org/scream3r/jssc/TestSerialPortList.java new file mode 100644 index 0000000..87251c6 --- /dev/null +++ b/src/test/java/org/scream3r/jssc/TestSerialPortList.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package org.scream3r.jssc; + +import java.util.Arrays; +import junit.framework.TestCase; + +/** + * + * @author mvogt + */ +public class TestSerialPortList extends TestCase { + + public void testSerialPortListing() { + String[] out = SerialPortList.getPortNames(); + System.out.println(Arrays.toString(out)); + + // if we reached this point, loading the dynamic lib worked. + assertTrue(true); + } +} diff --git a/src/test/java/org/scream3r/jssc/TestSerialPortRead.java b/src/test/java/org/scream3r/jssc/TestSerialPortRead.java new file mode 100644 index 0000000..a941261 --- /dev/null +++ b/src/test/java/org/scream3r/jssc/TestSerialPortRead.java @@ -0,0 +1,47 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.scream3r.jssc; + +import junit.framework.TestCase; +import static junit.framework.TestCase.assertTrue; +import org.junit.Assert; + +/** + * + * @author mvogt + */ +public class TestSerialPortRead extends TestCase { + + private String findASerialPort() { + String[] out = SerialPortList.getPortNames(); + + if (out.length > 0) { + return out[0]; + } else { + return null; + } + } + + public void testSerialPortRead() { + String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux + + if (port != null) { + SerialPort serialPort = new SerialPort(port); + try { + serialPort.openPort();//Open serial port + serialPort.setParams(9600, 8, 1, 0);//Set params. + byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port + serialPort.closePort();//Close serial port + } catch (SerialPortException ex) { + System.out.println(ex); + } + } else { + Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?"); + } + assertTrue(true); + + } +} diff --git a/src/test/java/org/scream3r/jssc/TestSerialPortWrite.java b/src/test/java/org/scream3r/jssc/TestSerialPortWrite.java new file mode 100644 index 0000000..a35b812 --- /dev/null +++ b/src/test/java/org/scream3r/jssc/TestSerialPortWrite.java @@ -0,0 +1,48 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.scream3r.jssc; + +import junit.framework.TestCase; +import org.junit.Assert; + +/** + * + * @author mvogt + */ +public class TestSerialPortWrite extends TestCase { + + private String findASerialPort() { + String[] out = SerialPortList.getPortNames(); + + if (out.length > 0) { + return out[0]; + } else { + return null; + } + } + + public void testSerialPortWrite() { + String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux + + if (port != null) { + SerialPort serialPort = new SerialPort(port); + try { + serialPort.openPort();//Open serial port + serialPort.setParams(SerialPort.BAUDRATE_9600, + SerialPort.DATABITS_8, + SerialPort.STOPBITS_1, + SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0); + serialPort.writeBytes("This is a test string".getBytes());//Write data to port + serialPort.closePort();//Close serial port + } catch (SerialPortException ex) { + System.out.println(ex); + } + } else { + Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?"); + } + assertTrue(true); + } +}