zhougf 083c4ec3ac 3.25 测试
改端子扫码,产线页面,主页
2025-03-25 21:22:36 +08:00

279 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.db;
using WindowsFormsApp1.mod;
using WindowsFormsApp1.mod.database;
using WindowsFormsApp1.mod.server;
using WindowsFormsApp1.server;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp1
{
public partial class ScanForm_DZ : Form
{
public ApiClient ApiClient = new ApiClient();
public ScanForm_DZ()
{
InitializeComponent();
Init.ScanPort.DataReceived += SerialPort_DataReceived_DZ;
端子1.Text = "";
端子2.Text = "";
端子3.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
//Init.ScanPort.DataReceived -= SerialPort_DataReceived_DZ;
//if (string.IsNullOrEmpty(端子1.Text)
// || string.IsNullOrEmpty(端子2.Text)
// || string.IsNullOrEmpty(端子3.Text))
//{
// //MessageBox.Show($"有端子未扫描!");
// //return;
//}
//else
//{
// this.Close();
//}
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (请扫描条码.ForeColor == Color.Red)
{
请扫描条码.ForeColor = Color.RosyBrown;
}
else
{
请扫描条码.ForeColor = Color.Red;
}
}
public void SerialPort_DataReceived_DZ(object sender, SerialDataReceivedEventArgs e)
{
try
{
var PlcData = new plcdata();
if (!Init.ScanPort.IsOpen)
{
Init.ScanPort?.Open();
}
// 读取串口接收到的数据
var dataBuilder = new StringBuilder();
while(Init.ScanPort.BytesToRead > 0)
{
System.Threading.Thread.Sleep(100);
dataBuilder.Append(Init.ScanPort.ReadExisting());
}
var barcode = dataBuilder.ToString();
if (端子1.Text == barcode || 端子2.Text == barcode || 端子3.Text == barcode)
{
return;
}
SQLiteHelper<t_keyvalue> sQLiteHelper_key = new db.SQLiteHelper<t_keyvalue>();
var key_sqlresult = sQLiteHelper_key.QueryAll();
if (string.IsNullOrWhiteSpace(端子1.Text))
{
this.Invoke(new Action(() =>
{
端子1.Text = barcode;
}));
var result = ApiClient.CallFunctionByName("SN_FeedingCheck", PlcData, new otherdata() { JS_SN = barcode });
if (result.Success)
{
this.Invoke(new Action(() =>
{
端子1校验.Text = "校验OK";
}));
//继续调用接口
var result2 = ApiClient.CallFunctionByName("Link_Comp", PlcData, new otherdata() { JS_SN = barcode });
if (result2.Success)
{
this.Invoke(new Action(() =>
{
端子1结果.Text = "绑定OK";
button1.Visible = false;
}));
// 写数据到 主页的barcode 数据库改,缓存改
var txt = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode1");
txt.value = 端子1.Text;
var code = key_sqlresult.Data.First(x => x.key == txt.key);
code.value = 端子1.Text;
sQLiteHelper_key.Update(code);
}
else
{
this.Invoke(new Action(() =>
{
端子1结果.Text = "绑定NG";
}));
}
}
else
{
this.Invoke(new Action(() =>
{
端子1校验.Text = "校验NG";
}));
}
}
else if (string.IsNullOrWhiteSpace(端子2.Text))
{
this.Invoke(new Action(() =>
{
端子2.Text = barcode;
}));
var result = ApiClient.CallFunctionByName("SN_FeedingCheck", PlcData, new otherdata() { JS_SN = barcode });
if (result.Success)
{
this.Invoke(new Action(() =>
{
端子2校验.Text = "校验OK";
}));
//继续调用接口
var result2 = ApiClient.CallFunctionByName("Link_Comp", PlcData, new otherdata() { JS_SN = barcode });
if (result2.Success)
{
this.Invoke(new Action(() =>
{
端子2结果.Text = "绑定OK";
button2.Visible = false;
}));
// 写数据到 主页的barcode 数据库改,缓存改
var txt = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode2");
txt.value = 端子2.Text;
var code = key_sqlresult.Data.First(x => x.key == txt.key);
code.value = 端子2.Text;
sQLiteHelper_key.Update(code);
}
else
{
this.Invoke(new Action(() =>
{
端子2结果.Text = "绑定NG";
}));
}
}
else
{
this.Invoke(new Action(() =>
{
端子2校验.Text = "校验NG";
}));
}
}
else if (string.IsNullOrWhiteSpace(端子3.Text))
{
this.Invoke(new Action(() =>
{
端子3.Text = barcode;
}));
var result = ApiClient.CallFunctionByName("SN_FeedingCheck", PlcData, new otherdata() { JS_SN = barcode });
if (result.Success)
{
this.Invoke(new Action(() =>
{
端子3校验.Text = "校验OK";
}));
var result2 = ApiClient.CallFunctionByName("Link_Comp", PlcData, new otherdata() { JS_SN = barcode });
if (result2.Success)
{
this.Invoke(new Action(() =>
{
端子3结果.Text = "绑定OK";
button3.Visible = false;
}));
// 写数据到 主页的barcode 数据库改,缓存改
var txt = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode3");
txt.value = 端子3.Text;
var code = key_sqlresult.Data.First(x => x.key == txt.key);
code.value = 端子3.Text;
sQLiteHelper_key.Update(code);
}
else
{
this.Invoke(new Action(() =>
{
端子3结果.Text = "绑定NG";
}));
}
}
else
{
this.Invoke(new Action(() =>
{
端子3校验.Text = "校验NG";
}));
}
}
//if (this.InvokeRequired)
//{
// this.Invoke(new Action(() =>
// {
// 端子1.Text = ReturnValue;
// 端子1.Visible = true;
// }));
//}
//else
//{
// 端子1.Text = ReturnValue;
// 端子1.Visible = true;
//}
}
catch (Exception ex)
{
MessageBox.Show($"读取数据时出错: {ex.Message}");
}
}
private void button1_Click_1(object sender, EventArgs e)
{
端子1.Text = "";
}
private void button2_Click_1(object sender, EventArgs e)
{
端子2.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
端子3.Text = "";
}
private void ScanForm_DZ_FormClosed(object sender, FormClosedEventArgs e)
{
FormHelp.RemoveAllEventHandlers(Init.ScanPort, "DataReceived");
}
}
}