Guest User

Untitled

a guest
Jan 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. public class ApiSenderExample {
  2.  
  3. private final static Logger log = Logger.getLogger(ApiSenderExample.class);
  4.  
  5. private ApiSenderExample() throws Exception {
  6.  
  7. XBee xbee = new XBee();
  8.  
  9. final int sleep = 500;
  10.  
  11. int count = 0;
  12. int errors = 0;
  13. int ackErrors = 0;
  14. int ccaErrors = 0;
  15. int purgeErrors = 0;
  16.  
  17. long now;
  18.  
  19. try {
  20. // my coordinator
  21. // xbee.open("/dev/tty.usbserial-A4004Rim", 9600);
  22. xbee.open("/dev/tty.usbserial-A600eIB5", 115200);
  23.  
  24. while (true) {
  25.  
  26. // log.debug("Sending count " + count);
  27. // XBeeResponse response = xbee.sendTxRequest16(destination, 0x0a, payload);
  28.  
  29. int[] payload = new int[] {255,127,0,1};
  30. // to verify correct byte escaping, we'll send a start byte
  31. //int[] payload = new int[] { XBeePacket.SpecialByte.START_BYTE.getValue() };
  32.  
  33. // specify the remote XBee 16-bit MY address
  34. XBeeAddress64 destination = new XBeeAddress64(0x00, 0x13, 0xa2, 0x00, 0x40, 0x6e, 0x7d, 0x49);
  35. TxRequest64 tx = new TxRequest64(destination, payload);
  36. // or send a TX64 (same thing except we are addressing by SH+SL address)
  37. // XBeeAddress64 destination = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x08, 0xb4, 0x8f);
  38. // TxRequest64 tx2 = new TxRequest64(destination64, payload);
  39.  
  40. now = System.currentTimeMillis();
  41. xbee.sendAsynchronous(tx);
  42.  
  43. XBeeResponse response = null;
  44.  
  45. while (true) {
  46. // blocks until we get response
  47. try {
  48. response = xbee.getResponse(1000);
  49.  
  50. if (response.getApiId() != ApiId.TX_STATUS_RESPONSE) {
  51. log.debug("expected tx status but received " + response);
  52. } else {
  53. // log.debug("got tx status");
  54.  
  55. if (((TxStatusResponse) response).getFrameId() != tx.getFrameId()) {
  56. throw new RuntimeException("frame id does not match");
  57. }
  58.  
  59. if (((TxStatusResponse) response).getStatus() != TxStatusResponse.Status.SUCCESS) {
  60. errors++;
  61.  
  62. if (((TxStatusResponse) response).isAckError()) {
  63. ackErrors++;
  64. } else if (((TxStatusResponse) response).isCcaError()) {
  65. ccaErrors++;
  66. } else if (((TxStatusResponse) response).isPurged()) {
  67. purgeErrors++;
  68. }
  69.  
  70. log.debug("Tx status failure with status: " + ((TxStatusResponse) response).getStatus());
  71. } else {
  72. // success
  73. log.debug("Success. count is " + count + ", errors is " + errors + ", in " + (System.currentTimeMillis() - now) + ", ack errors "
  74. + ackErrors + ", ccaErrors " + ccaErrors + ", purge errors " + purgeErrors);
  75.  
  76. for(int x : response.getProcessedPacketBytes()) {
  77. System.out.println((byte)x);
  78. }
  79. }
  80.  
  81. count++;
  82.  
  83. break;
  84. }
  85. } catch(XBeeTimeoutException e) {
  86. System.out.println("\n\nTIMEOUT\n\n");
  87. }
  88. }
  89.  
  90. Thread.sleep(sleep);
  91. }
  92. } finally {
  93. xbee.close();
  94. }
  95. }
  96.  
  97. public static void main(String[] args) throws Exception {
  98. // init log4j
  99. PropertyConfigurator.configure("log4j.properties");
  100. new ApiSenderExample();
  101. }
  102. }
Add Comment
Please, Sign In to add comment