0
votes

I am working on UART-RS485. The data is sent in bytes. I have tested with hard-coded code using array. However, I would like to find out how to use array in a dynamic matter as the data packet size may vary.

 Task<UInt32> storeAsyncTask;

 byte[] data = { 0xF5, 0x81, 0x66, 0x0F, 0x00, 0x6B }; 
 dataWriteObject.WriteBytes(data);

 storeAsyncTask = dataWriteObject.StoreAsync().AsTask();

 UInt32 bytesWritten = await storeAsyncTask;

Please help thanks.

Updated: I recently tried using bitconverter but it doesn't work. I am not sure where I did wrong.

        Task<UInt32> storeAsyncTask;

        BitConverter.GetBytes(F5).CopyTo(data, 0);
        BitConverter.GetBytes(TxAddress).CopyTo(data, 1);
        BitConverter.GetBytes(TxCommand).CopyTo(data, 2);
        BitConverter.GetBytes(TxData).CopyTo(data, 3);
        BitConverter.GetBytes(~TxData).CopyTo(data, 4);

        TxChkSum = 0;

        foreach (byte a in data)
        {
            TxChkSum += a;
        }

        BitConverter.GetBytes(TxChkSum).CopyTo(data, 5);

        dataWriteObject.WriteBytes(data);

        storeAsyncTask = dataWriteObject.StoreAsync().AsTask();

        UInt32 bytesWritten = await storeAsyncTask;

Update 23-01-2020

I tried using the list method but by addrange the data is added in range of 2 bytes(16-bits) instead a one byte (8 bits) where the RS485 communication i am interfacing is transmitted in byte by byte. May I know how to work around it?

enter image description here

2nd issue is that at dataWriteObject.WriteBytes(data.ToArray());it throw an exception as follow;

"Object reference not set to an instance of an object."

enter image description here

I am not sure what does it mean and how to overcome it.

eg.

byte F5 = 0xF5;
byte TxCommand= 0x66
byte TxData = 0x0F

        try
        {
            List<byte> data = new List<byte>();

            data.AddRange(BitConverter.GetBytes(F5));
            data.AddRange(BitConverter.GetBytes(TxAdr));
            data.AddRange(BitConverter.GetBytes(TxCommand));
            data.AddRange(BitConverter.GetBytes(TxData));
            data.AddRange(BitConverter.GetBytes(~TxData));

            TxChkSum = 0;

            foreach (byte a in data)
            {
                TxChkSum += a;
            }
            data.AddRange(BitConverter.GetBytes(TxChkSum));

            dataWriteObject.WriteBytes(data.ToArray());

            Task<UInt32> storeAsyncTask;
            storeAsyncTask = dataWriteObject.StoreAsync().AsTask();

            UInt32 bytesWritten = await storeAsyncTask;
        }
        catch (Exception ex)
        {
            MainStatusDisplay.Text = ex.Message;
        }
1

1 Answers

1
votes

BitConverter.GetBytes is used to convert base data types to an array of bytes, and an array of bytes to base data types. Please see following code.

            byte[] data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00  };

            double aDouble = 0.11;
            BitConverter.GetBytes(aDouble).CopyTo(data, 0);

After called CopyTo method, the data array will be filled with this result.

0x29 0x5c 0x8f 0xc2 0xf5 0x28 0xbc 0x3f 0x00 0x00

So that, if the size of converted buffer is bigger than the target data array which you want to copy to, there will be an overflow exception. You may try with following code.

            List<byte> data = new List<byte>();

            data.AddRange(BitConverter.GetBytes(F5));
            data.AddRange(BitConverter.GetBytes(TxAddress));
            data.AddRange(BitConverter.GetBytes(TxCommand));
            data.AddRange(BitConverter.GetBytes(TxData));
            data.AddRange(BitConverter.GetBytes(~TxData));

            TxChkSum = 0;

            foreach (byte a in data)
            {
                TxChkSum += a;
            }

            data.AddRange(BitConverter.GetBytes(TxChkSum));

            dataWriteObject.WriteBytes(data.ToArray());

UPDATE: In fact, in your scenario, it is not necessary to convert the byte data by BitConverter. You may try to use following code.

            List<byte> data = new List<byte>();

            data.Add(F5);
            data.Add(TxAddress);
            data.Add(TxCommand);
            data.Add(TxData);
            data.Add(~TxData);

            TxChkSum = 0;

            foreach (byte a in data)
            {
                TxChkSum += a;
            }