Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created January 7, 2024 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/18c040b1c7941464403aa772ecafa79f to your computer and use it in GitHub Desktop.
Save dj1711572002/18c040b1c7941464403aa772ecafa79f to your computer and use it in GitHub Desktop.
C# Console UDP Recieving F9P RTK binary data
using System;
using static System.Net.Mime.MediaTypeNames;
public class UdpReceiver
{
static void Main()
{
//バインドするローカルIPとポート番号
string localIpString = "192.168.0.109";
System.Net.IPAddress localAddress =
System.Net.IPAddress.Parse(localIpString);
int localPort = 10000;
int n=0;
//UdpClientを作成し、ローカルエンドポイントにバインドする
System.Net.IPEndPoint localEP =
new System.Net.IPEndPoint(localAddress, localPort);
System.Net.Sockets.UdpClient udp =
new System.Net.Sockets.UdpClient(localEP);
for (; ; )
{
//データを受信する
System.Net.IPEndPoint remoteEP = null;
byte[] rcvBytes = udp.Receive(ref remoteEP);
for (int i = 0; i < rcvBytes.Length; i++)
{
string hexdata = rcvBytes[i].ToString("X");
Console.Write(hexdata);
Console.Write(',');
}
//データを文字列に変換する
string rcvMsg = System.Text.Encoding.ASCII.GetString(rcvBytes);
//受信したデータと送信者の情報を表示する
//Console.WriteLine("受信したデータ:{0}", rcvMsg);
Console.WriteLine();
Console.WriteLine(n.ToString()+":送信元アドレス:{0}/ポート番号:{1}",
remoteEP.Address, remoteEP.Port);
//"exit"を受信したら終了
//ConsoleKeyInfo input = Console.ReadKey();
// Console.WriteLine("押されたキーは" + input.Key.ToString());
if (Console.KeyAvailable == true)
{
break;
}
n++;
}
//UdpClientを閉じる
udp.Close();
Console.WriteLine("終了しました。");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment