Polling for data
The simplest way to read IPv6 data is by executing the readIPData method of the local Thread device. This method blocks your application until IPv6 data is received or the provided timeout has expired.
Method | Description |
---|---|
readIPData(int) | Specifies the time to wait in milliseconds for IPv6 data reception (method blocks during that time or until IPv6 data is received). If you don't specify a timeout, the method uses the default receive timeout configured in XBeeDevice. |
Read IPv6 data (polling)
import com.digi.xbee.api.ThreadDevice; import com.digi.xbee.api.models.IPMessage; [...] // Instantiate a Thread device object ThreadDevice myDevice = new ThreadDevice("COM1", 9600); myDevice.open(); // Read IPv6 data. IPMessage ipMessage = myDevice.readIPData(); [...]
The method returns the read data inside a IPMessage object and contains the following information:
- IPv6 address of the device that sent the data
- Transmission protocol
- Source and destination ports
- Byte array with the contents of the received data
- You can retrieve the previous information using the corresponding getters of the IPMessage object:
Get the IPMessage information
import java.net.Inet6Address; import com.digi.xbee.api.ThreadDevice; import com.digi.xbee.api.models.IPMessage; import com.digi.xbee.api.models.IPProtocol; [...] // Instantiate a Thread device object. ThreadDevice myDevice = [...] // Read IP data. IPMessage ipMessage = myDevice.readIPData(); Inet6Address destAddr = ipMessage .getIPAddress(); IPProtocol protocol = ipMessage .getProtocol(); int srcPort = ipMessage .getSourcePort(); int destPort = ipMessage .getDestPort(); byte[] data = ipMessage .getData(); [...]
You can also read IPv6 data that comes from a specific IPv6 address. For that purpose, the Thread device objects provide the readIPDataFrom method:
Read network data from a specific remote XBee device (polling)
import java.net.Inet6Address; import com.digi.xbee.api.ThreadDevice; import com.digi.xbee.api.models.IPMessage; [...] Inet6Address ipAddr = (Inet6Address) Inet6Address.getByName("FDB3:0001:0002:0000:0004:0005:0006:0007"); // Instantiate a Thread device object. ThreadDevice myDevice = new ThreadDevice("COM1", 9600); myDevice.open(); // Read IPv6 data. IPMessage ipMessage = myDevice.readIPDataFrom(ipAddr); [...]
This method also returns an IPMessage object containing the same information described before.
In either case, the default timeout to wait for data is two seconds. You can configure this timeout with the getReceiveTimeout and setReceiveTimeout methods of an XBee device class.
Get/set the timeout for synchronous operations
import com.digi.xbee.api.ThreadDevice; [...] public static final int NEW_TIMEOUT_FOR_SYNC_OPERATIONS = 5 * 1000; // 5 seconds ThreadDevice myDevice = [...] // Retrieving the configured timeout for synchronous operations. System.out.println("Current timeout: " + myDevice.getReceiveTimeout() + " milliseconds."); [...] // Configuring the new timeout (in milliseconds) for synchronous operations. myDevice.setReceiveTimeout(NEW_TIMEOUT_FOR_SYNC_OPERATIONS); [...]