0

I have a serial communication protocol and I am receiving messages all the time and the messages arrive in an array almost all at the same time and I would like to separate this array into several others using the characters that determine the beginning [ 102] [102] and stopping at the characters that indicate the end of the message [50] [101], how could you do this?

enter image description here

I tried as follows:

byte[] buffer = Arrays.copyOfRange(dados,0,16);
                    byte[] buffer1 =  Arrays.copyOfRange(dados,16,32);
                    byte[] buffer2 = Arrays.copyOfRange(dados, 32, 48);

however, if I stop receiving the message from any of my equipment, the main array changes, generating an exception

  • What have you tried to do? You have to provide a [mre] so we can help you solve any errors or fix any incorrect behaviour in your code. Stack Overflow is not meant to be used as a software development service where other people write your code for you. – JustAnotherDeveloper Sep 04 '20 at 10:51
  • I'm sorry I will edit the question with the code snippet of how I did it – Paulo Roberto Lima Sep 04 '20 at 10:55

1 Answers1

0
  • Get the beginning and end positions of your frames. You can use this article to get it. byte[] array pattern search

  • implement for this something like this:

List<Integer> startPositions = getPositions(inputBytes, startPattern);
List<Integer> endPositions = getPositions(inputBytes, endPattern);        
  • Then slice inputBytes by iterating over the starts.
  • When you are working on a starts => search the next visible end in your end list.
  • Then you can cut the slices
Maik
  • 310
  • 1
  • 11