add some doc and jtag controller

This commit is contained in:
2025-04-02 21:51:20 +08:00
parent 0da5b85173
commit 2af0986d67
7 changed files with 232 additions and 43 deletions

View File

@@ -2,12 +2,29 @@ using System.Net;
using System.Net.Sockets;
using System.Text;
public struct UDPData
{
public DateTime datetime;
public string addr;
public int port;
public byte[] data;
}
/// <summary>
/// UDP Server
/// </summary>
public class UDPServer
{
private int listenPort;
private UdpClient listener;
private IPEndPoint groupEP;
private Dictionary<string, UDPData> udpData = new Dictionary<string, UDPData>();
/// <summary>
/// Construct a udp server with fixed port
/// </summary>
/// <param name="port"> Device UDP Port </param>
/// <returns> UDPServer class </returns>
public UDPServer(int port)
{
// Construction
@@ -31,9 +48,31 @@ public class UDPServer
{
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
byte[] bytes = listener.EndReceive(res, ref remoteEP);
var nowtime = DateTime.Now;
// Handle RemoteEP
string remoteStr;
if (remoteEP is not null)
{
var remoteAddress = remoteEP.Address.ToString();
var remotePort = remoteEP.Port;
// Record UDP Receive Data
udpData.Add(remoteAddress, new UDPData()
{
addr = remoteAddress,
port = remotePort,
data = bytes,
datetime = nowtime,
});
remoteStr = $"{remoteAddress}:{remotePort}";
}
else
{
remoteStr = "Unknown";
}
// Handle Package
var sign = bytes[0];
string recvData;
if (sign == (byte)WebProtocol.PackSign.SendAddr)
{
@@ -60,8 +99,7 @@ public class UDPServer
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
string remoteStr = (remoteEP is null) ? "Unknown" : $"{remoteEP.Address.ToString()}:{remoteEP.Port.ToString()}";
Console.WriteLine($"Receive Data from {remoteStr} at {DateTime.Now.ToString()}:");
Console.WriteLine($"Receive Data from {remoteStr} at {nowtime.ToString()}:");
Console.WriteLine($"Original Data: {BitConverter.ToString(bytes).Replace("-", " ")}");
if (recvData.Length != 0) Console.WriteLine(recvData);
Console.WriteLine();
@@ -69,6 +107,10 @@ public class UDPServer
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
}
/// <summary>
/// Start UDP Server
/// </summary>
/// <returns>None</returns>
public void Start()
{
try
@@ -85,6 +127,10 @@ public class UDPServer
}
}
/// <summary>
/// Close UDP Server
/// </summary>
/// <returns>None</returns>
public void Stop()
{
listener.Close();