c#窗体应用程序实现当软件开启,就在数据库扫描是否有设置的良品和不良品条码的当天ICT测试记录(ICT_test表中Barcode,Date,Result),良品和不良品条码在本地文件夹txt文件中获取。如果数据库中有良品和不良品测试记录存在,lable状态栏变绿并提示“已点检,请进行ICT测试”,然后进行别的条码的ICT测试。就将扫到的条码通过模拟键盘发送到ICT测试软件进行测试。如果没有,别的条码发送时会被拦截,label状态栏变红,并提示“请进行测试前点检”,然后就先进行设置条码的ICT测试,判断扫描到txt_Barcode中条码是否为获取到设置的良品或不良品条码,如输入的条码不符合,label状态栏变红,并提示“条码错误”,如输入的条码正确并为设置的良品条码,label状态栏变绿,并提示“请进行良品点检”,就将良品条码发送到ICT测试软件进行ICT测试,当发现某一文件夹中有良品测试报告生成,表示良品测试完成,测试完成后就提示“良品点检完成”。如扫描的是设置的不良品条码,则label状态栏变绿,并提示“请进行不良品点检”,进行ICT测试。当发现某一文件夹中有不良品测试报告生成,表示不良品测试完成。(所有条码先扫描到c#窗体软件txt_barcode中,在通过模拟键盘定位发送到ICT测试软件条码框中,然后进行ICT测试)代码实现
时间: 2023-11-27 19:05:57 浏览: 178
以下是一个简单的代码实现,仅供参考:
```csharp
using System;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace ICTTest
{
public partial class Form1 : Form
{
private string connectionString = "Data Source=.;Initial Catalog=ICT;Integrated Security=True";
private string goodBarcodesFile = "good_barcodes.txt";
private string badBarcodesFile = "bad_barcodes.txt";
private string testFolder = "test_reports";
private string goodTestFile = "good_test.txt";
private string badTestFile = "bad_test.txt";
private bool isGoodTestComplete = false;
private bool isBadTestComplete = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM ICT_test WHERE Date=@date AND Barcode IN (SELECT Barcode FROM good_barcodes UNION SELECT Barcode FROM bad_barcodes)", connection);
command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString());
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "已点检,请进行ICT测试";
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "请进行测试前点检";
txtBarcode.Focus();
}
connection.Close();
}
private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
string barcode = txtBarcode.Text.Trim();
if (lblStatus.Text == "请进行测试前点检")
{
if (CheckBarcode(barcode))
{
if (IsGoodBarcode(barcode))
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "请进行良品点检";
SendBarcodeToICT(barcode);
}
else if (IsBadBarcode(barcode))
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "请进行不良品点检";
SendBarcodeToICT(barcode);
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "条码错误";
}
}
}
else if (lblStatus.Text == "请进行良品点检")
{
if (CheckBarcode(barcode) && IsGoodBarcode(barcode))
{
SendBarcodeToICT(barcode);
if (IsGoodTestComplete())
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "良品点检完成";
}
}
}
else if (lblStatus.Text == "请进行不良品点检")
{
if (CheckBarcode(barcode) && IsBadBarcode(barcode))
{
SendBarcodeToICT(barcode);
if (IsBadTestComplete())
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "不良品点检完成";
}
}
}
txtBarcode.Text = "";
txtBarcode.Focus();
}
}
private bool CheckBarcode(string barcode)
{
// 检查条码是否符合要求,比如长度、字符集等
return true;
}
private bool IsGoodBarcode(string barcode)
{
string[] goodBarcodes = File.ReadAllLines(goodBarcodesFile);
foreach (string goodBarcode in goodBarcodes)
{
if (barcode == goodBarcode)
{
return true;
}
}
return false;
}
private bool IsBadBarcode(string barcode)
{
string[] badBarcodes = File.ReadAllLines(badBarcodesFile);
foreach (string badBarcode in badBarcodes)
{
if (barcode == badBarcode)
{
return true;
}
}
return false;
}
private void SendBarcodeToICT(string barcode)
{
// 使用模拟键盘发送条码到ICT测试软件
}
private bool IsGoodTestComplete()
{
if (isGoodTestComplete)
{
return true;
}
string[] testFiles = Directory.GetFiles(testFolder);
foreach (string testFile in testFiles)
{
if (testFile.EndsWith(goodTestFile))
{
isGoodTestComplete = true;
return true;
}
}
return false;
}
private bool IsBadTestComplete()
{
if (isBadTestComplete)
{
return true;
}
string[] testFiles = Directory.GetFiles(testFolder);
foreach (string testFile in testFiles)
{
if (testFile.EndsWith(badTestFile))
{
isBadTestComplete = true;
return true;
}
}
return false;
}
}
}
```
需要注意的是,代码中涉及到的文件路径、数据库连接字符串等需要根据实际情况进行修改。另外,模拟键盘发送条码需要使用第三方库或API,需要进行额外的研究和开发。
阅读全文