12,162
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace AcquisitonWebService
{
/// <summary>
/// AcWebSvr 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class AcWebSvr : System.Web.Services.WebService
{
//建立连接
[WebMethod]
public string Connect()
{
//声明一个socket对象
Socket newClient;
byte[] outData = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
//创建套接字
IPEndPoint addIe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 502);
//实例化newClient对象,初始化
newClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//设置receive超时时间
newClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, -100);
try
{
//进行连接
newClient.Connect(addIe);
}
catch (SocketException e)
{
return e.Message;
}
//发送指令
newClient.Send(outData);
//读取数据
string dataStr = null;
//while (dataStr == null)
//{
try
{
byte[] inData = new byte[1024];
//接受数据
newClient.Receive(inData, 0, 1000, SocketFlags.None);
int length = inData[5];
byte[] dataShow = new byte[length + 6];
//将数据转移到dataShow中
for (int i = 0; i < length + 6; i++) { dataShow[i] = inData[i]; }
//二进制转化为对应的字符型
dataStr = BitConverter.ToString(dataShow);
}
catch (SocketException e)
{
return e.Message;
}
catch (IOException e)
{
return e.Message;
}
catch (Exception e)
{
return e.Message;
}
//}
return dataStr;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace AcquisitionClient
{
public partial class DataMonitoring : Form
{
//实例化温度泛型
private List<double> dataReceive = new List<double>(80);
//实例化湿度泛型
private List<double> dataReceive2 = new List<double>(80);
//实例化时间泛型
private List<double> dataTime = new List<double>(80);
//声明线程
private Thread dataAcquisitionThread;
//设定起始数为0
private int i = 0;
//声明webservice服务对象
DataWebAcquisition.AcWebSvr myDataAcquisition = new DataWebAcquisition.AcWebSvr();
//实例化对象
/// <summary>
/// 窗体 加载初始化
/// </summary>
public DataMonitoring()
{
InitializeComponent();
}
/// <summary>
/// 开始/结束监测数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataMonitorButton_Click(object sender, EventArgs e)
{
switch (DataMonitorButton.Text)
{
case "开始监测":
dataAcquisitionThread = new Thread(new ThreadStart(this.ChartDraw));
dataAcquisitionThread.Start();
DataMonitorButton.Text = "结束监测";
break;
case "结束监测":
if (dataAcquisitionThread.IsAlive) dataAcquisitionThread.Abort();
TemperatureChart.Series["温度"].Points.Clear();
humidityChart.Series["湿度"].Points.Clear();
dataReceive2.Clear();
dataReceive.Clear();
dataTime.Clear();
i = 0;
DataMonitorButton.Text = "开始监测";
break;
}
}
/// <summary>
/// 获取数据以及绘制图标
/// </summary>
public void ChartDraw()
{
while (true)
{
string msg = myDataAcquisition.Connect();
//DataReceiveTxt.AppendText(msg + "\r\n");
try
{
//加入到绘图Y轴泛型中
string[] msgList = msg.Split(new char[] { '-' });
dataReceive.Add(Convert.ToDouble(Convert.ToInt32(msgList[msgList.Length - 1], 16)));
dataReceive2.Add(Convert.ToDouble(Convert.ToInt32(msgList[msgList.Length - 3], 16)));
//加入到绘图X轴泛型中
double n = Convert.ToDouble(i);
n = Math.Round(n * 2, 2);
dataTime.Add(n);
//调用主线程invoke方法绘制chart图标
if (TemperatureChart.IsHandleCreated)
{
////实例化一个委托指向一个方法,兵通过委托对象使用该方法
//myDelegate myInvoke = new myDelegate(UpdateCpuChart);
//TemperatureChart.Invoke(myInvoke);
//建立匿名委托,指向某方法
this.Invoke((MethodInvoker)delegate () { UpdateCpuChart(); });
}
Thread.Sleep(200);
i = i + 1;
}
catch
{
MessageBox.Show(msg);
Thread.Sleep(5000);
}
}
}
/// <summary>
/// 更新chart表
/// </summary>
public void UpdateCpuChart()
{
TemperatureChart.Series["温度"].Points.AddXY(dataTime[i], dataReceive[i]);
humidityChart.Series["湿度"].Points.AddXY(dataTime[i], dataReceive2[i]);
}
/// <summary>
/// 暂停采集
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AcqusitionSuspendButton_Click(object sender, EventArgs e)
{
switch (AcquisitionSuspendButton.Text)
{
case "暂停":
if (dataAcquisitionThread.IsAlive)
dataAcquisitionThread.Suspend();
AcquisitionSuspendButton.Text = "重连";
break;
case "重连":
dataAcquisitionThread.Resume();
AcquisitionSuspendButton.Text = "暂停";
break;
}
}
/// <summary>
/// 窗体退出时,进行的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataMonitoring_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(0);
}
}
}
//接受数据
newClient.Receive(inData, 0, 1000, SocketFlags.None);