static void Main() { NamedPipeServerStream pipe_server = new NamedPipeServerStream("AC_pipe"); pipe_server.WaitForConnection(); byte[] command = new byte[16]; while (true) { pipe_server.Read(command, 0, command.Length); //server do some work according to command received //here simply echo command reply reply = util.convert_to_reply(command); byte[] echo_msg = util.form_byte_array(reply.order_type, reply.is_completed, reply.yaw_degree, reply.pitch_degree); if (pipe_server.IsConnected) { pipe_server.Write(echo_msg, 0, echo_msg.Length); } else { break; } } }
public static reply convert_to_reply(byte[] arr) { byte[] op = new byte[4]; byte[] is_sc = new byte[4]; byte[] yaw = new byte[4]; byte[] pitch = new byte[4]; op = sub_array(arr, 0, 4); is_sc = sub_array(arr, 4, 4); yaw = sub_array(arr, 8, 4); pitch = sub_array(arr, 12, 4); reply pack = new reply(); Int32 op_int = BitConverter.ToInt32(op, 0); Int32 is_sc_int = BitConverter.ToInt32(is_sc, 0); float yaw_float = BitConverter.ToSingle(yaw, 0); float pitch_float = BitConverter.ToSingle(pitch, 0); switch (op_int) { case 0: pack.order_type = order_type.Pose; break; case 10: pack.order_type = order_type.Reset; break; case 20: pack.order_type = order_type.Yaw; break; case 30: pack.order_type = order_type.Pitch; break; case 40: pack.order_type = order_type.Rotation; break; case 50: pack.order_type = order_type.Spin; break; default: break; } if (is_sc_int == 1) { pack.is_completed = true; } else { pack.is_completed = false; } pack.yaw_degree = yaw_float; pack.pitch_degree = pitch_float; return(pack); }
static void Main(string[] args) { foreach (var process in Process.GetProcessesByName("named_pipe_server")) { process.Kill(); } //run named pipe server ProcessStartInfo start_info = new ProcessStartInfo(); start_info.CreateNoWindow = true; start_info.UseShellExecute = true; start_info.FileName = "..\\..\\..\\named_pipe_server\\bin\\Debug\\named_pipe_server.exe"; Process ac_process = Process.Start(start_info); //connect to named pipe NamedPipeClientStream pipe_client = new NamedPipeClientStream("AC_pipe"); pipe_client.Connect(); float yaw_degree = 0.0f; float pitch_degree = 0.0f; Random i = new Random(); Random j = new Random(); yaw_degree = (float)i.Next() + (float)j.Next() / 10.0f; pitch_degree = (float)j.Next() + (float)i.Next() / 10.0f; byte[] command = util.form_byte_array(order_type.Pose, false, yaw_degree, pitch_degree); Console.WriteLine("send: " + order_type.Reset.ToString() + ", " + true.ToString() + ", " + yaw_degree.ToString() + ", " + pitch_degree.ToString()); pipe_client.Write(command, 0, 16); //read reply form server byte[] reply = new byte[16]; pipe_client.Read(reply, 0, 16); reply pack = util.convert_to_reply(reply); Console.WriteLine("received: " + pack.order_type.ToString() + ", " + pack.is_completed.ToString() + ", " + pack.yaw_degree.ToString() + ", " + pack.pitch_degree.ToString()); Console.ReadKey(); }