当前位置:   article > 正文

自动化设备上位机设计 四

自动化设备上位机设计 四

目录

一 设计原型

二 后台代码


一 设计原型

二 后台代码

  1. using SimpleTCP;
  2. using SqlSugar;
  3. using System.Text;
  4. namespace 自动化上位机设计
  5. {
  6. public partial class Form1 : Form
  7. {
  8. SqlHelper sqlHelper = new SqlHelper();
  9. SqlSugarClient dbContent = null;
  10. bool IsRun = false;
  11. int Count = 0;
  12. //数据收集对象
  13. EntryClass Datas = new EntryClass();
  14. //网络通讯客户端:长度测试仪器
  15. SimpleTcpClient client = new SimpleTcpClient();
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. dbContent = sqlHelper.db;
  20. timer1.Enabled = true;
  21. timer1.Tick += Timer1_Tick;
  22. Task.Run(() =>
  23. {
  24. //设置编码格式,默认是UTF8
  25. client.StringEncoder = System.Text.ASCIIEncoding.UTF8;
  26. //设置分隔符,默认是0x13
  27. client.Delimiter = Encoding.ASCII.GetBytes("\r")[0];
  28. //收到分割数据的事件,遇到分隔符就会触发事件
  29. client.DelimiterDataReceived += (sender, msg) =>
  30. {
  31. AddLog("DelimiterStr-" + DateTime.Now.ToString() + msg.MessageString);
  32. };
  33. //收到数据的事件,可以在这里实现自己的协议
  34. client.DataReceived += (sender, msg) =>
  35. {
  36. //字节数组
  37. AddLog("字节数组Data:" + BitConverter.ToString(msg.Data));
  38. //字符串消息
  39. AddLog("字符串消息RDataseivedStr:" + msg.MessageString);
  40. //记录长度数据
  41. Datas.Len = msg.MessageString;
  42. };
  43. client.Connect("127.0.0.1", 8899);
  44. });
  45. }
  46. private void Timer1_Tick(object? sender, EventArgs e)
  47. {
  48. this.Invoke(() =>
  49. {
  50. uiLedDisplay1.Text = DateTime.Now.ToString("HH:mm:ss");
  51. });
  52. }
  53. private void Form1_Load(object sender, EventArgs e)
  54. {
  55. sqlHelper.CreateTable<EntryClass>();
  56. //开始批量插入数据
  57. Task.Run(() => { AddData(); });
  58. }
  59. private void AddData()
  60. {
  61. while (true)
  62. {
  63. if (!IsRun) continue;
  64. //Datas.Len = new Random().Next(200, 300).ToString();
  65. Datas.Width = new Random().Next(40, 300).ToString();
  66. Datas.Hight = new Random().Next(150, 250).ToString();
  67. Datas.R = new Random().Next(10, 360).ToString();
  68. Datas.Weit = new Random().Next(20, 30).ToString();
  69. Datas.ZY = new Random().Next(50, 90).ToString();
  70. Datas.DC = new Random().Next(40, 300).ToString();
  71. Datas.DR = new Random().Next(400, 600).ToString();
  72. Datas.YL = new Random().Next(28, 88).ToString();
  73. Datas.WD = new Random().Next(89, 900).ToString();
  74. Datas.SD = new Random().Next(234, 454).ToString();
  75. Datas.LJ = new Random().Next(67, 89).ToString();
  76. //没有测量长度则不增加数据
  77. if (string.IsNullOrEmpty(Datas.Len)) continue;
  78. //数据放入数据库中
  79. dbContent.Insertable(Datas).ExecuteCommand();
  80. this.Invoke(() =>
  81. {
  82. //同步展示参数
  83. CD.Text = Datas.Len;
  84. KD.Text = Datas.Width;
  85. GD.Text = Datas.Hight;
  86. JD.Text = Datas.R;
  87. ZL.Text = Datas.Weit;
  88. ZY.Text = Datas.ZY;
  89. DY.Text = Datas.DC;
  90. NZ.Text = Datas.DR;
  91. PRESS.Text = Datas.YL;
  92. CC.Text = Datas.WD;
  93. SD.Text = Datas.SD;
  94. LJJ.Text = Datas.LJ;
  95. //参数展示到集合中
  96. Data.Rows.Add(Datas.Len, Datas.Width, Datas.Hight, Datas.R, Datas.Weit, Datas.ZY, Datas.DC, Datas.DR, Datas.YL, Datas.WD, Datas.SD, Datas.LJ, DateTime.Now.ToShortTimeString(), "OK", "测试数据");
  97. //计数
  98. Count++;
  99. uiLedDisplay2.Text = Count.ToString();
  100. //打印日志
  101. RunLog.AppendText("设备运行中,数据已加入到数据库\r\n");
  102. ErorrLog.AppendText("设备运行中,数据已加入到数据库\r\n");
  103. Log.AppendText("设备运行中,数据已加入到数据库\r\n");
  104. });
  105. Thread.Sleep(2000);
  106. }
  107. }
  108. private void uiSwitch1_ValueChanged(object sender, bool value)
  109. {
  110. if (uiSwitch1.Active)
  111. {
  112. uiLight1.OnColor = Color.Green;
  113. IsRun = true;
  114. RunLog.AppendText("设备启动。。。\r\n");
  115. ErorrLog.AppendText("设备启动。。。\r\n");
  116. Log.AppendText("设备启动。。。\r\n");
  117. }
  118. else
  119. {
  120. uiLight1.OnColor = Color.Red;
  121. IsRun = false;
  122. RunLog.AppendText("设备停止。。。\r\n");
  123. ErorrLog.AppendText("设备停止。。。\r\n");
  124. Log.AppendText("设备停止。。。\r\n");
  125. }
  126. }
  127. private void AddLog(string msg)
  128. {
  129. this.Invoke(() =>
  130. {
  131. //打印日志
  132. RunLog.AppendText($"{msg}\r\n");
  133. ErorrLog.AppendText($"{msg}\r\n");
  134. Log.AppendText($"{msg}\r\n");
  135. });
  136. }
  137. }
  138. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号