赞
踩
目录
- using SimpleTCP;
- using SqlSugar;
- using System.Text;
-
- namespace 自动化上位机设计
- {
- public partial class Form1 : Form
- {
-
-
- SqlHelper sqlHelper = new SqlHelper();
- SqlSugarClient dbContent = null;
- bool IsRun = false;
- int Count = 0;
-
- //数据收集对象
- EntryClass Datas = new EntryClass();
-
- //网络通讯客户端:长度测试仪器
- SimpleTcpClient client = new SimpleTcpClient();
-
- public Form1()
- {
- InitializeComponent();
-
- dbContent = sqlHelper.db;
-
- timer1.Enabled = true;
- timer1.Tick += Timer1_Tick;
-
- Task.Run(() =>
- {
- //设置编码格式,默认是UTF8
- client.StringEncoder = System.Text.ASCIIEncoding.UTF8;
- //设置分隔符,默认是0x13
- client.Delimiter = Encoding.ASCII.GetBytes("\r")[0];
-
-
- //收到分割数据的事件,遇到分隔符就会触发事件
- client.DelimiterDataReceived += (sender, msg) =>
- {
- AddLog("DelimiterStr-" + DateTime.Now.ToString() + msg.MessageString);
- };
-
- //收到数据的事件,可以在这里实现自己的协议
- client.DataReceived += (sender, msg) =>
- {
- //字节数组
- AddLog("字节数组Data:" + BitConverter.ToString(msg.Data));
- //字符串消息
- AddLog("字符串消息RDataseivedStr:" + msg.MessageString);
-
- //记录长度数据
- Datas.Len = msg.MessageString;
- };
-
- client.Connect("127.0.0.1", 8899);
- });
- }
-
- private void Timer1_Tick(object? sender, EventArgs e)
- {
- this.Invoke(() =>
- {
- uiLedDisplay1.Text = DateTime.Now.ToString("HH:mm:ss");
- });
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- sqlHelper.CreateTable<EntryClass>();
-
- //开始批量插入数据
- Task.Run(() => { AddData(); });
- }
-
- private void AddData()
- {
-
- while (true)
- {
- if (!IsRun) continue;
-
-
- //Datas.Len = new Random().Next(200, 300).ToString();
- Datas.Width = new Random().Next(40, 300).ToString();
- Datas.Hight = new Random().Next(150, 250).ToString();
- Datas.R = new Random().Next(10, 360).ToString();
- Datas.Weit = new Random().Next(20, 30).ToString();
- Datas.ZY = new Random().Next(50, 90).ToString();
- Datas.DC = new Random().Next(40, 300).ToString();
- Datas.DR = new Random().Next(400, 600).ToString();
- Datas.YL = new Random().Next(28, 88).ToString();
- Datas.WD = new Random().Next(89, 900).ToString();
- Datas.SD = new Random().Next(234, 454).ToString();
- Datas.LJ = new Random().Next(67, 89).ToString();
-
- //没有测量长度则不增加数据
- if (string.IsNullOrEmpty(Datas.Len)) continue;
-
- //数据放入数据库中
- dbContent.Insertable(Datas).ExecuteCommand();
-
- this.Invoke(() =>
- {
- //同步展示参数
- CD.Text = Datas.Len;
- KD.Text = Datas.Width;
- GD.Text = Datas.Hight;
- JD.Text = Datas.R;
- ZL.Text = Datas.Weit;
- ZY.Text = Datas.ZY;
- DY.Text = Datas.DC;
- NZ.Text = Datas.DR;
- PRESS.Text = Datas.YL;
- CC.Text = Datas.WD;
- SD.Text = Datas.SD;
- LJJ.Text = Datas.LJ;
-
- //参数展示到集合中
- 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", "测试数据");
-
- //计数
- Count++;
- uiLedDisplay2.Text = Count.ToString();
-
- //打印日志
- RunLog.AppendText("设备运行中,数据已加入到数据库\r\n");
- ErorrLog.AppendText("设备运行中,数据已加入到数据库\r\n");
- Log.AppendText("设备运行中,数据已加入到数据库\r\n");
-
- });
-
- Thread.Sleep(2000);
- }
- }
-
- private void uiSwitch1_ValueChanged(object sender, bool value)
- {
- if (uiSwitch1.Active)
- {
- uiLight1.OnColor = Color.Green;
- IsRun = true;
- RunLog.AppendText("设备启动。。。\r\n");
- ErorrLog.AppendText("设备启动。。。\r\n");
- Log.AppendText("设备启动。。。\r\n");
- }
- else
- {
- uiLight1.OnColor = Color.Red;
- IsRun = false;
- RunLog.AppendText("设备停止。。。\r\n");
- ErorrLog.AppendText("设备停止。。。\r\n");
- Log.AppendText("设备停止。。。\r\n");
- }
- }
-
-
-
- private void AddLog(string msg)
- {
- this.Invoke(() =>
- {
- //打印日志
- RunLog.AppendText($"{msg}\r\n");
- ErorrLog.AppendText($"{msg}\r\n");
- Log.AppendText($"{msg}\r\n");
- });
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。