[英]Need help figuring out how to capture and display a C type struct value in a C# WinForms GUI
我有一個 C 程序,它將顯示從端口 51020 捕獲的 UDP 數據。UDP 數據包是結構,我想關注的數據是我需要指向的結構中的一個字段。 基本上這將是一個實時顯示。 第一件事是,我需要有關在 C# 中創建類似 C 結構的指導,以及如何訪問和顯示捕獲到 label 或 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# 中的 label 上。還能夠指向結構中的特定字段以訪問數組中的值。 我是 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)'\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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.