当前位置:   article > 正文

基于C#的简单http服务器,用于多台工控机数据的监控_winform监测多台设备信息

winform监测多台设备信息

      最近工作在公司有这么一个需求:在公司车间有多台电脑运行着视觉检测程序,我们在办公室的时候需要清楚车间程序的运行状态(比如NG率等等)。

需求分析:多台电脑的数据上传到一台电脑,那么需要一台服务器,上传的数据可以被多个人看到,最好是以网页的形式,谁都可以访问。这个貌似涉及到服务器(后台)、网站(前端),虽然自己平时的工作也算是大杂烩,但是涉及面比较广,还是挺有难度的。

最后得出的方案:在办公室使用一台电脑作为服务器,车间电脑使用http协议上传数据到服务器。然后再写一个简单的网页,访问服务器,返回JSON数据,显示出来。这个网站架设在服务器电脑上,使用Windows自带的IIS。


(一)、IIS设置

                   进入【控制面板】-【程序】,选中《打开或关闭Windows功能》


打开之后,选中其中两项(红色方框)




点击确定之后,等待几分钟,在C盘目录下生成inetpub文件夹,点击进去,有5个文件夹



等下写好的html文件就放在 wwwroot文件夹下,这时候可以在浏览器中输入自己的IP(或者localhost)来访问,,比如直接输入109.17.86.100回车即可出现网页

注意:第一步我们把网站架设好了,网站内容待会解释哈。


(二)服务器

http服务器可以使用Java编写,但是Windows下.NET平台编写很方便

        http服务器作用:保存上传的数据,提供网站访问(我监听了2个端口)

       直接贴代码:

  1. <span style="font-size:14px;">using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.IO;
  12. using System.Threading;
  13. namespace Vision_Server
  14. {
  15. public partial class Form1 : Form
  16. {
  17. String[] NGString=new String[39];
  18. String[] FailString = new String[39];
  19. String[] TimeString = new String[39];
  20. Thread uploadThread;
  21. Thread downloadThread;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. for (int i = 0; i < 39; i++)
  29. {
  30. NGString[i] = "*";
  31. FailString[i] = "*";
  32. TimeString[i] = "*";
  33. }
  34. for (int i = 1; i < 40; i++)
  35. {
  36. dataGridView_AAM.Rows.Add(i.ToString() + "#", "*", "*", "*", "*");
  37. }
  38. //上传数据服务
  39. uploadThread = new Thread(new ThreadStart(uploadPro));
  40. uploadThread.Start();
  41. //下载数据服务
  42. downloadThread = new Thread(new ThreadStart(downloadPro));
  43. downloadThread.Start();
  44. }
  45. private void uploadPro()
  46. {
  47. using (HttpListener listerner = new HttpListener())
  48. {
  49. listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
  50. listerner.Prefixes.Add("http://109.17.86.92:8080/server/");
  51. listerner.Start();
  52. while (true)
  53. {
  54. //等待请求连接
  55. //没有请求则GetContext处于阻塞状态
  56. HttpListenerContext ctx = listerner.GetContext();
  57. ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
  58. try
  59. {
  60. string pro = ctx.Request.QueryString["pro"];
  61. string line = ctx.Request.QueryString["line"];
  62. string NG = ctx.Request.QueryString["NG"];
  63. string fail = ctx.Request.QueryString["fail"];
  64. printLog(" pro=" + pro + " line=" + line + " NG=" + NG + " Fail=" + fail);
  65. if (pro == "AAM")
  66. {
  67. int lineIndex = int.Parse(line) - 1;
  68. dataGridView_AAM.Rows[lineIndex].Cells[1].Value = NG;
  69. dataGridView_AAM.Rows[lineIndex].Cells[2].Value = fail;
  70. dataGridView_AAM.Rows[lineIndex].Cells[3].Value = DateTime.Now.ToString();
  71. NGString[lineIndex] = NG;
  72. FailString[lineIndex] = fail;
  73. TimeString[lineIndex] = DateTime.Now.ToString();
  74. }
  75. //使用Writer输出http响应代码
  76. using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
  77. {
  78. writer.WriteLine("OK");
  79. //writer.Close();
  80. // ctx.Response.Close();
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. printLog(ex.Message);
  86. }
  87. }
  88. listerner.Stop();
  89. }
  90. }
  91. private void downloadPro()
  92. {
  93. using (HttpListener listerner = new HttpListener())
  94. {
  95. listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
  96. listerner.Prefixes.Add("http://109.17.86.92:8888/web/");
  97. listerner.Start();
  98. while (true)
  99. {
  100. //等待请求连接
  101. //没有请求则GetContext处于阻塞状态
  102. HttpListenerContext ctx = listerner.GetContext();
  103. ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
  104. try
  105. {
  106. int locationY = int.Parse(ctx.Request.QueryString["locationY"]);
  107. //使用Writer输出http响应代码
  108. using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
  109. {
  110. String retString = "";
  111. //返回AAM数据json
  112. retString += "{";
  113. retString += addMarks("AAM") + ":";
  114. retString += "[";
  115. for (int i = 1; i < 40; i++)
  116. {
  117. retString += "{";
  118. retString += addMarks("line") + ":" + addMarks(i.ToString()) + ",";
  119. retString += addMarks("pro") + ":" + addMarks("AAM") + ",";
  120. retString += addMarks("NG") + ":" + addMarks(NGString[i - 1]) + ",";
  121. retString += addMarks("fail") + ":" + addMarks(FailString[i - 1]) + ",";
  122. retString += addMarks("time") + ":" + addMarks(TimeString[i - 1]);
  123. retString += "}";
  124. if (i != 39)
  125. retString += ",";
  126. }
  127. retString += "]";
  128. retString += ",";
  129. retString += addMarks("locationY") + ":" + addMarks(locationY.ToString());
  130. retString += "}";
  131. writer.WriteLine(retString);
  132. writer.Close();
  133. ctx.Response.Close();
  134. }
  135. }
  136. catch(Exception ex)
  137. {
  138. printLog(ex.Message);
  139. }
  140. }
  141. listerner.Stop();
  142. }
  143. }
  144. private String addMarks(String s)
  145. {
  146. return "\"" + s + "\"";
  147. }
  148. private void printLog(String str)
  149. {
  150. CheckForIllegalCrossThreadCalls = false;
  151. if (textBox_log.Lines.Length > 30)
  152. {
  153. textBox_log.Text = "";
  154. }
  155. String time = DateTime.Now.ToShortTimeString();
  156. textBox_log.Text += time+">" + str + "\r\n";
  157. }
  158. private void button_exit_Click(object sender, EventArgs e)
  159. {
  160. uploadThread.Abort();
  161. downloadThread.Abort();
  162. Application.Exit();
  163. }
  164. }
  165. }</span><span style="font-weight: bold; font-size: 24px;">
  166. </span>
界面效果



(三)、web站点

上面的服务器界面只有一台电脑可以显示,为了可以一网页的形式共享,编写一个网页

网页定时刷新,拉去数据到表格中显示

上代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Vision监控</title>
  6. <style type="text/css">
  7. .go_no {
  8. background-color: #FF3;
  9. }
  10. #apDiv1 {
  11. position:absolute;
  12. width:200px;
  13. height:115px;
  14. z-index:1;
  15. left: 526px;
  16. top: 91px;
  17. }
  18. .mytitle {
  19. color: #0F0;
  20. }
  21. #AAMTable {
  22. color: #00F;
  23. }
  24. </style></head>
  25. <center>
  26. <body>
  27. <p><strong class="mytitle">AAM 复合化程序监控(自动刷新)</strong></p>
  28. <table id="AAMTable" border="1">
  29. <tr>
  30. <th width="50" align="center" scope="col">line</th>
  31. <th width="100" align="center" scope="col">GO-NO</th>
  32. <th width="50" align="center" scope="col">Fail</th>
  33. <th width="150" align="center" scope="col">time</th>
  34. </tr>
  35. <tr>
  36. <td align="center">1#</td>
  37. <td align="center" id="AAMTable"> </td>
  38. <td align="center" id="AAMTable"> </td>
  39. <td align="center" id="AAMTable"> </td>
  40. </tr>
  41. <tr>
  42. <td align="center">2#</td>
  43. <td align="center" id="AAMTable"> </td>
  44. <td align="center" id="AAMTable"> </td>
  45. <td align="center" id="AAMTable"> </td>
  46. </tr>
  47. <tr>
  48. <td align="center">3#</td>
  49. <td align="center" id="AAMTable"> </td>
  50. <td align="center" id="AAMTable"> </td>
  51. <td align="center" id="AAMTable"> </td>
  52. </tr>
  53. <tr>
  54. <td align="center">4#</td>
  55. <td align="center" id="AAMTable"> </td>
  56. <td align="center" id="AAMTable"> </td>
  57. <td align="center" id="AAMTable"> </td>
  58. </tr>
  59. <tr>
  60. <td align="center">5#</td>
  61. <td align="center" id="AAMTable"> </td>
  62. <td align="center" id="AAMTable"> </td>
  63. <td align="center" id="AAMTable"> </td>
  64. </tr>
  65. <tr>
  66. <td align="center">6#</td>
  67. <td align="center" id="AAMTable"> </td>
  68. <td align="center" id="AAMTable"> </td>
  69. <td align="center" id="AAMTable"> </td>
  70. </tr>
  71. <tr>
  72. <td align="center">7#</td>
  73. <td align="center" id="AAMTable"> </td>
  74. <td align="center" id="AAMTable"> </td>
  75. <td align="center" id="AAMTable"> </td>
  76. </tr>
  77. <tr>
  78. <td align="center">8#</td>
  79. <td align="center" id="AAMTable"> </td>
  80. <td align="center" id="AAMTable"> </td>
  81. <td align="center" id="AAMTable"> </td>
  82. </tr>
  83. <tr>
  84. <td align="center">9#</td>
  85. <td align="center" id="AAMTable"> </td>
  86. <td align="center" id="AAMTable"> </td>
  87. <td align="center" id="AAMTable"> </td>
  88. </tr>
  89. <tr>
  90. <td align="center">10#</td>
  91. <td align="center" id="AAMTable"> </td>
  92. <td align="center" id="AAMTable"> </td>
  93. <td align="center" id="AAMTable"> </td>
  94. </tr>
  95. <tr>
  96. <td align="center">11#</td>
  97. <td align="center" id="AAMTable"> </td>
  98. <td align="center" id="AAMTable"> </td>
  99. <td align="center" id="AAMTable"> </td>
  100. </tr>
  101. <tr>
  102. <td align="center">12#</td>
  103. <td align="center" id="AAMTable"> </td>
  104. <td align="center" id="AAMTable"> </td>
  105. <td align="center" id="AAMTable"> </td>
  106. </tr>
  107. <tr>
  108. <td align="center">13#</td>
  109. <td align="center" id="AAMTable"> </td>
  110. <td align="center" id="AAMTable"> </td>
  111. <td align="center" id="AAMTable"> </td>
  112. </tr>
  113. <tr>
  114. <td align="center">14#</td>
  115. <td align="center" id="AAMTable"> </td>
  116. <td align="center" id="AAMTable"> </td>
  117. <td align="center" id="AAMTable"> </td>
  118. </tr>
  119. <tr>
  120. <td align="center">15#</td>
  121. <td align="center" id="AAMTable"> </td>
  122. <td align="center" id="AAMTable"> </td>
  123. <td align="center" id="AAMTable"> </td>
  124. </tr>
  125. <tr>
  126. <td align="center">16#</td>
  127. <td align="center" id="AAMTable"> </td>
  128. <td align="center" id="AAMTable"> </td>
  129. <td align="center" id="AAMTable"> </td>
  130. </tr>
  131. <tr>
  132. <td align="center">17#</td>
  133. <td align="center" id="AAMTable"> </td>
  134. <td align="center" id="AAMTable"> </td>
  135. <td align="center" id="AAMTable"> </td>
  136. </tr>
  137. <tr>
  138. <td align="center">18#</td>
  139. <td align="center" id="AAMTable"> </td>
  140. <td align="center" id="AAMTable"> </td>
  141. <td align="center" id="AAMTable"> </td>
  142. </tr>
  143. <tr>
  144. <td align="center">19#</td>
  145. <td align="center" id="AAMTable"> </td>
  146. <td align="center" id="AAMTable"> </td>
  147. <td align="center" id="AAMTable"> </td>
  148. </tr>
  149. <tr>
  150. <td align="center">20#</td>
  151. <td align="center" id="AAMTable"> </td>
  152. <td align="center" id="AAMTable"> </td>
  153. <td align="center" id="AAMTable"> </td>
  154. </tr>
  155. <tr>
  156. <td align="center">21#</td>
  157. <td align="center" id="AAMTable"> </td>
  158. <td align="center" id="AAMTable"> </td>
  159. <td align="center" id="AAMTable"> </td>
  160. </tr>
  161. <tr>
  162. <td align="center">22#</td>
  163. <td align="center" id="AAMTable"> </td>
  164. <td align="center" id="AAMTable"> </td>
  165. <td align="center" id="AAMTable"> </td>
  166. </tr>
  167. <tr>
  168. <td align="center">23#</td>
  169. <td align="center" id="AAMTable"> </td>
  170. <td align="center" id="AAMTable"> </td>
  171. <td align="center" id="AAMTable"> </td>
  172. </tr>
  173. <tr>
  174. <td align="center">24#</td>
  175. <td align="center" id="AAMTable"> </td>
  176. <td align="center" id="AAMTable"> </td>
  177. <td align="center" id="AAMTable"> </td>
  178. </tr>
  179. <tr>
  180. <td align="center">25#</td>
  181. <td align="center" id="AAMTable"> </td>
  182. <td align="center" id="AAMTable"> </td>
  183. <td align="center" id="AAMTable"> </td>
  184. </tr>
  185. <tr>
  186. <td align="center">26#</td>
  187. <td align="center" id="AAMTable"> </td>
  188. <td align="center" id="AAMTable"> </td>
  189. <td align="center" id="AAMTable"> </td>
  190. </tr>
  191. <tr>
  192. <td align="center">27#</td>
  193. <td align="center" id="AAMTable"> </td>
  194. <td align="center" id="AAMTable"> </td>
  195. <td align="center" id="AAMTable"> </td>
  196. </tr>
  197. <tr>
  198. <td align="center">28#</td>
  199. <td align="center" id="AAMTable"> </td>
  200. <td align="center" id="AAMTable"> </td>
  201. <td align="center" id="AAMTable"> </td>
  202. </tr>
  203. <tr>
  204. <td align="center">29#</td>
  205. <td align="center" id="AAMTable"> </td>
  206. <td align="center" id="AAMTable"> </td>
  207. <td align="center" id="AAMTable"> </td>
  208. </tr>
  209. <tr>
  210. <td align="center">30#</td>
  211. <td align="center" id="AAMTable"> </td>
  212. <td align="center" id="AAMTable"> </td>
  213. <td align="center" id="AAMTable"> </td>
  214. </tr>
  215. <tr>
  216. <td align="center">31#</td>
  217. <td align="center" id="AAMTable"> </td>
  218. <td align="center" id="AAMTable"> </td>
  219. <td align="center" id="AAMTable"> </td>
  220. </tr>
  221. <tr>
  222. <td align="center">32#</td>
  223. <td align="center" id="AAMTable"> </td>
  224. <td align="center" id="AAMTable"> </td>
  225. <td align="center" id="AAMTable"> </td>
  226. </tr>
  227. <tr>
  228. <td align="center">33#</td>
  229. <td align="center" id="AAMTable"> </td>
  230. <td align="center" id="AAMTable"> </td>
  231. <td align="center" id="AAMTable"> </td>
  232. </tr>
  233. <tr>
  234. <td align="center">34#</td>
  235. <td align="center" id="AAMTable"> </td>
  236. <td align="center" id="AAMTable"> </td>
  237. <td align="center" id="AAMTable"> </td>
  238. </tr>
  239. <tr>
  240. <td align="center">35#</td>
  241. <td align="center" id="AAMTable"> </td>
  242. <td align="center" id="AAMTable"> </td>
  243. <td align="center" id="AAMTable"> </td>
  244. </tr>
  245. <tr>
  246. <td align="center">36#</td>
  247. <td align="center" id="AAMTable"> </td>
  248. <td align="center" id="AAMTable"> </td>
  249. <td align="center" id="AAMTable"> </td>
  250. </tr>
  251. <tr>
  252. <td align="center">37#</td>
  253. <td align="center" id="AAMTable"> </td>
  254. <td align="center" id="AAMTable"> </td>
  255. <td align="center" id="AAMTable"> </td>
  256. </tr>
  257. <tr>
  258. <td align="center">38#</td>
  259. <td align="center" id="AAMTable"> </td>
  260. <td align="center" id="AAMTable"> </td>
  261. <td align="center" id="AAMTable"> </td>
  262. </tr>
  263. <tr>
  264. <td align="center">39#</td>
  265. <td align="center" id="AAMTable"> </td>
  266. <td align="center" id="AAMTable"> </td>
  267. <td align="center" id="AAMTable"> </td>
  268. </tr>
  269. </table>
  270. <p> </p>
  271. </body>
  272. </center>
  273. </html>
  274. <script type=text/javascript>
  275. var locationY=50;
  276. function timedGetText(){
  277. locationY=document.getElementById("AAMTable").offsetTop;
  278. var url="http://109.17.86.92:8888/web/?locationY=" + locationY; //服务器地址
  279. var time=400;
  280. var request = new XMLHttpRequest();
  281. var timeout = false;
  282. //访问限时,限时长为200ms
  283. var timer = setTimeout( function(){
  284. timeout = true;
  285. request.abort();
  286. }, time );
  287. request.open( "GET", url );
  288. request.onreadystatechange = function(){
  289. if( request.readyState !== 4 ) return;
  290. if( timeout ) return;
  291. clearTimeout( timer );
  292. if( request.status === 200 ){
  293. //数据返回成功
  294. var obj =JSON.parse(request.responseText ); //将JSON字符串转换成JSON对象
  295. for(var i=0;i<39;i++)
  296. {
  297. var fail=obj.AAM[i].fail;
  298. var NG=obj.AAM[i].NG;
  299. var time=obj.AAM[i].time;
  300. //表格操作
  301. document.getElementById("AAMTable").rows[i+1].cells[1].innerHTML=NG;
  302. document.getElementById("AAMTable").rows[i+1].cells[2].innerHTML=fail;
  303. document.getElementById("AAMTable").rows[i+1].cells[3].innerHTML=time;
  304. }
  305. var retlocationY=obj.locationY;
  306. window.scrollBy(0,retlocationY);
  307. }
  308. }
  309. request.send( null );
  310. }
  311. timedGetText();
  312. function myrefresh()
  313. { //刷新页面
  314. window.location.reload();
  315. }
  316. setInterval('myrefresh()',2000); //定时刷新页面,2s一次
  317. </script>

网页效果:




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

闽ICP备14008679号