diff --git a/ProtocolTests.sln b/ProtocolTests.sln index 3c09c0a..00040f8 100644 --- a/ProtocolTests.sln +++ b/ProtocolTests.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33424.131 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdpTestApp", "UdpTestApp\UdpTestApp.csproj", "{97751007-78CC-4356-9F17-F118BA9297FB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Debug|Any CPU.Build.0 = Debug|Any CPU {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Release|Any CPU.ActiveCfg = Release|Any CPU {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Release|Any CPU.Build.0 = Release|Any CPU + {97751007-78CC-4356-9F17-F118BA9297FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97751007-78CC-4356-9F17-F118BA9297FB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97751007-78CC-4356-9F17-F118BA9297FB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97751007-78CC-4356-9F17-F118BA9297FB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/UdpTestApp/Program.cs b/UdpTestApp/Program.cs new file mode 100644 index 0000000..32e3610 --- /dev/null +++ b/UdpTestApp/Program.cs @@ -0,0 +1,144 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace UdpTestApp; + +public class Program +{ + private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; + private static bool _continue; + private static Thread readThread; + private static UdpClient udpClient = null; + + public static bool applicationQuitRequested { get; private set; } + + static void Main(string[] args) + { + while (applicationQuitRequested == false) + { + Console.WriteLine($"----- USB HID PROTOCOL TEST APP -----"); + Console.WriteLine($"Command list:"); + Console.WriteLine($"s = start"); + Console.WriteLine($"q = quit"); + + var key = Console.ReadKey(); + + switch (key.Key) + { + // start + case ConsoleKey.S: + Console.Write($"Write hostname: "); + string? hostname = Console.ReadLine(); + + if (hostname == null || + String.IsNullOrEmpty(hostname)) + { + Console.WriteLine($"Invalid hostname entered."); + break; + } + + Console.Write($"Write port number: "); + string? portString = Console.ReadLine(); + int port; + + if (portString == null || + portString == "" || + Int32.TryParse(portString, out port) == false) + { + Console.WriteLine($"Invalid port entered."); + break; + } + + try + { + udpClient = new UdpClient(11000); + udpClient.Connect(hostname, port); + } + catch (Exception) + { + Console.WriteLine($"Could not connect to endpoint."); + break; + } + + _continue = true; + readThread = new Thread(Read); + readThread.Start(); + + Console.WriteLine("Connected to device. Write 'quit' to end communication."); + + while (_continue) + { + var message = Console.ReadLine(); + + if (stringComparer.Equals("quit", message)) + { + _continue = false; + } + else if (message != null && String.IsNullOrEmpty(message) == false) + { + try + { + byte[] msgBytes = Encoding.ASCII.GetBytes(message); + List listBytes = new List(); + listBytes.AddRange(msgBytes); + listBytes.Add((byte)'\n'); + var array = listBytes.ToArray(); + udpClient.Send(array, array.Length); + } + catch (Exception) + { + Console.WriteLine("Message could not be sent."); + } + + } + } + + readThread.Join(TimeSpan.FromSeconds(2)); + //readThread.Abort(); + readThread = null; + udpClient.Close(); + udpClient = null; + Console.WriteLine("Closed connection."); + + + break; + + // quit + case ConsoleKey.Q: + applicationQuitRequested = true; + break; + + default: + Console.WriteLine($"{Environment.NewLine}Did not recognize key.. {Environment.NewLine}"); + break; + } + + + } + } + + public static void Read() + { + while (_continue) + { + try + { + //IPEndPoint object will allow us to read datagrams sent from any source. + IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); + + // Blocks until a message returns on this socket from a remote host. + Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); + string returnData = Encoding.ASCII.GetString(receiveBytes); + + // Uses the IPEndPoint object to determine which of these two hosts responded. + Console.WriteLine("Message received: " + returnData.ToString()); + Console.WriteLine("Was sent from " + + RemoteIpEndPoint.Address.ToString() + + " on their port number " + + RemoteIpEndPoint.Port.ToString()); + } + catch { } + } + } +} \ No newline at end of file diff --git a/UdpTestApp/UdpTestApp.csproj b/UdpTestApp/UdpTestApp.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/UdpTestApp/UdpTestApp.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +