MES_PLC_Control/WindowsFormsApp1/MaterialScanForm.cs

238 lines
8.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using WindowsFormsApp1.db;
using WindowsFormsApp1.mod;
using WindowsFormsApp1.mod.server;
using WindowsFormsApp1.server;
namespace WindowsFormsApp1
{
public partial class MaterialScanForm : Form
{
private SQLiteHelper<t_materialscan> _dbHelper;
private List<t_materialscan> materials = new List<t_materialscan>();
private ApiClient _apiClient;
public MaterialScanForm()
{
InitializeComponent();
Init.ScanPort.DataReceived += SerialPort_DataReceived_CL;
_dbHelper = new SQLiteHelper<t_materialscan>();
_apiClient = new ApiClient();
LoadMaterials();
SetupControls();
}
private void LoadMaterials()
{
var result = _dbHelper.Query(m => m.isonline);
if (result.Success)
{
materials = result.Data;
}
RefreshDataGrid();
}
public void SerialPort_DataReceived_CL(object sender, SerialDataReceivedEventArgs e)
{
try
{
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();
txtScan.Text = barcode;
}
catch (Exception ex)
{
MessageBox.Show($"读取数据时出错: {ex.Message}");
}
}
private void BtnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtScan.Text) || txtScan.Text == "请扫描材料条码")
{
MessageBox.Show("请扫描材料条码!");
return;
}
string scanCode = txtScan.Text;
if (materials.Any(m => m.scancode == scanCode))
{
MessageBox.Show("该材料已经在列表中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtScan.Text = "请扫描材料条码";
txtScan.ForeColor = Color.Gray;
txtScan.Focus();
return;
}
// 调用接口
var OtherData = new otherdata();
OtherData.JS_SN = scanCode;
OtherData.locationindex = rbPosition1.Checked ? 1 : 2;
OtherData.opindex = 1;
var mesResult = _apiClient.CallFunctionByName("Glue_CheckOut", new plcdata(), OtherData);
if (mesResult.Mesresult == mesresult.FAIL)
{
MessageBox.Show("上线失败!", mesResult.Msg, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var material = new t_materialscan
{
scancode = scanCode,
position = rbPosition1.Checked ? "1" : "2",
description = txtDescription.Text == "请输入说明" ? "" : txtDescription.Text,
scantime = DateTime.Now,
isonline = true
};
var result = _dbHelper.Insert(material);
if (result.Success)
{
materials.Add(material);
RefreshDataGrid();
// 清空输入
txtScan.Text = "请扫描材料条码";
txtScan.ForeColor = Color.Gray;
txtDescription.Text = "请输入说明";
txtDescription.ForeColor = Color.Gray;
txtScan.Focus();
}
else
{
MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DgvMaterials_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
if (MessageBox.Show("确认要下线该材料吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
var material = materials[e.RowIndex];
// 调用接口
var OtherData = new otherdata();
OtherData.JS_SN = material.scancode;
OtherData.locationindex = int.Parse(material.position);
OtherData.opindex = 2;
var mesResult = _apiClient.CallFunctionByName("Glue_CheckOut", new plcdata(), OtherData);
if (mesResult.Mesresult == mesresult.FAIL)
{
MessageBox.Show("下线失败!", mesResult.Msg, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
material.isonline = false;
var result = _dbHelper.Update(material);
if (result.Success)
{
materials.RemoveAt(e.RowIndex);
RefreshDataGrid();
}
else
{
MessageBox.Show("下线失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void SetupControls()
{
// 设置文本框默认提示
SetupTextBoxHint(txtScan, "请扫描材料条码");
SetupTextBoxHint(txtDescription, "请输入说明");
// 绑定事件
btnAdd.Click += BtnAdd_Click;
dgvMaterials.CellClick += DgvMaterials_CellClick;
// 调整表格行高和样式
dgvMaterials.RowTemplate.Height = 60; // 增加行高
dgvMaterials.ColumnHeadersHeight = 60; // 相应调整表头高度
dgvMaterials.DefaultCellStyle.Font = new Font("微软雅黑", 14F);
dgvMaterials.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 14F);
dgvMaterials.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgvMaterials.AllowUserToAddRows = false;
dgvMaterials.AllowUserToDeleteRows = false;
dgvMaterials.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvMaterials.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgvMaterials.RowHeadersVisible = false;
}
private void SetupTextBoxHint(TextBox textBox, string hint)
{
textBox.Text = hint;
textBox.ForeColor = Color.Gray;
textBox.Enter += (s, e) => {
if (textBox.Text == hint)
{
textBox.Text = "";
textBox.ForeColor = Color.Black;
}
};
textBox.Leave += (s, e) => {
if (string.IsNullOrWhiteSpace(textBox.Text))
{
textBox.Text = hint;
textBox.ForeColor = Color.Gray;
}
};
}
private void RefreshDataGrid()
{
// 确保表格有列
if (dgvMaterials.Columns.Count == 0)
{
dgvMaterials.Columns.Add(colOffline);
dgvMaterials.Columns.Add(colScanCode);
dgvMaterials.Columns.Add(colPosition);
dgvMaterials.Columns.Add(colDescription);
dgvMaterials.Columns.Add(colTime);
// 调整列宽
colOffline.Width = 50;
colScanCode.Width = 400;
colPosition.Width = 50;
colDescription.Width = 80;
colTime.Width = 250;
}
dgvMaterials.Rows.Clear();
foreach (var material in materials)
{
dgvMaterials.Rows.Add("下线", material.scancode, material.position,
material.description, material.scantime.ToString("yyyy-MM-dd HH:mm:ss"));
}
// 确保所有行的高度都是60
foreach (DataGridViewRow row in dgvMaterials.Rows)
{
row.Height = 60;
}
}
}
}