Files
MES_PLC_Control/WindowsFormsApp1/form/ScanForm.cs
T

96 lines
2.7 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.server;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp1
{
public partial class ScanForm : Form
{
// 自定义属性,用于存储返回值
public string ReturnValue { get; set; }
public ScanForm()
{
InitializeComponent();
Init.ScanPort.DataReceived += SerialPort_DataReceived;
LOt号.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ReturnValue))
{
MessageBox.Show("扫码为空,请确认!");
return;
}
this.DialogResult = DialogResult.OK;
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(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (!Init.ScanPort.IsOpen)
{
Init.ScanPort?.Open();
}
// 读取串口接收到的数据
ReturnValue = Init.ScanPort.ReadExisting();
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
LOt号.Text = ReturnValue;
LOt号.Visible = true;
}));
}
else
{
LOt号.Text = ReturnValue;
LOt号.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show($"读取数据时出错: {ex.Message}");
}
}
private void ScanForm_FormClosing(object sender, FormClosingEventArgs e)
{
FormHelp.RemoveAllEventHandlers(Init.ScanPort, "DataReceived");
}
}
}