c# - 需要帮助弄清楚如何在 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)'\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);
}
}
推荐阅读
- python - 将工作日名称字符串转换为日期时间
- python - 我可以在不更改记录的模块名称/函数名称/行号的情况下扩展 Logger 类吗?
- java - 如何获取密钥以及如何添加更多值?
- java - Vertx for Java 中的依赖注入/ORM
- php - 如果内容存在,则将逗号或破折号附加到参数
- reactjs - 有没有办法在获取或重新加载数据时在反应虚拟化表中显示加载微调器?
- json - Dart 解码转义 JSON
- python - tkinter 在使用参数调用函数后导致函数挂起
- r - 意外的 as.POSIXct 和 strptime 围绕 DST 的行为
- coldfusion - 如何在浏览器中将cfdocument pdf文件和服务器返回给用户?