Campbell Scientific Java PakBus Software Development Kit User Manual

Page 11

Advertising
background image

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 station

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

Advertising