当前位置:   article > 正文

C# NamedPipe管道的简单应用_namedpipline c#实现

namedpipline c#实现

用于同一台电脑两个进程之间数据交互

一、声明管道标识名serverName,服务器、客户端基于此创建

二、创建一个服务器 NamedPipeServerStream ,以此获取读取数据流(StreamReader)、写入数据流(StreamWriter);然后等待客户端连接 serverStream_m.WaitForConnection(); 客户端连接后,用读取数据流循环读取缓存数据ReadLine();

三、创建一个客户端 NamedPipeClientStream,以此获取读取数据流(StreamReader)、写入数据流(StreamWriter);然后客户端连接服务器Connect()

四、定义通讯协议,即接收指定数据做指定事情

五、服务器、客户端写入数据流(StreamWriter)写入数据后,一定要调用Flush()函数,把数据写入到缓存区

PS:在代码中打开另外一个进程

  1. Process[] processes = Process.GetProcessesByName("程序名称");
  2. foreach (Process process1 in processes)
  3. {
  4. process1.Kill();
  5. }
  6. Process process = Process.Start("程序名称.exe");

1、服务器代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.IO.Pipes;
  8. using System.Threading;
  9. namespace NamedPipeComm
  10. {
  11. public class PipeServer
  12. {
  13. private NamedPipeServerStream serverStream_m = null;
  14. private StreamReader sReader_m;
  15. private StreamWriter sWriter_m;
  16. private object lock_m = new object();
  17. private IRCameraControl IRCamera_m;
  18. private const string TOTAL_TEMP = "TOTAL";
  19. private const string SINGLE_TEMP = "SINGLE";
  20. private const string IS_CONNECT = "CONNECT";
  21. private const string SHOW = "SHOW";
  22. private const string HIDE = "HIDE";
  23. private const string CLOSE = "CLOSE";
  24. public PipeServer(string serverName, IRCameraControl iRCamera)
  25. {
  26. serverStream_m = new NamedPipeServerStream(serverName);
  27. sReader_m = new StreamReader(serverStream_m);
  28. sWriter_m = new StreamWriter(serverStream_m);
  29. IRCamera_m = iRCamera;
  30. HandleDataReceived();
  31. }
  32. /// <summary>
  33. /// 处理客户端数据
  34. /// </summary>
  35. /// <param name="ar"></param>
  36. private void HandleDataReceived()
  37. {
  38. Task.Run(delegate
  39. {
  40. serverStream_m.WaitForConnection();
  41. do
  42. {
  43. try
  44. {
  45. if (serverStream_m.IsConnected)
  46. {
  47. string[] data = sReader_m.ReadLine().Split(',');
  48. switch (data[0])
  49. {
  50. case TOTAL_TEMP:
  51. float[] fInfo = IRCamera_m.GetTempInfoTotalROI();
  52. if (fInfo.Length == 3)
  53. {
  54. sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo[0], fInfo[1], fInfo[2]));
  55. sWriter_m.Flush();
  56. }
  57. break;
  58. case SINGLE_TEMP:
  59. try
  60. {
  61. int count = Convert.ToInt32(data[1]);
  62. float[] fInfo1 = IRCamera_m.GetTempInfoROI(count);
  63. if (fInfo1.Length == 3)
  64. {
  65. sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo1[0], fInfo1[1], fInfo1[2]));
  66. sWriter_m.Flush();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. }
  72. break;
  73. case IS_CONNECT:
  74. if (IRCamera_m.IsConnect)
  75. {
  76. sWriter_m.WriteLine(IS_CONNECT);
  77. sWriter_m.Flush();
  78. }
  79. break;
  80. case SHOW:
  81. IRCamera_m.ShowForm();
  82. break;
  83. case CLOSE:
  84. if (IRCamera_m.frmIRAY_m != null)
  85. {
  86. IRCamera_m.frmIRAY_m.Close();
  87. }
  88. else
  89. {
  90. IRCamera_m.ReleaseSDK();
  91. }
  92. break;
  93. case HIDE:
  94. if (IRCamera_m.frmIRAY_m != null)
  95. {
  96. IRCamera_m.frmIRAY_m.Hide();
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. }
  105. Thread.Sleep(10);
  106. } while (true);
  107. });
  108. }
  109. public void Close()
  110. {
  111. if (sReader_m != null)
  112. {
  113. sReader_m.Close();
  114. sReader_m.Dispose();
  115. sReader_m = null;
  116. }
  117. if (sWriter_m != null)
  118. {
  119. sWriter_m.Close();
  120. sWriter_m.Dispose();
  121. sWriter_m = null;
  122. }
  123. if (serverStream_m != null)
  124. {
  125. serverStream_m.Close();
  126. serverStream_m.Dispose();
  127. serverStream_m = null;
  128. }
  129. }
  130. }
  131. }

2、客户端代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Pipes;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NamedPipeComm
  9. {
  10. public class PipeClient
  11. {
  12. public bool IsConnect
  13. {
  14. get;
  15. set;
  16. }
  17. private NamedPipeClientStream clientStream_m = null;
  18. //private BinaryReader sReader_m;
  19. //private BinaryWriter sWriter_m;
  20. private StreamReader sReader_m;
  21. private StreamWriter sWriter_m;
  22. private const string TOTAL_TEMP = "TOTAL";
  23. private const string SINGLE_TEMP = "SINGLE";
  24. private const string IS_CONNECT = "CONNECT";
  25. private const string SHOW = "SHOW";
  26. private const string HIDE = "HIDE";
  27. private const string CLOSE = "CLOSE";
  28. public PipeClient(string serverName)
  29. {
  30. clientStream_m = new NamedPipeClientStream(serverName);
  31. //sReader_m = new BinaryReader(clientStream_m);
  32. //sWriter_m = new BinaryWriter(clientStream_m);
  33. sReader_m = new StreamReader(clientStream_m);
  34. sWriter_m = new StreamWriter(clientStream_m);
  35. try
  36. {
  37. clientStream_m.Connect(3000);
  38. }
  39. catch (Exception ex)
  40. {
  41. }
  42. }
  43. public bool Open()
  44. {
  45. IsConnect = false;
  46. if (QueryLine(IS_CONNECT) == IS_CONNECT)
  47. {
  48. IsConnect = true;
  49. return IsConnect;
  50. }
  51. return IsConnect;
  52. }
  53. public void Close()
  54. {
  55. if (sReader_m != null)
  56. {
  57. sReader_m.Close();
  58. sReader_m.Dispose();
  59. sReader_m = null;
  60. }
  61. if (sWriter_m != null)
  62. {
  63. sWriter_m.Close();
  64. sWriter_m.Dispose();
  65. sWriter_m = null;
  66. }
  67. if (clientStream_m != null)
  68. {
  69. clientStream_m.Close();
  70. clientStream_m.Dispose();
  71. clientStream_m = null;
  72. }
  73. }
  74. public string QueryLine(string str)
  75. {
  76. string result = "";
  77. try
  78. {
  79. sWriter_m.WriteLine(str);
  80. sWriter_m.Flush();
  81. DateTime start = DateTime.Now;
  82. do
  83. {
  84. result = sReader_m.ReadLine();
  85. if (start.Subtract(DateTime.Now).TotalSeconds > 5 || result != "")
  86. {
  87. break;
  88. }
  89. } while (true);
  90. }
  91. catch (Exception ex)
  92. {
  93. }
  94. return result;
  95. }
  96. /// <summary>
  97. /// 获取ROI温度信息,返回值索引:0=均值,1=最小值,2=最大值
  98. /// </summary>
  99. /// <param name="index">ROI索引0~,注意需小于选择的ROI个数,否则返回错误数据</param>
  100. /// <returns></returns>
  101. public float[] GetTempInfoROI(int index)
  102. {
  103. float[] value = new float[3];
  104. string write = SINGLE_TEMP + "," + index.ToString();
  105. string[] reads = QueryLine(write).Split(',');
  106. try
  107. {
  108. value[0] = Convert.ToSingle(reads[0]);
  109. value[1] = Convert.ToSingle(reads[1]);
  110. value[2] = Convert.ToSingle(reads[2]);
  111. }
  112. catch (Exception ex)
  113. {
  114. }
  115. return value;
  116. }
  117. /// <summary>
  118. /// 获取ROI合计温度信息,返回值索引:0=均值,1=最小值,2=最大值
  119. /// </summary>
  120. /// <returns></returns>
  121. public float[] GetTempInfoTotalROI()
  122. {
  123. float[] value = new float[3];
  124. string write = TOTAL_TEMP;
  125. string[] reads = QueryLine(write).Split(',');
  126. try
  127. {
  128. value[0] = Convert.ToSingle(reads[0]);
  129. value[1] = Convert.ToSingle(reads[1]);
  130. value[2] = Convert.ToSingle(reads[2]);
  131. }
  132. catch (Exception ex)
  133. {
  134. }
  135. return value;
  136. }
  137. public void ShowForm()
  138. {
  139. try
  140. {
  141. sWriter_m.WriteLine(SHOW);
  142. sWriter_m.Flush();
  143. }
  144. catch (Exception ex)
  145. {
  146. }
  147. }
  148. public void HideForm()
  149. {
  150. try
  151. {
  152. sWriter_m.WriteLine(HIDE);
  153. sWriter_m.Flush();
  154. }
  155. catch (Exception ex)
  156. {
  157. }
  158. }
  159. public void ReleaseSDK()
  160. {
  161. try
  162. {
  163. sWriter_m.WriteLine(CLOSE);
  164. sWriter_m.Flush();
  165. }
  166. catch (Exception ex)
  167. {
  168. }
  169. }
  170. }
  171. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/220887
推荐阅读
相关标签
  

闽ICP备14008679号