下位機使用北京大華程控電源DH1766-1,上位機使用WPF。實現了電壓電流實時採集,曲線顯示。上午在公司調試成功,手頭沒有程控電源,使用TCP服務端模擬。昨天寫的TCP服務端正好排上用場。服務器
界面以下:dom
服務端socket
服務端實在上篇基礎上實現的。須要作以下更改:tcp
while (true)
{
try
{
byte[] bufferDate = new byte[1024];
int realLen = pSocket.Receive(bufferDate);函數
if (realLen <= 0)
{
this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "退出\r\n");測試
socketList.Remove(pSocket);
//客戶端退出的時候會發送一個空字節
pSocket.Shutdown(SocketShutdown.Both);
pSocket.Close();this
return;
}
string receiveStr = Encoding.Default.GetString(bufferDate, 0, realLen);
switch (receiveStr)
{
case "MEAS:VOLTage:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
break;
case "MEAS:CURR:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
break;
default:
break;
}
this.Invoke(addTextDelegate, receiveStr + "from" + pSocket.RemoteEndPoint.ToString() + "\r\n");
}
catch (Exception ex)
{
this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "異常退出\r\n");spa
socketList.Remove(pSocket);
pSocket.Shutdown(SocketShutdown.Both);
pSocket.Close();
return;
}
}線程
在While循環中加入:調試
switch (receiveStr)
{
case "MEAS:VOLTage:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
break;
case "MEAS:CURR:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
break;
default:
break;
}
模擬電源,當收到電壓查詢時,發送16~25中隨機數,因爲電源是三個通道的,所以發送三個隨機數,用逗號隔開。一樣收到電流查詢,發送2~5之間的隨機數。
完整的客戶端源碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
addTextDelegate = new AddTextDelegate(AddText);
}
private AddTextDelegate addTextDelegate;
private List<Socket> socketList = new List<Socket>();
public delegate void AddTextDelegate(string text);
private void AddText(string text)
{
txtLog.Text += text;
}
Random r = new Random();
private void btnStart_Click(object sender, EventArgs e)
{
//參數:尋址方式 傳輸數據方式 通訊協議
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress iPAddress = IPAddress.Parse(txtIP.Text);
//建立EndPoint
IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, int.Parse(txtPort.Text));
//綁定端口
socket.Bind(iPEndPoint);
//開啓偵聽
socket.Listen(10);
txtLog.Text += "服務啓動開啓偵聽……\r\n";
Thread thread = new Thread((s) =>
{
Socket serSocket = (Socket)s;
while (true)//不斷接收客戶端鏈接
{
this.Invoke(addTextDelegate, "服務正在等待客戶端鏈接……\r\n");
//開始接收客戶端的鏈接
//阻塞當前線程,等待客戶端鏈接
//客戶端鏈接上以後,服務端自動生成一個socket和鏈接的客端通訊
Socket proxSocket = serSocket.Accept();
this.Invoke(addTextDelegate, "客戶端鏈接成功!\r\n" + proxSocket.RemoteEndPoint.ToString());
//proxSocket.Send(Encoding.Default.GetBytes("鏈接成功!"));
socketList.Add(proxSocket);//當前通訊的socket放到集合中
new Thread(p =>
{
Socket pSocket = (Socket)p;
while (true)
{
try
{
byte[] bufferDate = new byte[1024];
int realLen = pSocket.Receive(bufferDate);
if (realLen <= 0)
{
this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "退出\r\n");
socketList.Remove(pSocket);
//客戶端退出的時候會發送一個空字節
pSocket.Shutdown(SocketShutdown.Both);
pSocket.Close();
return;
}
string receiveStr = Encoding.Default.GetString(bufferDate, 0, realLen);
switch (receiveStr)
{
case "MEAS:VOLTage:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
break;
case "MEAS:CURR:ALL?\n":
proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
break;
default:
break;
}
this.Invoke(addTextDelegate, receiveStr + "from" + pSocket.RemoteEndPoint.ToString() + "\r\n");
}
catch (Exception ex)
{
this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "異常退出\r\n");
socketList.Remove(pSocket);
pSocket.Shutdown(SocketShutdown.Both);
pSocket.Close();
return;
}
}
})
{ IsBackground = true }.Start(proxSocket);
}
});
thread.IsBackground = true;
thread.Start(socket);
}
private void btnSend_Click(object sender, EventArgs e)
{
string str = txtSend.Text;
byte[] data = Encoding.Default.GetBytes(str);
foreach (var socket in socketList)
{
if (socket != null && socket.Connected)
{
socket.Send(data);
}
}
}
}
上位機實現客戶端功能。具體以下:
一、字段和屬性
public readonly IPEndPoint TagetIPEP;
public bool IsConnected { get; set; } = false;
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { /*ReceiveTimeout=1000,SendTimeout=1000*/};
private Thread recListenThread;
public string ReceiveStr { get; set; }
public byte[] ReceiveByte { get; set; }
TagetIPEP是服務器地址和端口。
IsConnected是鏈接的狀態,這個比較重要,在發送和接收時,都要更加IsConnected進行,並更新IsConnected。
Socket用於和客戶端通信。
recListenThread是監聽客戶端消息的線程。
ReceiveStr和ReceiveByte用來存儲客戶端發來的消息。
二、方法函數
鏈接方法:
public bool Connect()
{
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
//ReceiveTimeout = 1000,
//SendTimeout=1000
};
//IAsyncResult connResult = socket.BeginConnect(TagetIPEP.Address, TagetIPEP.Port, null, null);
//connResult.AsyncWaitHandle.WaitOne(5000, true);
//if (connResult.IsCompleted)
//{
socket.Connect(TagetIPEP.Address, TagetIPEP.Port);
IsConnected = true;
//開啓接收監聽
recListenThread = new Thread(() =>
{
while (true)
{
try
{
ReceiveByte = new byte[1024];
int realLen = socket.Receive(ReceiveByte);
ReceiveStr = Encoding.Default.GetString(ReceiveByte, 0, realLen);
ReceiveEvent();
if (realLen <= 0)
{
if (socket != null && socket.Connected)
{
//服務器退出
IsConnected = false;
Log.WriteLog("服務器退出!");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
MessageBox.Show("鏈接斷開!");
}
return;
}
}
catch (Exception ex)
{
if (socket != null && socket.Connected)
{
IsConnected = false;
Log.WriteLog("服務器異常退出!", ex);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
return;
}
}
})
{ IsBackground = true };
recListenThread.Start();
return true;
//}
}
catch (Exception ex)
{
Log.WriteLog(TagetIPEP.Address + "鏈接失敗", ex);
}
return false;
}
鏈接函數返回值爲bool類型,根據返回值判斷鏈接是否成功鏈接。這裏每次鏈接都實例化了一個socket,由於在執行socket.close()後,從新打開會失敗,而斷線重連會常常用到,沒有找到更好的方法,乾脆從新實例化socket。鏈接成功後,開啓監聽服務端消息的線程。這裏使用了一個ReceiveEvent()事件,在接收到消息時會觸發這個事件,刷新UI界面。
發送方法:
public bool Send(string msg)
{
byte[] sendMsg = Encoding.Default.GetBytes(msg);
if (sendMsg.Length > 0&&IsConnected)
{
if (socket != null && socket.Connected)
{
try
{
socket.Send(sendMsg);
return true;
}
catch (Exception ex)
{
IsConnected = false;
Log.WriteLog("發送數據失敗,目標地址" + TagetIPEP.Address, ex);
}
}
}
return false;
}
關閉方法:
public void Close()
{
if (socket != null && socket.Connected)
{
IsConnected = false;
recListenThread.Abort();
Log.WriteLog("關閉鏈接!");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
在出現異常時調用
消息接收事件:
public event Action ReceiveEvent;
每次接收消息時觸發,獲取屬性ReceiveStr和ReceiveByte的值,刷新UI界面。
完整代碼:
public class TCPClient
{
public TCPClient(/*IPEndPoint localIPEP,*/IPEndPoint targetIPEP)
{
//socket.Bind(localIPEP);
TagetIPEP = targetIPEP;
}
public readonly IPEndPoint TagetIPEP;
public bool IsConnected { get; set; www.meiwanyule.cn} = false;
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { /*ReceiveTimeout=1000,SendTimeout=1000*/};
public bool Connect(www.sanxinyulevip.com)
{
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
//ReceiveTimeout = 1000,
//SendTimeout=1000
};
//IAsyncResult connResult = socket.BeginConnect(TagetIPEP.Address, TagetIPEP.Port, null, null);
//connResult.AsyncWaitHandle.WaitOne(5000, true);
//if (connResult.IsCompleted)
//{
socket.Connect(TagetIPEP.Address, TagetIPEP.Port);
IsConnected = true;
//開啓接收監聽
recListenThread = new Thread((www.yongshi123.cn) =>
{
while (true)
{
try
{
ReceiveByte = new byte[1024];
int realLen = socket.Receive(ReceiveByte);
ReceiveStr = Encoding.Default.GetString(ReceiveByte, 0, realLen);
ReceiveEvent();
if (realLen <= 0)
{
if (socket !=www.michenggw.com null && socket.Connected)
{
//服務器退出
IsConnected = false;
Log.WriteLog("服務器退出!");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
MessageBox.Show("鏈接斷開!");
}
return;
}
}
catch (Exception ex)
{
if (socket != null && socket.Connected)
{
IsConnected = false;
Log.WriteLog("服務器異常退出!", ex);
socket.Shutdown(SocketShutdown.Both);
socket.Close(www.suoLaieyuLe.com);
}
return;
}
}
})
{ IsBackground = true };
recListenThread.Start();
return true;
//}
}
catch (Exception ex)
{
Log.WriteLog(TagetIPEP.Address + "鏈接失敗", ex);
}
return false;
}
public bool Send(string msg)
{
byte[] sendMsg = Encoding.Default.GetBytes(msg);
if (sendMsg.Length > 0&&IsConnected)
{
if (socket != null && socket.Connected)
{
try
{
socket.Send(www.xintiandiyule1.com sendMsg);
return true;
}
catch (www.ysyl157.com Exception ex)
{
IsConnected = false;
Log.WriteLog("發送數據失敗,目標地址" + TagetIPEP.Address, ex);
}
}
}
return false;
}
public event Action ReceiveEvent;
public string ReceiveStr { get; set; }
public byte[] ReceiveByte {www.dasheng178.com get; set; }
public void Close()
{
if (socket != null && socket.Connected)
{
IsConnected = false;
recListenThread.Abort();
Log.WriteLog("關閉鏈接!");
socket.Shutdown(SocketShutdown.Both);
socket.Close(www.qcaphb.com/);
}
}
private Thread recListenThread;
}
前臺調用,聲明Timer定時器,每一個一秒觸發一次。觸發事件以下:
private string flag = "";
private void QueryTimer_Elapsed(object sender,www.ysgj1688.com System.Timers.ElapsedEventArgs e)
{
Now = DateTime.Now;
if (!tcp.Send("MEAS:VOLTage:ALL?\n"))
{
queryTimer.Enabled =www.baishenyvip.com false;
StartContent = "開始";
ConnContent = "鏈接";
tcp.IsConnected = false;
MessageBox.Show("查詢失敗!");
return;
}
flag = "V";
Thread.Sleep(50);
if (!tcp.Send("MEAS:CURR:ALL?\n"))
{
queryTimer.Enabled www.365soke.com= false;
StartContent = "開始";
ConnContent = "鏈接";
tcp.IsConnected = false;
MessageBox.Show("查詢失敗!");
return;
}
flag = "C";
#region 測試
//angle += 18;
//if (angle > 360)
//{
// angle = 18;
//}
#endregion
}
刷新UI界面的事件以下:
private void Tcp_ReceiveEvent()
{
Task.Run(() =>
{
Application.Current.Dispatcher.Invoke((www.fengshen157.com) =>
{
RemoteIP = tcp.TagetIPEP.ToString();
switch (flag)
{
case "V":
VoltValue = Math.Round(Convert.ToDouble(tcp.ReceiveStr.Split(',')[0]), 3);
break;
case "C":
CurrentValue = Math.Round(Convert.ToDouble(tcp.ReceiveStr.Split(',')[0]), 3);
break;
default:
break;
}
#region 測試
//VoltValue = Math.Round(Math.Sin((angle) * pi / 180) * 16 + 16, 3);
//CurrentValue =www.yongshiyule178.com Math.Round(Math.Sin((angle) * pi / 180) * 2.5 + 2.5, 3);
#endregion
VoltValues.Add(VoltValue);
CurrentValues.Add(CurrentValue);
if (VoltValues.Count > 30) { VoltValues.RemoveAt(0); CurrentValues.RemoveAt(0); } }); }); }