Wire devices attached to TINI

10 May

Example: Another way of determining the ROM ID for all 1-Wire devices attached to TINI: The program below performs the same function on TINI as our previous example, but uses TINI-specific objects. We start with the program intact, then dissect it, and show a sample run of the program.

Listing 10-9: TINIROM_ID2.java
import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import  com.dalsemi.onewire.container.*;
import java.io.*;
import java.util.*;
public   class TINIROM_ID2 {
public static void main (String[] args) {
DSPortAdapter myTINIExternal = null;
DSPortAdapter myTINIInternal = null;
OneWireContainer singleContainer = null;
Enumeration myContainers = null;
try {

myTINIExternal =
OneWireAccessProvider.getAdapter(“TINIExternalAdapter”,
“serial1”);
myTINIExternal.targetAllFamilies();
myContainers = myTINIExternal.getAllDeviceContainers();
System.out.println(“Adapter: “ +
myTINIExternal.getAdapterName());
System.out.println(“Port: “ + myTINIExternal.getPortName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                                    “ +
singleContainer.getAddressAsString());
}
System.out.println();
myTINIInternal =
OneWireAccessProvider.getAdapter(“TINIInternalAdapter”,
“default”);
myTINIInternal.targetAllFamilies();
myContainers = myTINIInternal.getAllDeviceContainers();
System.out.println(“Adapter: “ +
myTINIInternal.getAdapterName());
System.out.println(“Port: “ + myTINIInternal.getPortName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                                    “ +
singleContainer.getAddressAsString());
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
System.out.println();
}
}

We start with the normal import statements, class declaration, and main() method declaration. We also declare some objects: DSPortAdapter objects representing our TINI external and internal 1-Wire buses, a single OneWireContainer object and an Enumeration of containers. We’re going to create each one of the port adapters, then create an Enumeration of device containers representing the devices found on each one.

import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import  com.dalsemi.onewire.container.*;
import java.io.*;


import java.util.*;
public   class TINIROM_ID2 {
public static void main (String[] args) {
DSPortAdapter myTINIExternal = null;
DSPortAdapter myTINIInternal = null;
OneWireContainer singleContainer = null;
Enumeration myContainers = null;
Here we are creating our DSPortAdapter object representing the TINI external              1-
Wire bus. In contrast to the previous program, we are now specifying the port adapter
and port names. We’re then telling the 1-Wire bus to include all device families in any
search ROM process. As before, we then create an Enumeration of all device
containers found and print out all the ROM ID for the corresponding devices.
do search the bus for all device families
try {
myTINIExternal =
OneWireAccessProvider.getAdapter(“TINIExternalAdapter”,
“serial1”);
myTINIExternal.targetAllFamilies();
myContainers = myTINIExternal.getAllDeviceContainers();
System.out.println(“Adapter: “ + myTINIExternal.getAdapterName());
System.out.println(“Port: “ + myTINIExternal.getPortName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                                                               “ +
singleContainer.getAddressAsString());
}
System.out.println();
Now, we repeat the process for the TINI internal 1-Wire bus.
myTINIInternal =
OneWireAccessProvider.getAdapter(“TINIInternalAdapter”,
“default”);
myTINIInternal.targetAllFamilies();
myContainers = myTINIInternal.getAllDeviceContainers();
System.out.println(“Adapter: “ +
myTINIInternal.getAdapterName());
System.out.println(“Port: “ + myTINIInternal.getPortName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                                                               “ +
singleContainer.getAddressAsString());
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
System.out.println();
}
}

When executed on a TINI, the program produces the following output:

C:\> javac -bootclasspath                                             %TINI_HOME%\bin\tiniclasses.jar
-d bin src\TINIROM_ID2.java
C:\> java                                                             -classpath %TINI_HOME%\bin\tini.jar;. BuildDependency
-p                                                                    %TINI_HOME%\bin\owapi_dependencies_TINI.jar
-f bin
-x                                                                    %TINI_HOME%\bin\owapi_dep.txt
-o bin\TINIROM_ID.tini
-d %TINI_HOME%\bin\tini.db

FTP the ROM_ID.tini to your TINI, and then run the program:

TINI /> java ROM_ID2.tini
Adapter: TINIExternalAdapter
Port: serial1
ROM ID:   EF0000004B224210
ROM ID:                                                               9A0000000C152305
Adapter: TINIInternalAdapter
Port: default
ROM ID:   D45E70005C8FAA89

This is the same result as before, just a different way of getting to it. It’s important to note that this program will only work on TINI because it uses TINI specific objects. Additionally, since the TINIExternalAdapter and TINIInternalAdapter only have one port each, we don’t need to go through the process of selecting the port as we did before.

Our last example of this takes it a step further.

Example: Yet another way of determining the ROM ID for all
1-Wire devices attached to TINI

Below is one more example of how to do the same thing.

Listing 10-10: TINIROM_ID3.java

import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import  com.dalsemi.onewire.container.*;
import java.io.*;
import java.util.*;
public   class TINIROM_ID3 {
public static void main (String[] args) {
DSPortAdapter myTINIExternal = null;
DSPortAdapter myTINIInternal = null;
OneWireContainer singleContainer = null;
Enumeration myContainers = null;
try {
myTINIExternal = new TINIExternalAdapter();
myTINIExternal.targetAllFamilies();
myContainers = myTINIExternal.getAllDeviceContainers();
System.out.println(“Adapter: “ +
myTINIExternal.getAdapterName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                               “ +
singleContainer.getAddressAsString());
}
System.out.println();
myTINIInternal = new TINIInternalAdapter();
myTINIInternal.targetAllFamilies();
myContainers = myTINIInternal.getAllDeviceContainers();
System.out.println(“Adapter: “ +
myTINIInternal.getAdapterName());
while (myContainers.hasMoreElements()) {
singleContainer =
(OneWireContainer)myContainers.nextElement();
System.out.println(“ROM ID:                               “ +
singleContainer.getAddressAsString());
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
System.out.println();
}
}

The only thing that differs in this program is the manner in which we create the DSPortAdapter objects. This time, instead of using OneWireAccessProvider methods to create our object for us, we are going to use the following:

myTINIExternal = new TINIExternalAdapter();
myTINIInternal = new TINIInternalAdapter();

These constructors directly create TINIExternalAdapter and TINIInternalAdapter objects that are subclasses of the DSPortAdapter class. The only other difference is with respect to the ports. In the previous example, we specified a port for each adapter, which had the effect of selecting a port. That allowed us to use the getPortName() method. In this example, we haven’t specified or “selected” a port, and therefore aren’t able to retrieve port names in the same fashion. While this discussion has focused on some differences between how the 1-Wire bus is accessed on TINI vs. the PC, most programs that run on the PC can be run on TINI without changes. The key is compilation. Our next examples illustrate this.

Example: Controlling a DS2405 addressable switch from TINI
Previously, we made a self-contained Java class called switch that we accessed from the PC. This same class can be compiled and loaded onto TINI in the form of a .tini file and executed under Slush. We don’t have to modify the source code, but we do have to take a few things into consideration during compilation. These things were discussed in detail in Chapter 7 but we will review them here. The following is a listing of the commands used to compile the program for TINI. Carriage returns have been inserted into some of the command lines for readability. These commands are stored in a .bat file and executed as a script.

C:\> javac-d bin src\Switch.java
C:\> java -classpath %TINI_HOME%\bin\tini.jar;. BuildDependency
-p                                                                                        %TINI_HOME%\bin\owapi_dependencies_TINI.jar
-f bin
-x                                                                                        %TINI_HOME%\bin\owapi_dep.txt
-o bin\Switch.tini
-d %TINI_HOME%\bin\tini.db
-add OneWireContainer05

Let’s take the same small circuit board, featuring an LED and a 2504 addressable switch, used in our previous switch example, and attach it to the 1-Wire bus on TINI. When this program is loaded onto a TINI module via FTP and executed via a Telnet session on TINI under the Slush operating system, the following results:

TINI /> java Switch.tini on
Device ROM ID: 9A0000000C152305
Device Latch State: true
Device Level: false
TINI /> java Switch.tini off
Device ROM ID: 9A0000000C152305
Device Latch State: false
Device Level: true

The program functions exactly as before.

Example: Using a temperature iButton with TINI
Previously we saw how a temperature iButton could be accessed on a PC. That same program can be used on a TINI module. Our iButton is placed on a small circuit board using an iButton clip and the board is connected to a TINI socket with cat 5 cable and RJ11 connectors. When we previously used the Thermometer.java example, we were illustrating the use of the parallel port and the COM ports on the PC. As we previously mentioned, we can pass two parameters to the program to indicate which PortAdapter and which CommPort to use. If no parameters are given, then the program uses the default PortAdapter and the default CommPort. As we saw from running the DefaultAdapter program, the default TINI 1-Wire portAdapter is TINIExternalAdapter and the default communication port is serial1. We can compile Thermometer.java, run the class file through BuildDependency and then run Thermometer.tini on our TINI stick with results similar to those from the previous run. Don’t forget that this program needs to be converted to a TINI program, a .tini file, using the methodology outlined above. The commands used are:

C:\> javac -d bin src\Thermometer.java
C:\> java -classpath %TINI_HOME%\bin\tini.jar;. BuildDependency
-p                                                                                        %TINI_HOME%\bin\owapi_dependencies_TINI_001.jar
-f bin
-x                                                                                        %TINI_HOME%\bin\owapi_dep.txt
-o bin\Thermometer.tini
-d %TINI_HOME%\bin\tini.db
-add OneWireContainer10

TINI /> java Thermometer.tini
The devices ROM ID: EF0000004B224210
The measured temperature: 23.5 Deg C, or 74.3 Deg F
TINI />

Summary
This chapter has given you a good introduction to the 1-Wire bus and writing Java programs to talk to 1-Wire devices. We will be using what we have learned in this chapter in the remaining sections of this book, so if there are parts that were not clear, go back and reread them. If necessary, consult the following references and the data sheets for the specific 1-Wire devices you need to communicate with.

References
1. Tech Brief1: 1-Wire Net Design Guide,

http://www.maxim-ic.com/1st_pages/tb1.htm

2. Understanding and Using Cyclic Redundancy Checks with Dallas
Semiconductor iButton Products,

http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf

3. Reading and Writing iButtons via Serial Interfaces,

http://pdfserv.maxim-ic.com/arpdf/AppNotes/app74.pdf

4. Dallas Semiconductor / Maxim Semiconductor 1-wire devices,

http://dbserv.maxim-ic.com/1-Wire.cfm

Random Posts

Comments are closed.