using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dmc1000;
using ClosedXML.Excel;
namespace ReaderCode
{
public partial class MainForm2 : Form
{
public MainForm2()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
public string NumID;
/// <summary>
/// 执行网络收发和数据处理的两个线程
/// </summary>
Thread thread0, thread1;
/// <summary>
/// 辅助收发网络接口
/// </summary>
Socket socketSR;
/// <summary>
/// 验证完成的数据文件路径
/// </summary>
string filepath = @"" + DateTime.Now.ToLongDateString().ToString() + ".xlsx";
/// <summary>
/// 实例化数据检查对象
/// </summary>
DataCheak dataCheak = new DataCheak();
/// <summary>
/// 阵列用面板引用
/// </summary>
Control FlowPanelControl;
/// <summary>
/// 超时计数用int
/// </summary>
int outTimeCOUNT = 0;
/// <summary>
/// 连续错误检测
/// </summary>
int contiErrorCOUNT = 0;
/// <summary>
/// 窗口加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm2_Load(object sender, EventArgs e)
{
//初始化存放数据的列表
dataCheak.ExcelStringArray = new List<string>();
dataCheak.CheckedErronStringArray = new List<string>();
dataCheak.CheckedStringArray = new List<string>();
//按钮状态失能
//初始化运动控制卡
RunControl.InitDMC1000b();
//检查保存数据的Excel文件
checkCsvFile();
//尝试转换连接的Socket到本from
FlowPanelControl = GetControl(flowLayoutPanel1.Name);
try //导入连接好的Socket
{
if (this.Tag != null)
{
socketSR = (Socket)this.Tag;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//开启后台线程监听读码器返回数据
thread0 = new Thread(SetReader.Recvie1);
thread0.IsBackground = true;
thread0.Start(socketSR);
//开启后台线程监听读码数据
thread1 = new Thread(LIsentQRInfotext);
thread1.IsBackground = true;
thread1.Start();
}
/// <summary>
/// 打开激光按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (OpenAMbutton1.Text == "打开激光")
{
SetReader.Sendcommand(socketSR, "AMON");
OpenAMbutton1.Text = "关闭激光";
}
else
{
SetReader.Sendcommand(socketSR, "AMOFF");
OpenAMbutton1.Text = "打开激光";
}
}
/// <summary>
/// 归零按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToZerobutton1_Click(object sender, EventArgs e)
{
RunControl.ZeroFunction(0, 1);
}
/// <summary>
/// 扫码位置按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Locationbutton1_Click(object sender, EventArgs e)
{
LocationForm locationForm = new LocationForm();
locationForm.Tag = this;
locationForm.ShowDialog();
}
/// <summary>
/// 检查数据文件是否存在或创建
/// </summary>
private void checkCsvFile()
{
if (!File.Exists(filepath))
{
}
}
/// <summary>
/// 窗口关闭时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
/// <summary>
/// 开始读码按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartRunbutton1_Click(object sender, EventArgs e)
{
dataCheak.ExcelStringArray = dataCheak.GetExcelDataByNumber(NumIDtextBox1.Text.Trim());
if (dataCheak.ExcelStringArray == null)
{
MessageBox.Show("读取测试给的数据失败,检查输入的号码和文件","提示");
return;
}
statusLabel.Text = "运行中";
if (RunControl.StepID == 1)
{
dataCheak.CheckedErronStringArray.Clear();
dataCheak.CheckedStringArray.Clear();
}
Thread thread = new Thread(RunControl.AutoReadQR);
thread.IsBackground = true;
thread.Start(socketSR);
}
/// <summary>
/// 根据查找名称查找控件
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
Control GetControl(string name)
{
foreach (Control item in this.Controls)
{
if (name == item.Name)
{
return item;
}
}
return null;
}
/// <summary>
/// 开新线程处理自动运行需要的步骤 1比对2写入3错误红色4超时黄色
/// </summary>
void LIsentQRInfotext()
{
while (true)
{
//开启计时
if (RunControl.startREADSingle)
{
OutTimer.Start();
}
if (outTimeCOUNT >= 8) //如果超时 找出该图框 变黄
{
//错误一次将标志+1
contiErrorCOUNT++;
foreach (Control item in FlowPanelControl.Controls)
{
if (item.Name == RunControl.StepID.ToString())
{
if (item == null)
{
MessageBox.Show("阵列框异常");
}
if (item is Button)
{
item.BackColor = Color.Yellow;
}
}
}
//计时关闭
OutTimer.Stop();
//超时计数归零
outTimeCOUNT = 0;
//完成一步
RunControl.StepID += 1;
//继续运行
RunControl.RUNsingle = true;
}
if (QRInfo.Text != RunControl.QRinfo)
{
//计时清零关闭
OutTimer.Stop();
//超时计数归零
outTimeCOUNT = 0;
//Label字符串同步
QRInfo.Text = RunControl.QRinfo;
//检查是否包含 (是否正确) 为True将数据写入字符数组
if (dataCheak.CheckReadedInfo(RunControl.QRinfo))
{
//将连续错误标志位置0
contiErrorCOUNT = 0;
//写入字符 数组
dataCheak.CheckedStringArray.Add(RunControl.QRinfo);
//完成一步
RunControl.StepID += 1;
//继续运行
RunControl.RUNsingle = true;
}
else //如果不包含 找出该图框 变红
{
//错误一次将标志+1
contiErrorCOUNT++;
foreach (Control item in FlowPanelControl.Controls)
{
if (item.Name == RunControl.StepID.ToString())
{
if (item == null)
{
MessageBox.Show("阵列框异常");
}
if (item is Button)
{
item.BackColor = Color.Red;
}
}
}
//完成一步
RunControl.StepID += 1;
dataCheak.CheckedErronStringArray.Add(RunControl.QRinfo);
//继续运行
RunControl.RUNsingle = true;
}
}
//如果连续错误三次将结束自动运行
if (contiErrorCOUNT == 3)
{
Dmc1000.Dmc1000.d1000_decel_stop(RunControl.XAixs);
Dmc1000.Dmc1000.d1000_decel_stop(RunControl.YAixs);
//停止运行
RunControl.RUNsingle = false;
MessageBox.Show("连续错误,请检查文件是否正确");
}
}
}
/// <summary>
/// 嘀嗒计时,检查是否超时事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OutTimer_Tick(object sender, EventArgs e)
{
outTimeCOUNT++;
if (outTimeCOUNT >= 10)
{
RunControl.RUNsingle = true;
outTimeCOUNT = 0;
}
}
/// <summary>
/// 停止运行按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopAutoButton_Click(object sender, EventArgs e)
{
Dmc1000.Dmc1000.d1000_decel_stop(RunControl.XAixs);
Dmc1000.Dmc1000.d1000_decel_stop(RunControl.YAixs);
RunControl.StepID = 1;
RunControl.StopSingle = true;
statusLabel.Text = "停止中";
}
private void OpenOkListButton_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 程序退出时````
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Controls.Clear();
//while (this.Controls.Count>0)
//{
// this.Controls.RemoveAt(0);
//}
thread0 = null;
thread1 = null;
}
}
}
新手小白编写 设备逻辑控制
最新推荐文章于 2024-11-14 17:50:52 发布
该代码实现了一个C#窗体应用程序,用于接收和处理网络数据,使用了多线程分别进行数据监听和读取。程序还涉及到了数据存储到Excel文件,以及对数据的验证和错误检查功能。同时,它与DMC1000运动控制卡进行交互,实现了激光开关和位置控制。
摘要由CSDN通过智能技术生成