需要帮助了解如何在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#的新手,如果有任何帮助,我们将不胜感激。

很多人不会喜欢这个,但它应该可以

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)'', 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);
}
}

最新更新