首页 > 解决方案 > 需要帮助弄清楚如何在 C# WinForms GUI 中捕获和显示 C 类型的结构值

问题描述

我有一个 C 程序,它将显示从端口 51020 捕获的 UDP 数据。UDP 数据包是结构,我想要关注的数据是结构中我需要指向的字段。基本上这将是一个实时显示。首先,我需要有关在 C# 中创建类似 C 的结构的指导,以及如何在 C# 的 GUI 上访问和显示捕获到标签或文本框中的数据。

我引用的 C 类结构是:

typedef struct DD_DATAGRM {
    unsigned long LENGTH;
    unsigned long ABCD_Frame;
    unsigned long CHANNEL[256];
    unsigned char STATUS[256];
    unsigned long CRC32;
} DATAGRAM;
**DATAGRAM recv_data**

为了获得所需的数据,我将在 C 中使用以下语法:

*(float*)&recv_data.CHANNEL[0]

我目前拥有的简单 C# 程序的代码是:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;


namespace UDP_Recv
{
    
   public partial class Form1 : Form
   {

       UdpClient Client = new UdpClient(51020); // port number
       string data = "";
    
       public Form1()
       {
           InitializeComponent();
       }


       private void button1_Click(object sender, EventArgs e)
       {

           try
           {
               Client.BeginReceive(new AsyncCallback(recv), null);
           }
           catch(Exception ex)
           {
               richTextBox1.Text += ex.Message.ToString();
           }
       }

       void recv(IAsyncResult res)
       { 

           IPEndPoint RemoteIP = new IPEndPoint(IPAddress.Any, 60240);
           byte[] received = Client.EndReceive(res, ref RemoteIP);
           data = Encoding.UTF8.GetString(received);
         
           //to avoid cross-threading we use Method Invoker
           this.Invoke(new MethodInvoker(delegate
           {
               richTextBox1.Text += "\nReceived data: " + data;
           }));

           Client.BeginReceive(new AsyncCallback(recv), null);
       }
    }}

我需要帮助创建一个将在端口 51020 上接收并显示在 C# 中的文本框或标签上的结构。还能够指向结构中的特定字段以访问数组中的值。我是 C# 新手,任何帮助将不胜感激。

标签: c#cwinformsstruct

解决方案


很多人不会喜欢这个,但它应该工作

       const int SIZE = 8 + 8 + 2048 + 256 + 8;
        public class DD_DATAGRM
        {
            public ulong LENGTH { get; set; }
            public ulong ABCD_Frame { get; set; }
            public ulong[] CHANNEL = new ulong[256];
            public string STATUS = "";
            public ulong CRC32 { get; set; }
        }

        static void Main(string[] args)
        {
            byte[] received = { 0x01, 0x02, 0x03 };
            List<DD_DATAGRM> ChannelBase = new List<DD_DATAGRM>();

            //check the receive data is multiple of DD_DATAGRM
            if (received.Length % SIZE != 0)
            {
                Console.WriteLine("ERROR");
            }
            else
            {
                for (int i = 0; i < received.Length; i += SIZE)
                {
                    DD_DATAGRM newDataGRM = new DD_DATAGRM();
                    newDataGRM.LENGTH = BitConverter.ToUInt64(received, i);
                    newDataGRM.ABCD_Frame = BitConverter.ToUInt64(received, i + 8);
                    for (int j = 0; j < 256; j++)
                    {
                        newDataGRM.CHANNEL[j] = BitConverter.ToUInt64(received, (8 * j) + 16);
                    }
                    int location = Array.IndexOf(received, (byte)'\0', 2064);
                    newDataGRM.STATUS = Encoding.UTF8.GetString(received, 2064, location - 2064);
                    newDataGRM.CRC32 = BitConverter.ToUInt64(received, i + 2320);
                    ChannelBase.Add(newDataGRM);
                }
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("Frame", typeof(ulong));
            dt.Columns.Add("Status", typeof(string));
            for (int col = 0; col < 256; col++)
            {
                dt.Columns.Add(col.ToString(), typeof(ulong));
            }

            foreach (DD_DATAGRM gram in ChannelBase)
            {
                List<object> rowData = new List<object>();
                rowData.Add(gram.ABCD_Frame);
                rowData.Add(gram.STATUS);
                rowData.AddRange(gram.CHANNEL.Cast<List<object>>());
                dt.Rows.Add(rowData);
            }
        }

推荐阅读