I have a working version from your original question on this subject which you can use to build on:
1. The C# console application
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.IO.Pipes;
class Test
{
static int CsTest()
{
int rc = 0;
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream(".", "CSServer", PipeDirection.InOut))
{
Console.Write("Attempting to connect to pipe...");
pipeClient.Connect();
Console.WriteLine("Connected to pipe.");
Console.WriteLine("There are currently {0} pipe server instances open.",
pipeClient.NumberOfServerInstances);
StreamWriter sw = new StreamWriter(pipeClient);
StreamReader sr = new StreamReader(pipeClient);
while (true)
{
try
{
sw.AutoFlush = true;
sw.WriteLine("Hello from c#");
string temp = sr.ReadLine();
Console.WriteLine("Received from python : {0}", temp);
}
catch (EndOfStreamException)
{
break;
}
}
}
return rc;
}
static void Main(string[] args)
{
int result = CsTest();
Console.WriteLine($"\nC# test result: {result}");
}
}
2. The Python application
import win32file
import win32pipe
import time
pipeName="CSServer"
pipe_handle = win32pipe.CreateNamedPipe(
r'\\.\pipe\\'+pipeName,
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
0,
None)
win32pipe.ConnectNamedPipe(pipe_handle, None)
while True:
ret, read_message = win32file.ReadFile(pipe_handle, 1000)
print(F'{ret = } Received from c#: ' + read_message.decode('utf-8'))
ret, length = win32file.WriteFile(pipe_handle, 'Hello from Python\n'.encode())
print(F'{ret = }, {length = } from WriteFile')
win32file.FlushFileBuffers(pipe_handle)
time.sleep(2)
You can use this to test the basic connection between the two sides, and then add whetever other features you need.