Get SerialPort device descriptor using jssc

How to get SerialPort device descriptor using jssc ( java-simple-serial-connector) ?

The getPortName() method gives the port name ( eg COM2 ), but a handle will be more useful.

If you need to fix this open source API to get a device handle , how to do it?

+5
source share
3 answers

The device descriptor is the root of the descriptor tree and contains basic information about the device. Unique numbers, idVendor and idProduct identify the connected device. The Windows operating system uses these numbers to determine which device driver is loading.

idVendor is the number assigned to each company manufacturing USB devices. The USB Developer Forum is responsible for administering the assignment of vendor identifiers.

idProduct is another 16-bit field containing the number assigned by the manufacturer to identify a specific product.


From jantje question,

Do I need what windows 10 show as names in the connected device?

Ans: Open the "Settings app" and click on "Devices". Clicking on “Devices” will open a tab in which you can configure settings for all your printers, connected devices, Bluetooth devices, a mouse and touchpad, input settings and autorun settings. The Connected Devices tab shows the equipment connected to your PC. Click "Add device" and your computer will automatically scan connected devices . The Bluetooth tab is simple with simple settings to connect your device to a PC via Bluetooth. Press the Bluetooth button and the device will automatically start scanning for any Bluetooth device in the range.

If there are any problems when displaying devices or inaccessible, we need the following tasks.

  • Scan your hardware and devices for driver issues.
  • Repair a damaged, damaged, or incompatible Bluetooth driver.
  • Update the latest Bluetooth driver for Windows 10.
  • Download and install the most suitable driver in Windows 10.

Top 2. Remove and add Bluetooth devices

Top 3. Allow Bluetooth devices to connect to this computer

Can't connect to the computer from your Bluetooth device? Make sure that you allow Bluetooth devices to connect to your computer. Follow these steps: 1. Go to Control Panel. Click Hardware and Sound and Bluetooth Devices. 2. Click on the "Settings" tab. 3. Ensure that the "Allow Bluetooth devices to connect to this computer" checkbox is selected.


How to connect Bluetooth devices to a Windows 10 computer?

To connect a Bluetooth enabled mobile phone:

  • Go to the "Start" button and select "Settings", "Devices" and "Bluetooth". Click Add.
  • Alternatively, go to Control Panel, click on “Hardware and Sound” and “Bluetooth Devices”. Click Add.

Resource Link:


Fix network connectivity issues

There are 7 ways to fix network connectivity issues:

  • Update network adapter driver
  • Rollback the network adapter driver
  • Run the network troubleshooting tool, followed by the network commands.
  • Temporarily disable firewalls
  • Temporarily disable all anti-virus or malware programs.
  • Uninstall the network adapter driver and restart
  • Reinstall network devices using netcfg -d

UPDATE:

How does your answer refer to jssc?

I am checking your problem. I got this on Windows 7, it works fine, and on Windows 10 there are some hardware problems that cause problems.

Developers are working on this area. But this has not yet been fixed. issue # 63 and issue number 85 will clarify you.

 /** * Get serial port names in Windows * * @since 2.3.0 */ private static String[] getWindowsPortNames(Pattern pattern, Comparator<String> comparator) { String[] portNames = serialInterface.getSerialPortNames(); if(portNames == null){ return new String[]{}; } TreeSet<String> ports = new TreeSet<String>(comparator); for(String portName : portNames){ if(pattern.matcher(portName).find()){ ports.add(portName); } } return ports.toArray(new String[ports.size()]); } 

Here I realized that they use some templates for different OS.

 OS_LINUX: Pattern.compile("(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}"); OS_SOLARIS: Pattern.compile("[0-9]*|[az]*"); OS_MAC_OS_X: Pattern.compile("tty.(serial|usbserial|usbmodem).*"); OS_WINDOWS: Pattern.compile(""); 

Suggestions: use only official and latte drivers.

jSSC-2.8.0 version for the version (01.24.2014)

Corrections: Important! Fixed a bug where the port handles a potential leak.

This version contains built-in libraries for Windows (x86, x86-64), Linux (x86, x86-64, ARM soft and hard float), Solaris (x86, x86-64), Mac OS X (x86, x86-64, PPC , PPC64). All native libraries are contained in the jssc.jar file and you do not need to manually manage the native libs.

Resource Link:

+2
source

Take a look at gohai / java-simple-serial-connector , the SerialPortList class has a getPortProperties (String portName) method to get port properties, unfortunately not yet implemented for windows, but it's easy to implement and recompile it again to make it work.

Hope this helps.

+1
source

With the help of zamanillo his contribution, I can now answer it myself. this cannot be done with jssc 2.8.0.

Currently, there are steps to extend the jssc, but I do not know the release schedule and do not bring it fixed. The extension is the SerialPortList->getPortProperties(String portName) Implementations are available in modified jssc 2.8.0 for linux and mac. Windows implementations are harder to find.

Here is the linux and mac code taken from https://github.com/gohai/java-simple-serial-connector/blob/processing/src/cpp/_nix_based/jssc.cpp

 JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_getPortProperties (JNIEnv *env, jclass cls, jstring portName) { const char* portNameChar = (const char*)env->GetStringUTFChars(portName, NULL); jclass stringClass = env->FindClass("Ljava/lang/String;"); jobjectArray ret = env->NewObjectArray(5, stringClass, NULL); #ifdef __APPLE__ // this code is based on QtSerialPort CFMutableDictionaryRef matching = IOServiceMatching(kIOSerialBSDServiceValue); io_iterator_t iter = 0; kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iter); if (kr != kIOReturnSuccess) { env->ReleaseStringUTFChars(portName, portNameChar); return ret; } io_registry_entry_t service; while ((service = IOIteratorNext(iter))) { // compare portName against cu and tty devices bool found = false; CFTypeRef cu = 0; cu = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); if (cu) { char buffer[MAXPATHLEN]; CFStringGetCString(CFStringRef(cu), buffer, sizeof(buffer), kCFStringEncodingUTF8); //fprintf(stdout, "getPortProperties: %s\n", buffer); //fflush(stdout); if (strcmp(portNameChar, buffer) == 0) { found = true; } CFRelease(cu); } CFTypeRef tty = 0; tty = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIODialinDeviceKey), kCFAllocatorDefault, 0); if (tty) { char buffer[MAXPATHLEN]; CFStringGetCString(CFStringRef(tty), buffer, sizeof(buffer), kCFStringEncodingUTF8); //fprintf(stdout, "getPortProperties: %s\n", buffer); //fflush(stdout); if (strcmp(portNameChar, buffer) == 0) { found = true; } CFRelease(tty); } if (!found) { // not port we're looking for //fprintf(stderr, "getPortProperties: %s not found", portNameChar); //fflush(stderr); IOObjectRelease(service); continue; } io_registry_entry_t entry = service; do { int val = 0; char buffer[255]; CFTypeRef idProduct = 0; idProduct = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0); if (idProduct && !env->GetObjectArrayElement(ret, 0)) { CFNumberGetValue(CFNumberRef(idProduct), kCFNumberIntType, &val); sprintf(buffer, "%04x", val); jstring tmp = env->NewStringUTF(buffer); env->SetObjectArrayElement(ret, 0, tmp); env->DeleteLocalRef(tmp); CFRelease(idProduct); } CFTypeRef idVendor = 0; idVendor = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0); if (idVendor && !env->GetObjectArrayElement(ret, 1)) { CFNumberGetValue(CFNumberRef(idVendor), kCFNumberIntType, &val); sprintf(buffer, "%04x", val); jstring tmp = env->NewStringUTF(buffer); env->SetObjectArrayElement(ret, 1, tmp); env->DeleteLocalRef(tmp); CFRelease(idVendor); } CFTypeRef manufacturer = 0; manufacturer = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorString), kCFAllocatorDefault, 0); if (manufacturer && !env->GetObjectArrayElement(ret, 2)) { CFStringGetCString(CFStringRef(manufacturer), buffer, sizeof(buffer), kCFStringEncodingUTF8); jstring tmp = env->NewStringUTF(buffer); env->SetObjectArrayElement(ret, 2, tmp); env->DeleteLocalRef(tmp); CFRelease(manufacturer); } CFTypeRef product = 0; product = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductString), kCFAllocatorDefault, 0); if (product && !env->GetObjectArrayElement(ret, 3)) { CFStringGetCString(CFStringRef(product), buffer, sizeof(buffer), kCFStringEncodingUTF8); jstring tmp = env->NewStringUTF(buffer); env->SetObjectArrayElement(ret, 3, tmp); env->DeleteLocalRef(tmp); CFRelease(product); } CFTypeRef serial = 0; serial = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBSerialNumberString), kCFAllocatorDefault, 0); if (serial && !env->GetObjectArrayElement(ret, 4)) { CFStringGetCString(CFStringRef(serial), buffer, sizeof(buffer), kCFStringEncodingUTF8); jstring tmp = env->NewStringUTF(buffer); env->SetObjectArrayElement(ret, 4, tmp); env->DeleteLocalRef(tmp); CFRelease(serial); } kr = IORegistryEntryGetParentEntry(entry, kIOServicePlane, &entry); } while (kr == kIOReturnSuccess); IOObjectRelease(entry); IOObjectRelease(service); } IOObjectRelease(iter); #endif // __APPLE__ env->ReleaseStringUTFChars(portName, portNameChar); return ret; } 

For windows, I found a solution only for processing where a custom dll is used.

+1
source

Source: https://habr.com/ru/post/1236139/


All Articles