从包含数组的结构数组中访问数据

时间:2016-12-06 23:08:29

标签: c# visual-studio-2010

这是我在“包含阵列的结构数组”上提出的上一个问题的后续内容。感谢SO帮助,我能够获得一个包含从单独的数据字节数组初始化的数组的结构数组。

我正在研究的代码的目的是获取最终来自网络源的字节数据,该字节数据表示WVsamps结构声明中构造的波形样本数据包。

我需要在所述结构的数组中累积它们,以便我可以实时绘制波形,并返回并查看波形历史记录并进行测量。 ldSamps数组需要调整大小,以便可以接受最多0x40000个Wvsamps结构。样本达到1 Wvsamps struct size packet / sec。

这是WVsamps结构:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct Wvsamps
{
    const int Size = 3;
    fixed ushort wav1[Size];
    fixed ushort wav2[Size];
    fixed ushort wav3[Size];
    fixed ushort wav4[Size];
    public ushort modes;
    public uint time;
    public ushort chkSum;
}

它从诸如此类的字节数组中读取数据,该数组现在模拟通过网络连接接收的缓冲区中的数据:

namespace ScrollWave
{
    public static class waveData
    {
        public static byte[] wvSamps = 
        {
            0x94, 0xa5, 0xca, 0x62, 0x41, 0x28, 0x4c, 0x93, 0x09, 0x42, 0x00, 0x25, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x01, 0x40, 0x10, 0x00, 0x00, 0x70, 0x0d, 0x58, 0x3e, 0xc6, 0xd1, 0x07, 0x9c, 
            0x94, 0xa3, 0x8a, 0x62, 0x41, 0x29, 0x4c, 0x94, 0xc9, 0x32, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x01, 0x40, 0x10, 0x00, 0x00, 0x70, 0x0d, 0x58, 0x3e, 0xc6, 0xd2, 0x08, 0x0c, 
...etc.

以下是我如何将数据读入包含数组的结构数组(现在只需执行32个结构进行测试):

namespace ScrollWave
{
    public partial class Form1 : Form
    {
        public Form1() 
        {
            InitializeComponent();

            Wvsamps[] ldSamps = new Wvsamps[0x40000];                   // 72.8 hours worth of Wvsamps structs in ldSamps[] array
            int wvSampIndx = 0;

            for (int i = 0; i < 32; i++)                                // take 32 lines of wvSamps data and put into Wvsamps structs in ldSamps array
            {
                Wvsamps sample = new Wvsamps();
                //wav1 = [1,2,3]
                sample[0, 0] = waveData.wvSamps[wvSampIndx++];          // || waveData.wvSamps[wvSampIndx++];
                sample[0, 0] <<= 8;                                     // make bytes to shorts
                sample[0, 0] |= waveData.wvSamps[wvSampIndx++];

                sample[0, 1] = waveData.wvSamps[wvSampIndx++];
                sample[0, 1] <<= 8;
                sample[0, 1] |= waveData.wvSamps[wvSampIndx++];

                sample[0, 2] = waveData.wvSamps[wvSampIndx++];
                sample[0, 2] <<= 8;
                sample[0, 2] |= waveData.wvSamps[wvSampIndx++];

                //wav2 = [1,2,3]
                sample[1, 0] = waveData.wvSamps[wvSampIndx++];
                sample[1, 0] <<= 8;
                sample[1, 0] |= waveData.wvSamps[wvSampIndx++];

                sample[1, 1] = waveData.wvSamps[wvSampIndx++];
                sample[1, 1] <<= 8;
                sample[1, 1] |= waveData.wvSamps[wvSampIndx++];

                sample[1, 2] = waveData.wvSamps[wvSampIndx++];
                sample[1, 2] <<= 8;
                sample[1, 2] |= waveData.wvSamps[wvSampIndx++];

                //wav3 = [1,2,3]
                sample[2, 0] = waveData.wvSamps[wvSampIndx++];
                sample[2, 0] <<= 8;
                sample[2, 0] |= waveData.wvSamps[wvSampIndx++];

                sample[2, 1] = waveData.wvSamps[wvSampIndx++];
                sample[2, 1] <<= 8;
                sample[2, 1] |= waveData.wvSamps[wvSampIndx++];

                sample[2, 2] = waveData.wvSamps[wvSampIndx++];
                sample[2, 2] <<= 8;
                sample[2, 2] |= waveData.wvSamps[wvSampIndx++];

                //wav4 = [1,2,3]
                sample[3, 0] = waveData.wvSamps[wvSampIndx++];
                sample[3, 0] <<= 8;
                sample[3, 0] |= waveData.wvSamps[wvSampIndx++];

                sample[3, 1] = waveData.wvSamps[wvSampIndx++];
                sample[3, 1] <<= 8;
                sample[3, 1] |= waveData.wvSamps[wvSampIndx++];

                sample[3, 2] = waveData.wvSamps[wvSampIndx++];
                sample[3, 2] <<= 8;
                sample[3, 2] |= waveData.wvSamps[wvSampIndx++];

                //modes
                sample.modes = waveData.wvSamps[wvSampIndx++];
                sample.modes <<= 8;
                sample.modes |= waveData.wvSamps[wvSampIndx++];

                //time
                sample.time  = (uint)waveData.wvSamps[wvSampIndx++] << 24;
                sample.time |= (uint)waveData.wvSamps[wvSampIndx++] << 16;
                sample.time |= (uint)waveData.wvSamps[wvSampIndx++] << 8;
                sample.time |= (uint)waveData.wvSamps[wvSampIndx++];

                sample.chkSum = waveData.wvSamps[wvSampIndx++];
                sample.chkSum <<= 8;
                sample.chkSum |= waveData.wvSamps[wvSampIndx++];

                Console.WriteLine("{0:X04}, {1:X04}, {2:X04}, {3:X04}, {4:X04}, {5:X08}, {6:X04} ", sample[0, 0], sample[1, 0], sample[2, 0], sample[3, 0], sample.modes, sample.time, sample.chkSum);
            }

            // how would I access ldSamp.Wvsamps sample data here or in another method when I'm outside of the above for (::) loop?
            // ....
            //
        }

当我在for(;;)循环内部将wvSamps字节数组中的数据加载到WVsamps结构的ldSamps数组中时,我可以访问并向控制台发送WVsamps结构元素,以验证它们是否正确设置。

当我在for(;;)循环之外时,我无法弄清楚如何在特定的ldSamps数组位置访问WVsamps结构元素。

关注问题:

我想要做的是在WinForms中创建一个产品,我之前将其作为嵌入式产品运行在自定义硬件上,代码用C / C ++编写。我一直在尝试用C#中的C / C ++模拟我的算法和方法。也许在C#中有一种更合适的方法可以做到这一点,我试图将一个方形钉子敲入一个圆孔。 Lists或Dict或其他什么更适合解决这个问题,而不是坚持使用包含数组的结构数组?

0 个答案:

没有答案