DecodingTest.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.serotonin.modbus4j.test;
  2. import com.serotonin.modbus4j.serial.rtu.RtuMessageParser;
  3. import com.serotonin.modbus4j.sero.util.queue.ByteQueue;
  4. public class DecodingTest {
  5. public static void main(String[] args) throws Exception {
  6. // decodeRequest("0A0300770001356B");
  7. // decodeResponse("0A030200001D85");
  8. // decodeRequest("0A0600770001F96B");
  9. // decodeRequest("011000a80002043535353530f4");
  10. // decodeResponse("011000a80002042993");
  11. decodeRequest("010300010001D5CA");
  12. }
  13. public static void decodeRequest(String s) throws Exception {
  14. ByteQueue queue = new ByteQueue(toBytes(s));
  15. new RtuMessageParser(false).parseMessage(queue);
  16. }
  17. public static void decodeResponse(String s) throws Exception {
  18. ByteQueue queue = new ByteQueue(toBytes(s));
  19. new RtuMessageParser(true).parseMessage(queue);
  20. }
  21. public static byte[] toBytes(String s) {
  22. if (s.startsWith("["))
  23. s = s.substring(1);
  24. if (s.endsWith("]"))
  25. s = s.substring(0, s.length() - 1);
  26. String[] parts = s.split(",");
  27. if (parts.length == 1)
  28. parts = s.split("\\|");
  29. if (parts.length == 1)
  30. parts = s.split(" ");
  31. if (parts.length == 1) {
  32. parts = new String[s.length() / 2];
  33. for (int i = 0; i < parts.length; i++)
  34. parts[i] = s.substring(i * 2, i * 2 + 2);
  35. }
  36. byte[] bytes = new byte[parts.length];
  37. for (int i = 0; i < bytes.length; i++)
  38. bytes[i] = (byte) Integer.parseInt(parts[i].trim(), 16);
  39. return bytes;
  40. }
  41. }