2. Setting up the Network. Campbell Scientific Java, Java PakBus Software Development Kit

Add to My manuals
34 Pages

advertisement

2. Setting up the Network. Campbell Scientific Java, Java PakBus Software Development Kit | Manualzz

Java PakBus® Software Development Kit clock set/check, data collection, or sending a file. Each has a corresponding client interface which will receive status notifications as the transaction progresses and a completion notification when the transaction is complete. An application initiates transactions by creating specific transaction objects and “adding” them to the appropriate datalogger object.

All of the other classes in the API are designed as “helper” classes for the core classes mentioned above.

2. Setting up the Network

Setting up the API consists of creating an instance of the Network class. This class has one constructor which requires a PakBus® address, an input stream, and an output stream. The PakBus® address must uniquely identify your application in the PakBus® network in which it will participate. The input and output streams can be derived from an instance of java.net.Socket or of javax.comm.CommPort. They could also be derived from some other communications API supported on your platform. The network object will listen for incoming messages on the input stream object and will write outgoing messages to the output stream object. Once the network object has been created, the application must drive it by calling its check_state() method periodically. All notifications and activity in the network will result from this call. The following code fragment shows an example of constructing and driving a network object: import com.campbellsci.pakbus.*; import java.net.*; class Example2

{

private Network network;

private Socket socket;

private boolean complete;

public void run() throws Exception

{

socket = new Socket("192.168.4.225",6785);

network = new Network(

(short)4079,

socket.getInputStream(),

socket.getOutputStream());

complete = false;

while(!complete)

{

network.check_state();

Thread.sleep(100);

}

}

}

The application shown above doesn’t do much of interest to anyone. It will accept, but not act on most incoming messages. But, at this point, it is pretty much useless to us. This can be changed somewhat by adding station objects to the network. Consider the above example expanded:

2

Java PakBus® Software Development Kit import com.campbellsci.pakbus.*; import java.net.*; class Example2

{

private Network network;

private Socket socket;

private boolean complete;

private Datalogger my_cr1000;

private Datalogger my_other_cr1000;

public void run() throws Exception

{

// create the connection and its network

socket = new Socket("192.168.4.225",6785);

network = new Network(

(short)4079,

socket.getInputStream(),

socket.getOutputStream());

// create the station

my_cr1000 = new Datalogger((short)1085);

my_other_cr1000 = new Datalogger((short)1084,(short)1085);

network.add_station(my_cr1000);

network.add_station(my_other_cr1000);

my_other_cr1000.set_round_trip_time(10000);

// now drive the network

complete = false;

while(!complete)

{

network.check_state();

Thread.sleep(100);

}

}

}

Note here that we have used both constructor forms for class Datalogger. The first, used to create the my_cr1000 object, assumes the datalogger is a neighbour to our application meaning that it can be reached directly. The second form, used to create the my_other_cr1000 object, requires the specification of both the PakBus® address for the station as well as for the

PakBus® address of the neighbour that will be used to reach the station.

We also specified the round trip time for the second CR1000. If this value is not specified, it will default to 5000 milliseconds which is generally a pretty reasonable guess. The round trip time can depend upon the speed and latency of the link so it is up to the application to provide a good value. If too small a value is specified, the API will tend to be premature about sending retry messages. If too large a value is specified, the application will be slow to recognize paths that are not responding.

Before a Datalogger object can be “used”, it must first be added to the network’s list of stations by calling network.add_station().

You don’t need to add a station for every PakBus® node in the real network.

You only need to add stations for dataloggers with which your application will communicate.

3

advertisement

Related manuals