赞
踩
用于同一台电脑两个进程之间数据交互
一、声明管道标识名serverName,服务器、客户端基于此创建
二、创建一个服务器 NamedPipeServerStream ,以此获取读取数据流(StreamReader)、写入数据流(StreamWriter);然后等待客户端连接 serverStream_m.WaitForConnection(); 客户端连接后,用读取数据流循环读取缓存数据ReadLine();
三、创建一个客户端 NamedPipeClientStream,以此获取读取数据流(StreamReader)、写入数据流(StreamWriter);然后客户端连接服务器Connect()
四、定义通讯协议,即接收指定数据做指定事情
五、服务器、客户端写入数据流(StreamWriter)写入数据后,一定要调用Flush()函数,把数据写入到缓存区
PS:在代码中打开另外一个进程
- Process[] processes = Process.GetProcessesByName("程序名称");
- foreach (Process process1 in processes)
- {
- process1.Kill();
- }
- Process process = Process.Start("程序名称.exe");
1、服务器代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.IO.Pipes;
- using System.Threading;
-
- namespace NamedPipeComm
- {
- public class PipeServer
- {
- private NamedPipeServerStream serverStream_m = null;
- private StreamReader sReader_m;
- private StreamWriter sWriter_m;
- private object lock_m = new object();
- private IRCameraControl IRCamera_m;
-
- private const string TOTAL_TEMP = "TOTAL";
- private const string SINGLE_TEMP = "SINGLE";
- private const string IS_CONNECT = "CONNECT";
- private const string SHOW = "SHOW";
- private const string HIDE = "HIDE";
- private const string CLOSE = "CLOSE";
-
- public PipeServer(string serverName, IRCameraControl iRCamera)
- {
- serverStream_m = new NamedPipeServerStream(serverName);
-
- sReader_m = new StreamReader(serverStream_m);
- sWriter_m = new StreamWriter(serverStream_m);
-
- IRCamera_m = iRCamera;
- HandleDataReceived();
- }
-
- /// <summary>
- /// 处理客户端数据
- /// </summary>
- /// <param name="ar"></param>
- private void HandleDataReceived()
- {
- Task.Run(delegate
- {
- serverStream_m.WaitForConnection();
- do
- {
- try
- {
- if (serverStream_m.IsConnected)
- {
- string[] data = sReader_m.ReadLine().Split(',');
-
- switch (data[0])
- {
- case TOTAL_TEMP:
- float[] fInfo = IRCamera_m.GetTempInfoTotalROI();
- if (fInfo.Length == 3)
- {
- sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo[0], fInfo[1], fInfo[2]));
- sWriter_m.Flush();
- }
- break;
- case SINGLE_TEMP:
- try
- {
- int count = Convert.ToInt32(data[1]);
- float[] fInfo1 = IRCamera_m.GetTempInfoROI(count);
- if (fInfo1.Length == 3)
- {
- sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo1[0], fInfo1[1], fInfo1[2]));
- sWriter_m.Flush();
- }
- }
- catch (Exception ex)
- {
-
- }
- break;
- case IS_CONNECT:
- if (IRCamera_m.IsConnect)
- {
- sWriter_m.WriteLine(IS_CONNECT);
- sWriter_m.Flush();
- }
- break;
- case SHOW:
- IRCamera_m.ShowForm();
- break;
- case CLOSE:
- if (IRCamera_m.frmIRAY_m != null)
- {
- IRCamera_m.frmIRAY_m.Close();
- }
- else
- {
- IRCamera_m.ReleaseSDK();
- }
- break;
- case HIDE:
- if (IRCamera_m.frmIRAY_m != null)
- {
- IRCamera_m.frmIRAY_m.Hide();
- }
- break;
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- Thread.Sleep(10);
- } while (true);
- });
- }
- public void Close()
- {
- if (sReader_m != null)
- {
- sReader_m.Close();
- sReader_m.Dispose();
- sReader_m = null;
- }
- if (sWriter_m != null)
- {
- sWriter_m.Close();
- sWriter_m.Dispose();
- sWriter_m = null;
- }
- if (serverStream_m != null)
- {
- serverStream_m.Close();
- serverStream_m.Dispose();
- serverStream_m = null;
- }
- }
- }
- }

2、客户端代码
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Pipes;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace NamedPipeComm
- {
- public class PipeClient
- {
- public bool IsConnect
- {
- get;
- set;
- }
-
- private NamedPipeClientStream clientStream_m = null;
- //private BinaryReader sReader_m;
- //private BinaryWriter sWriter_m;
- private StreamReader sReader_m;
- private StreamWriter sWriter_m;
-
- private const string TOTAL_TEMP = "TOTAL";
- private const string SINGLE_TEMP = "SINGLE";
- private const string IS_CONNECT = "CONNECT";
- private const string SHOW = "SHOW";
- private const string HIDE = "HIDE";
- private const string CLOSE = "CLOSE";
-
- public PipeClient(string serverName)
- {
- clientStream_m = new NamedPipeClientStream(serverName);
- //sReader_m = new BinaryReader(clientStream_m);
- //sWriter_m = new BinaryWriter(clientStream_m);
- sReader_m = new StreamReader(clientStream_m);
- sWriter_m = new StreamWriter(clientStream_m);
- try
- {
- clientStream_m.Connect(3000);
- }
- catch (Exception ex)
- {
-
- }
- }
-
- public bool Open()
- {
- IsConnect = false;
- if (QueryLine(IS_CONNECT) == IS_CONNECT)
- {
- IsConnect = true;
- return IsConnect;
- }
- return IsConnect;
- }
- public void Close()
- {
- if (sReader_m != null)
- {
- sReader_m.Close();
- sReader_m.Dispose();
- sReader_m = null;
- }
- if (sWriter_m != null)
- {
- sWriter_m.Close();
- sWriter_m.Dispose();
- sWriter_m = null;
- }
- if (clientStream_m != null)
- {
- clientStream_m.Close();
- clientStream_m.Dispose();
- clientStream_m = null;
- }
- }
- public string QueryLine(string str)
- {
- string result = "";
- try
- {
- sWriter_m.WriteLine(str);
- sWriter_m.Flush();
- DateTime start = DateTime.Now;
- do
- {
- result = sReader_m.ReadLine();
-
- if (start.Subtract(DateTime.Now).TotalSeconds > 5 || result != "")
- {
- break;
- }
- } while (true);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
-
- /// <summary>
- /// 获取ROI温度信息,返回值索引:0=均值,1=最小值,2=最大值
- /// </summary>
- /// <param name="index">ROI索引0~,注意需小于选择的ROI个数,否则返回错误数据</param>
- /// <returns></returns>
- public float[] GetTempInfoROI(int index)
- {
- float[] value = new float[3];
- string write = SINGLE_TEMP + "," + index.ToString();
- string[] reads = QueryLine(write).Split(',');
- try
- {
- value[0] = Convert.ToSingle(reads[0]);
- value[1] = Convert.ToSingle(reads[1]);
- value[2] = Convert.ToSingle(reads[2]);
- }
- catch (Exception ex)
- {
-
- }
- return value;
- }
-
- /// <summary>
- /// 获取ROI合计温度信息,返回值索引:0=均值,1=最小值,2=最大值
- /// </summary>
- /// <returns></returns>
- public float[] GetTempInfoTotalROI()
- {
- float[] value = new float[3];
- string write = TOTAL_TEMP;
- string[] reads = QueryLine(write).Split(',');
- try
- {
- value[0] = Convert.ToSingle(reads[0]);
- value[1] = Convert.ToSingle(reads[1]);
- value[2] = Convert.ToSingle(reads[2]);
- }
- catch (Exception ex)
- {
-
- }
- return value;
- }
-
- public void ShowForm()
- {
- try
- {
- sWriter_m.WriteLine(SHOW);
- sWriter_m.Flush();
- }
- catch (Exception ex)
- {
- }
- }
- public void HideForm()
- {
- try
- {
- sWriter_m.WriteLine(HIDE);
- sWriter_m.Flush();
- }
- catch (Exception ex)
- {
- }
- }
- public void ReleaseSDK()
- {
- try
- {
- sWriter_m.WriteLine(CLOSE);
- sWriter_m.Flush();
- }
- catch (Exception ex)
- {
- }
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。