Finding devices by family on a specific port

10 May

Example: Finding devices by family on a specific port:

What if, instead of finding all 1-Wire devices on a specified port, I only wanted to find devices corresponding to a particular device family?  The solution to this is simple. In the previous program, we have the line:
myPortAdapter.targetAllFamilies();

Replace it with
myPortAdapter.targetFamily(familyInteger);

where familyInteger is an integer representing the family code of the devices we are interested in finding. In the previous example run, we used a DS9097U on COM1 that was connected to a little circuit board that had four switches on it. We saw the four switches in the program output, but we also saw a DS2502 EPROM that was embedded in the DS9097U for identification purposes. What if we were only interested in the switches? The ROM ID code of each switch begins with 12h. That is, 12 hex is in the least-significant 8 bits of the ROM ID code, which when printed out, are to the right. Let’s make the following change:

myPortAdapter.targetFamily(0×12);

By preceding our number with 0x, we are telling Java that the number is in hex.
We could also use the line:

myPortAdapter.targetFamily(18);
C:\> java ROM_ID DS9097U COM1
ROM ID:                                                                                 9E00000017844012
Device Name: DS2406
Alternate Name: Dual Addressable Switch, DS2407
Description:
1-Wire Dual Addressable Switch. PIO pin channel A sink capability of typical 50mA at 0.4V with soft turn-on; optional channel B typical 10 mA at 0.4V. 1024 bits of Electrically Programmable Read Only Memory (EPROM) partitioned into four 256 bit pages. 7 bytes of user-program- mable status memory to control the device.

ROM ID:                                                                                 7F00000017841A12
Device Name: DS2406
Alternate Name: Dual Addressable Switch, DS2407
Description:
1-Wire Dual Addressable Switch. PIO pin channel A sink capability of typical 50mA at 0.4V with soft turn-on; optional channel B typical 10 mA at 0.4V. 1024 bits of Electrically Programmable Read Only Memory (EPROM) partitioned into four 256 bit pages. 7 bytes of user-program- mable status memory to control the device.

ROM ID:                                                                                 0200000017461E12
Device Name: DS2406
Alternate Name: Dual Addressable Switch, DS2407
Description:
1-Wire Dual Addressable Switch. PIO pin channel A sink capability of typical 50mA at 0.4V with soft turn-on; optional channel B typical 10 mA at 0.4V. 1024 bits of Electrically Programmable Read Only Memory (EPROM) partitioned into four 256 bit pages. 7 bytes of user-program- mable status memory to control the device.

ROM ID:                                                                 9200000017473B12
Device Name: DS2406
Alternate Name: Dual Addressable Switch, DS2407
Description:
1-Wire Dual Addressable Switch. PIO pin channel A sink capability of typical 50mA at 0.4V with soft turn-on; optional channel B typical 10 mA at 0.4V. 1024 bits of Electrically Programmable Read Only Memory (EPROM) partitioned into four 256 bit pages. 7 bytes of user-program- mable status memory to control the device.

By using the targetFamily() method, we have found only the DS2407 switch devices.


Example: Identifying all software port adapters present

In our previous example, we entered the name of the adapter type and communication port we wanted to use. What if we didn’t want to enter this information?  What if, instead, we wanted to look to see which port adapters were currently supported on the computer?  What if you didn’t happen to know the syntax of the text string used for the adapter name? The following simple example shows how to obtain a list of all the software port adapters installed on a computer. We’ll show the program in its entirety first, and then go through it bit by bit.

Listing 10-2: PortAdapters.java

import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import java.io.*;
import java.util.*;
public   class PortAdapters {
public static void main (String[] args) {
DSPortAdapter singleAdapter = null;
Enumeration myAdapters = null;
Enumeration myPorts = null;
myAdapters = OneWireAccessProvider.enumerateAllAdapters();
while (myAdapters.hasMoreElements()) {
singleAdapter = (DSPortAdapter)myAdapters.nextElement();
System.out.print(“PortAdapter = “ +singleAdapter.getAdapterName());
myPorts = singleAdapter.getPortNames();
System.out.print( “; CommPorts = “ );
while (myPorts.hasMoreElements()) {
System.out.print(myPorts.nextElement() + “ “ );
}
System.out.println();
}
}
}
The first section imports the necessary class libraries.
import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import java.io.*;
import java.util.*;
The next section is the class declaration, the main() method declaration, and two
object declarations. We establish an Enumeration object that contains all of the
DSPortAdapter objects, and a single DSPortadapter object that is used in an
iterative loop to represent a single member of the Enumeration.
public   class PortAdapters {
public static void main (String[] args) {
DSPortAdapter singleAdapter = null;
Enumeration myAdapters = null;
Enumeration myPorts = null;

Having declared the Enumeration myAdapters, we’re now going to set it to the return values of the OneWireAccessProvider.enumerateAllAdapters() method. That method returns an enumeration of DSPortAdapter objects, one for each software port adapter found. Then, we loop through the entire Enumeration, once for each object in the Enumeration. Each time, we set the value of singleAdapter to the next element in the Enumeration of DSPortAdapters. We then use the getAdapterName() method of the DSPortadapter class to return a string value representing the name of the software adapter represented by that object.

myAdapters = OneWireAccessProvider.enumerateAllAdapters();
while (myAdapters.hasMoreElements()) {
singleAdapter = (DSPortAdapter)myAdapters.nextElement();
System.out.print(“PortAdapter = “ +singleAdapter.getAdapterName());
myPorts = singleAdapter.getPortNames();
System.out.print( “; CommPorts = “ );
while (myPorts.hasMoreElements()) {
System.out.print(myPorts.nextElement() + “ “ );
}
System.out.println();
}
}

The system this program is being demonstrated on has the TMEX software loaded, as well as the Java 1-Wire API.

C:\> java PortAdapters
PortAdapter = {DS9097E}; CommPorts = COM0 COM1 COM2 COM3 COM4 COM5
COM6 COM7 COM
8 COM9 COM10 COM11 COM12 COM13 COM14 COM15
PortAdapter = {DS1410E}; CommPorts = LPT0 LPT1 LPT2 LPT3 LPT4 LPT5
LPT6 LPT7 LPT
8 LPT9 LPT10 LPT11 LPT12 LPT13 LPT14 LPT15
PortAdapter = {DS9097U}; CommPorts = COM0 COM1 COM2 COM3 COM4 COM5
COM6 COM7 COM
8 COM9 COM10 COM11 COM12 COM13 COM14 COM15
PortAdapter = DS9097U; CommPorts = COM1 COM2 COM3 COM4
And on Linux
PortAdapter = DS9097U; CommPorts = /dev/ttyS0 /dev/ttyS1 /dev/ttyS2 /
dev/ttyS3

The first three entries are software port adapter names coming from the TMEX software. The last one comes from the Java 1-Wire API.

Random Posts

Comments are closed.