Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApiSenderExample {
- private final static Logger log = Logger.getLogger(ApiSenderExample.class);
- private ApiSenderExample() throws Exception {
- XBee xbee = new XBee();
- final int sleep = 500;
- int count = 0;
- int errors = 0;
- int ackErrors = 0;
- int ccaErrors = 0;
- int purgeErrors = 0;
- long now;
- try {
- // my coordinator
- // xbee.open("/dev/tty.usbserial-A4004Rim", 9600);
- xbee.open("/dev/tty.usbserial-A600eIB5", 115200);
- while (true) {
- // log.debug("Sending count " + count);
- // XBeeResponse response = xbee.sendTxRequest16(destination, 0x0a, payload);
- int[] payload = new int[] {255,127,0,1};
- // to verify correct byte escaping, we'll send a start byte
- //int[] payload = new int[] { XBeePacket.SpecialByte.START_BYTE.getValue() };
- // specify the remote XBee 16-bit MY address
- XBeeAddress64 destination = new XBeeAddress64(0x00, 0x13, 0xa2, 0x00, 0x40, 0x6e, 0x7d, 0x49);
- TxRequest64 tx = new TxRequest64(destination, payload);
- // or send a TX64 (same thing except we are addressing by SH+SL address)
- // XBeeAddress64 destination = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x08, 0xb4, 0x8f);
- // TxRequest64 tx2 = new TxRequest64(destination64, payload);
- now = System.currentTimeMillis();
- xbee.sendAsynchronous(tx);
- XBeeResponse response = null;
- while (true) {
- // blocks until we get response
- try {
- response = xbee.getResponse(1000);
- if (response.getApiId() != ApiId.TX_STATUS_RESPONSE) {
- log.debug("expected tx status but received " + response);
- } else {
- // log.debug("got tx status");
- if (((TxStatusResponse) response).getFrameId() != tx.getFrameId()) {
- throw new RuntimeException("frame id does not match");
- }
- if (((TxStatusResponse) response).getStatus() != TxStatusResponse.Status.SUCCESS) {
- errors++;
- if (((TxStatusResponse) response).isAckError()) {
- ackErrors++;
- } else if (((TxStatusResponse) response).isCcaError()) {
- ccaErrors++;
- } else if (((TxStatusResponse) response).isPurged()) {
- purgeErrors++;
- }
- log.debug("Tx status failure with status: " + ((TxStatusResponse) response).getStatus());
- } else {
- // success
- log.debug("Success. count is " + count + ", errors is " + errors + ", in " + (System.currentTimeMillis() - now) + ", ack errors "
- + ackErrors + ", ccaErrors " + ccaErrors + ", purge errors " + purgeErrors);
- for(int x : response.getProcessedPacketBytes()) {
- System.out.println((byte)x);
- }
- }
- count++;
- break;
- }
- } catch(XBeeTimeoutException e) {
- System.out.println("\n\nTIMEOUT\n\n");
- }
- }
- Thread.sleep(sleep);
- }
- } finally {
- xbee.close();
- }
- }
- public static void main(String[] args) throws Exception {
- // init log4j
- PropertyConfigurator.configure("log4j.properties");
- new ApiSenderExample();
- }
- }
Add Comment
Please, Sign In to add comment