A device connected via the USART-USB adapter sends data to the computer, the program on the computer receives the data packet (using the JSSC library) as an array of bytes. The array bytes are predefined parameters, such as Device Status, Serial Number, etc. accordingly, we need some kind of constant structure that implements the correspondence table - which byte is which parameter it is. Plus, we need some kind of constant structure to interpret the value of a parameter to a form suitable for output in the GUI, such as "FF" -> "IDLE", "FE" -> "SLEEP", etc. Moreover, there are dozens of such states and dozens of parameters. Now I started doing this:

public class Packet { public static String Partnum; public static String Version; public static String State; public static String LQI; public static String RSSI; public static String Freqest; public static String RXfifo; public static String VCO_VC_DAC; public static String WOR; public static void packetHandler(String[] arg) { Partnum = "0x"+arg[0]; Version = "0x"+arg[1]; // State if (arg[2].equals("0D")) { State = "RX"; } else if (arg[2].equals("01")) { State = "IDLE"; } else if (arg[2].equals("11")) { State = "RXFIFO_OVERFLOW"; } else { State = arg[2]; } LQI = arg[3]; Freqest = arg[4]; RSSI = arg[5]; RXfifo = arg[6]; VCO_VC_DAC = arg[7]; WOR = arg[8]+" "+arg[9]; } } 

It works, but I feel that this is not right and is not suitable for processing large packets. And the time for processing, even with a quantity of several parameters, is simply enormous. How to correctly parse and interpret the contents of a package with Java tools? Maybe there are some examples? what I found is interesting https://github.com/farrellf/TelemetryViewer/tree/master/Telemetry%20Viewer , but the package contains simply values ​​for the output and as such does not require processing or interpretation.

  • Show an example of an array of bytes that comes to you, preferably immediately in the form hex - Barmaley
  • For example: 13 0d 89 0a 1c db ae 32 20 9a 50 ee 40 78 36 fd 12 49 32 f6 9e 7d 49 dc ad 4f 14 f2. in fact, the contents of registers, buffers, etc. are sent from the device. I need to accept and process it somehow - Mikhail Krechko

1 answer 1

I would write something like this:

 public class Packet { private int partNum; private int version; private String state; private static boolean isRx(byte b) { return (b==0x0d)?true:false; } private static boolean isIdle(byte b) { return (b==0x01)?true:false; } private static boolean isOverflow(byte b) { return (b==0x11)?true:false; } public void parsePacketHandler(byte[] args) { this.partNum=args[0]; this.version=args[1]; if(isIdle(args[2)) this.state="idle"; else if(isRx(args[2])) this.state="rx"; else if(isOverflow(args[2])) this.state="overflow" else this.state=""+args[2]; } } 
  • Thank. It just bothers me that with such an approach, all the rules, if there are few parameters and no options for values, and when it comes to dozens of parameters and dozens of values, it turns into some long listings of unreadable code. Well, the execution time confuses, whether it is possible to parallelize processing into several streams with such an ideology. I think the collection can be applied, but they are sharpened by dynamic content change, but I basically need something like a table of constants. - Mikhail Krechko
  • Under each parameter, your class should do something .. or a subclass - Mikhail Krechko
  • In your code, you did the data parsing through a String , which is illogical. I did parsing bytes, which is much more efficient. Unfortunately, when you parse an array of bits / bytes of a long obscure listing, it still cannot be avoided - Barmaley
  • As it turned out, byte is absolutely inconvenient to use in Java, since it only holds numbers from -128 to 127, i.e. for example, 0xFE just doesn't fit. Some kind of unconventional byte. I have to use int. - Mikhail Krechko
  • @MikhailKrechko did you at least understand what you wrote? 0xFE=-2 - in Java notation and what does it bother? - Barmaley