I'm writing a serial port to TCP bridge in C#. It's a console application with 4 threads.
- main thread for reading commands from console (e.g. 'exit' to close the application)
- if a connection is established, it handles a client
- a TCP worker thread, which copies all received TCP data to COM3
- a UART worker thread, which copies all received UART data to TCP
This applications works fine if it's launched locally on my PC either in Visual Studio debug modus, in CMD.exe or even in PowerShell.
Now I want to grant one of my colleges the right to launch UARTServer.exe in a remote PowerShell session. PS is setup in the correct way and he can use PS like a ssh session (Enter-PSSession -ComputerName xxxx -Credentials yyyy).
But when he starts UARTServer.exe in his remote session, Console.ReadLine()
returns with null.
Here is the C# snipped for the main thread, which reads from stdin to look for commands like 'exit'.
Console.WriteLine("Starting UARTServer...");
uartServerThread = new Thread(new ThreadStart(ServerWorker));
uartServerThread.Start();
Console.WriteLine("Enter 'exit' to stop this server.");
Console.Write("UARTServer> ");
while(true)
{
var input = Console.ReadLine();
if (input == "exit")
{
break;
}
else if (input == null)
{
Console.WriteLine("Null");
break;
}
else
{
Console.WriteLine("Unknown command");
Console.Write("UARTServer> ");
}
}
Console.WriteLine("Shutting down UARTServer...");
uartServerThread.Abort(true);
uartServerThread.Join();
Console.WriteLine("Exiting UARTServer");
This is the corresponding console output:
[localhost]: PS D:\Shares\SATA> .\UARTServer.exe
Starting UARTServer...
Enter 'exit' to stop this server.
UARTServer> Null
Shutting down UARTServer...
Exiting UARTServer
[localhost]: PS D:\Shares\SATA>
My questions:
- Why does it happen? Is Stdin not connected in a remote session?
- How can I fix this?