Jonathan Nath
Jonathan Nath

Reputation: 71

rearranging and or rejecting an array

I have an array that is transmitted to me, when I receive good data it looks like this:

byte[] buffer = { 0xaa, 0x02, 0x47, 0xf0, 0x79, 0x00, 0x45, 0x2c, 0x12, 0x20, 0xc6, 0x00, 0xa1, 0x2d, 0xb2, 0x55 }; 

However I wanted to rearrange the array just in case it doesn't come in correctly. for instance:

byte[] buffer = { 0x02, 0x47, 0xf0, 0x79, 0x00, 0x45, 0x2c, 0x12, 0x20, 0xc6, 0x00, 0xa1, 0x2d, 0xb2, 0x55, 0xaa };

The message needs to start with 0xaa and end with 0x55. in this scenario it is 0xaa is at the end.

Bonus: how would I reject a message if say a special or junk character was accidentally transmitted?

I have tried using

array.copy(buffer, index, new_buffer, buffer.length-index);

as well but it didnt work. This is what I have so far:

         byte[] adjusted_buffer = new byte[16];
        //byte[] buffer = { 0xaa, 0x02, 0x47, 0xf0, 0x79, 0x00, 0x45, 0x2c, 0x12, 0x20, 0xc6, 0x00, 0xa1, 0x2d, 0xb2, 0x55 };
        byte[] buffer = { 0x02, 0x47, 0xf0, 0x79, 0x00, 0x45, 0x2c, 0x12, 0x20, 0xc6, 0x00, 0xa1, 0x2d, 0xb2, 0x55, 0xaa };

        if (buffer[0].Equals(0xaa) && buffer[15].Equals(0x55) && buffer.Length.Equals(16))
        {
            Console.WriteLine("arranged");
        }
        else
        {
            int index = Array.IndexOf<byte>(buffer, 0xaa);
            if (buffer[index - 1].Equals(0x55) && buffer.Length.Equals(16))
            {
                for (int i = 0; i < buffer.Length; i++)
                {
                    adjusted_buffer[i] = buffer[index + i];
                }

                Console.WriteLine(adjusted_buffer);
            }
            else
            {
                index = (byte) Array.IndexOf(buffer, 0xaa, index);
                if (buffer[index - 1].Equals(0x55) && buffer.Length.Equals(16))
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        adjusted_buffer[i] = buffer[index + i];
                    }
                    Console.WriteLine("rearranged again");

Upvotes: 0

Views: 166

Answers (1)

gfish3000
gfish3000

Reputation: 1567

Let's start off with declaring some constants...

const byte START = 0x02;
const byte END = 0x55;
const int SIZE = 16;

Then we can deal with the incoming messages...

var buffer = /* set incoming buffer value */;

if (buffer[0] != START)
{
    var fixedBuffer = new List<byte> { START };

    fixedBuffer.AddRange(removeFirstInstanceFromBuffer(buffer, START));

    buffer = fixedBuffer.ToArray();
}

if (buffer[buffer.Length - 1] != END)
{
    var fixedBuffer = new List<byte>();

    fixedBuffer.AddRange(removeFirstInstanceFromBuffer(buffer, END));
    fixedBuffer.Add(END);

    buffer = fixedBuffer.ToArray();
}

// wherever you want to have helper methods

private byte[] removeFirstInstanceFromBuffer(byte[] bytes, byte b)
{
    var idx = bytes.ToList().IndexOf(bytes.First(x => x == b));
    var truncated = new List<byte>();

    for (var i = 0; i < bytes.Length; i++) if (i != idx) truncated.Add(bytes[i]);

    return truncated.ToArray();
}

Then if you want to throw out junk characters and such, simply declare an array of the junk characters you want cleared and use the utility method below as follows...

var bytesToRemove = new List<byte>();

foreach (var b in buffer)
{
    if (JUNK_ARRAY.Contains(b)) bytesToRemove.Add(b);
}

foreach (var b in bytesToRemove)
{
    buffer = removeFirstInstanceFromBuffer(buffer, b);
}

NOTE: If your byte stream isn't coming through correctly, you might have bigger problems and just rearranging it might be a bad idea. Data like this can come from a motor or a sensor so if it's malformed, you'd just be ignoring the issue. The code given above will make sure your message is correct byte by byte and throw an error if there's a junk byte somewhere in the stream. And if you're losing packets, you also have a network issue to look into, not solely a programming one.

EDIT: Misunderstood request, fixed code to solve the requested problem as explained by @tweellt.

EDIT 2: Fixed helper method to find the index properly.

Upvotes: 2

Related Questions