UWP C# UART 数据缓冲区使用数组还是列表? [英] UWP C# UART data buffer to use Array or List?

查看:20
本文介绍了UWP C# UART 数据缓冲区使用数组还是列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 UART-RS485.数据以字节为单位发送.我已经使用 array 对硬编码代码进行了测试.但是,我想知道如何在动态中使用数组,因为数据包大小可能会有所不同.

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;

请帮忙谢谢.

更新:我最近尝试使用 bitconverter 但它不起作用.我不确定我哪里做错了.

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;

更新 23-01-2020

Update 23-01-2020

我尝试使用 list 方法,但通过 addrange 数据添加到 2 个字节(16 位)的范围内,而不是一个字节(8 位),其中我正在接口的 RS485 通信是逐字节传输的.我可以知道如何解决它吗?

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?

第二个问题是在dataWriteObject.WriteBytes(data.ToArray());它抛出一个异常如下;

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."

我不确定这是什么意思以及如何克服它.

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

例如.

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;
        }

推荐答案

BitConverter.GetBytes 用于将基本数据类型转换为字节数组,将字节数组转换为基本数据类型.请看下面的代码.

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);

调用 CopyTo 方法后,数据数组将填充此结果.

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());

更新:实际上,在您的场景中,没有必要通过 BitConverter 转换字节数据.您可以尝试使用以下代码.

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;
            }

这篇关于UWP C# UART 数据缓冲区使用数组还是列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆