Skip to content

Adds getPortProperties for Windows #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: processing
Choose a base branch
from
1,471 changes: 920 additions & 551 deletions src/cpp/windows/jssc.c++

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions src/cpp/windows/jssc_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* jSSC (Java Simple Serial Connector) - serial port communication library.
* Roman Belkov <roman.belkov@gmail.com>, 2017
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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 | https://github.com/scream3r/java-simple-serial-connector
*/

#include <string>
#include <vector>

#include <jni.h>
#include <stdlib.h>
#include <windows.h>

#include <initguid.h>
#include <devguid.h>
#include <winioctl.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <ntddmodm.h>

static std::wstring deviceRegistryProperty(HDEVINFO deviceInfoSet,
PSP_DEVINFO_DATA deviceInfoData,
DWORD property);

static std::wstring deviceDescription(HDEVINFO deviceInfoSet,
PSP_DEVINFO_DATA deviceInfoData);

static std::wstring busProvidedDescription(HDEVINFO deviceInfoSet,
PSP_DEVINFO_DATA deviceInfoData);

static std::wstring deviceManufacturer(HDEVINFO deviceInfoSet,
PSP_DEVINFO_DATA deviceInfoData);

static std::wstring deviceInstanceIdentifier(DEVINST deviceInstanceNumber);

static DEVINST parentDeviceInstanceNumber(DEVINST childDeviceInstanceNumber);

static std::wstring parseDeviceSerialNumber(const std::wstring &instanceIdentifier);

static std::wstring deviceSerialNumber(std::wstring instanceIdentifier,
DEVINST deviceInstanceNumber);

static uint16_t parseDeviceIdentifier(const std::wstring &instanceIdentifier,
const std::wstring &identifierPrefix,
int identifierSize, bool &ok);

static uint16_t deviceVendorIdentifier(const std::wstring &instanceIdentifier, bool &ok);

static uint16_t deviceProductIdentifier(const std::wstring &instanceIdentifier, bool &ok);

static bool anyOfPorts(const std::vector<std::wstring> &ports, const std::wstring &portName);

static std::wstring devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData);

static jstring wstr2jstr(JNIEnv *env, std::wstring cstr);

std::wstring jstr2wstr(JNIEnv *env, jstring string);

static std::wstring trimAfterFirstNull(std::wstring str);

static void addWStringToJavaArray(JNIEnv *env, jobjectArray objArray, int pos, std::wstring str);

template <typename I> std::wstring intToString(I num);
1 change: 1 addition & 0 deletions src/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ public class SerialPort {
public static final int BAUDRATE_57600 = 57600;
public static final int BAUDRATE_115200 = 115200;
public static final int BAUDRATE_128000 = 128000;
public static final int BAUDRATE_230400 = 230400;
public static final int BAUDRATE_256000 = 256000;


39 changes: 22 additions & 17 deletions src/java/jssc/SerialPortList.java
Original file line number Diff line number Diff line change
@@ -353,19 +353,17 @@ else if(portHandle != SerialNativeInterface.ERR_PORT_BUSY) {
}

public static Map<String, String> getPortProperties(String portName) {
if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX) {
int osType = SerialNativeInterface.getOsType();
if(osType == SerialNativeInterface.OS_LINUX) {
return getLinuxPortProperties(portName);
} else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
} else if(osType == SerialNativeInterface.OS_MAC_OS_X || osType == SerialNativeInterface.OS_WINDOWS) {
return getNativePortProperties(portName);
} else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){
// TODO
return new HashMap<String, String>();
} else {
return new HashMap<String, String>();
}
}

public static Map<String, String> getLinuxPortProperties(String portName) {
private static Map<String, String> getLinuxPortProperties(String portName) {
Map<String, String> props = new HashMap<String, String>();
try {
// portName has the format /dev/ttyUSB0
@@ -376,19 +374,20 @@ public static Map<String, String> getLinuxPortProperties(String portName) {
String[] sysfsPath = sysfsNode.getCanonicalPath().split("/");

// walk the tree to the root
for (int i=sysfsPath.length-2; 0 < i; i--) {
String curPath = "/";
for (int j=1; j <= i; j++) {
curPath += sysfsPath[j]+"/";
for (int i = sysfsPath.length-2; 0 < i; i--) {
StringBuilder curPath = new StringBuilder("/");
for (int j = 1; j <= i; j++) {
curPath.append(sysfsPath[j]).append("/");
}

// look for specific attributes
String[] attribs = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
for (int j=0; j < attribs.length; j++) {
String[] attributes = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
for (String attribute : attributes) {
try {
Scanner in = new Scanner(new FileReader(curPath+attribs[j]));
Scanner in = new Scanner(new FileReader(curPath + attribute));
in.useDelimiter(System.getProperty("line.separator"));
// we treat the values just as strings
props.put(attribs[j], in.next());
props.put(attribute, in.next());
} catch (Exception e) {
// ignore the attribute
}
@@ -405,14 +404,20 @@ public static Map<String, String> getLinuxPortProperties(String portName) {
return props;
}

public static Map<String, String> getNativePortProperties(String portName) {
private static Map<String, String> getNativePortProperties(String portName) {
Map<String, String> props = new HashMap<String, String>();
try {
String[] names;
if (SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS) {
names = new String[] { "idProduct", "idVendor", "manufacturer", "product", "busProvidedProduct", "serial" };
} else {
names = new String[] { "idProduct", "idVendor", "manufacturer", "product", "serial" };
}

// use JNI functions to read those properties
String[] names = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
String[] values = SerialNativeInterface.getPortProperties(portName);

for (int i=0; i < names.length; i++) {
for (int i = 0; i < names.length; i++) {
if (values[i] != null) {
props.put(names[i], values[i]);
}
Binary file modified src/java/libs/windows/jSSC-2.8_x86.dll
Binary file not shown.
Binary file modified src/java/libs/windows/jSSC-2.8_x86_64.dll
Binary file not shown.