-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathMainApp.java
More file actions
110 lines (93 loc) · 3.92 KB
/
MainApp.java
File metadata and controls
110 lines (93 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) 2014-2015 Digi International Inc.,
* All rights not expressly granted are reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
* =======================================================================
*/
package com.digi.xbee.api.setgetparameters;
import com.digi.xbee.api.XBeeDevice;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.utils.ByteUtils;
import com.digi.xbee.api.utils.StringUtils;
/**
* XBee Java Library Set and Get parameters sample application.
*
* <p>This example sets and gets the value of 4 parameters with different
* value types. Then it reads them from the device verifying the read values
* are the same as the values that were set.</p>
*
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
* included in the root directory.</p>
*/
public class MainApp {
/* Constants */
// TODO Replace with the serial port where your module is connected to.
private static final String PORT = "COM1";
// TODO Replace with the baud rate of your module.
private static final int BAUD_RATE = 9600;
private static final String PARAM_NODE_ID = "NI";
private static final String PARAM_PAN_ID = "ID";
private static final String PARAM_DEST_ADDRESS_H = "DH";
private static final String PARAM_DEST_ADDRESS_L = "DL";
private static final String PARAM_VALUE_NODE_ID = "Yoda";
private static final byte[] PARAM_VALUE_PAN_ID = new byte[]{0x12, 0x34};
private static final int PARAM_VALUE_DEST_ADDRESS_H = 0x00;
private static final int PARAM_VALUE_DEST_ADDRESS_L = 0xFFFF;
/**
* Application main method.
*
* @param args Command line arguments.
*/
public static void main(String[] args) {
System.out.println(" +---------------------------------------------+");
System.out.println(" | XBee Java Library Set/Get parameters Sample |");
System.out.println(" +---------------------------------------------+\n");
XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
try {
myDevice.open();
// Set parameters.
myDevice.setParameter(PARAM_NODE_ID, StringUtils.stringToByteArray(PARAM_VALUE_NODE_ID));
myDevice.setParameter(PARAM_PAN_ID, PARAM_VALUE_PAN_ID);
myDevice.setParameter(PARAM_DEST_ADDRESS_H, ByteUtils.intToByteArray(PARAM_VALUE_DEST_ADDRESS_H));
myDevice.setParameter(PARAM_DEST_ADDRESS_L, ByteUtils.intToByteArray(PARAM_VALUE_DEST_ADDRESS_L));
// Get parameters
byte[] paramValueNI = myDevice.getParameter(PARAM_NODE_ID);
byte[] paramValueID = myDevice.getParameter(PARAM_PAN_ID);
byte[] paramValueDH = myDevice.getParameter(PARAM_DEST_ADDRESS_H);
byte[] paramValueDL = myDevice.getParameter(PARAM_DEST_ADDRESS_L);
// Compare the read parameter values with the values that were set.
if (!StringUtils.byteArrayToString(paramValueNI).equals(PARAM_VALUE_NODE_ID)) {
System.out.println("NI parameter was not set correctly.");
myDevice.close();
System.exit(1);
}
if (ByteUtils.byteArrayToLong(paramValueID) != ByteUtils.byteArrayToLong(PARAM_VALUE_PAN_ID)) {
System.out.println("ID parameter was not set correctly.");
myDevice.close();
System.exit(1);
}
if (ByteUtils.byteArrayToInt(paramValueDH) != PARAM_VALUE_DEST_ADDRESS_H) {
System.out.println("DH parameter was not set correctly.");
myDevice.close();
System.exit(1);
}
if (ByteUtils.byteArrayToInt(paramValueDL) != PARAM_VALUE_DEST_ADDRESS_L) {
System.out.println("DL parameter was not set correctly.");
myDevice.close();
System.exit(1);
}
System.out.println("All parameters were set correctly!");
myDevice.close();
System.exit(0);
} catch (XBeeException e) {
e.printStackTrace();
myDevice.close();
System.exit(1);
}
}
}