1064 lines
40 KiB
C#
1064 lines
40 KiB
C#
using HZH_Controls;
|
||
using OpenCvSharp;
|
||
using OpenCvSharp.Extensions;
|
||
using Sunny.UI.Win32;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Diagnostics.Eventing.Reader;
|
||
using System.Drawing;
|
||
using System.Drawing.Drawing2D;
|
||
using System.Drawing.Imaging;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Runtime.InteropServices;
|
||
using System.Runtime.Remoting.Messaging;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Forms;
|
||
using System.Windows.Media.Media3D;
|
||
using static System.Net.Mime.MediaTypeNames;
|
||
//using XKRS.Device.SimboVision.SimboHelper;
|
||
|
||
|
||
namespace YSDetection
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
public MainForm()
|
||
{
|
||
InitializeComponent();
|
||
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
|
||
Control.CheckForIllegalCrossThreadCalls = false;
|
||
|
||
}
|
||
int SnaptNum = 1;
|
||
HikCamera hikCamera = new HikCamera();
|
||
HikCamera hikCamera2 = new HikCamera();
|
||
SerialPortBase serialPortBase = new SerialPortBase();
|
||
SiemensPLCTCPDriver siemensPLCTCP = new SiemensPLCTCPDriver();
|
||
SimboObjectDetection simboObjectDetection = new SimboObjectDetection();
|
||
//PaddleOcrModel paddleOcrModel = new PaddleOcrModel();
|
||
string LablePath = "D:\\huarui\\txt\\class.txt";
|
||
AutoResetEvent autoResetEvent;
|
||
AutoResetEvent waitResetEvent;
|
||
private void MainForm_Load(object sender, EventArgs e)
|
||
{
|
||
autoResetEvent = new AutoResetEvent(false);
|
||
waitResetEvent=new AutoResetEvent(false);
|
||
siemensPLCTCP.Start();
|
||
|
||
string PLCAdress3 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress3, false);
|
||
string PLCAdress1 = "M12.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, false);
|
||
string PLCAdress2 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress2, false);
|
||
string PLCAdress4 = "M14.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress4, true);
|
||
|
||
SnaptPage.Text = SnaptNum.ToString();
|
||
DataSet ds = SQLiteHelper.Query($"select xinghao from lungu");
|
||
|
||
//cbx.DataSource = ds;
|
||
DataTableReader rdr = ds.CreateDataReader();
|
||
HashSet<string> values = new System.Collections.Generic.HashSet<string>();
|
||
while (rdr.Read())//读取表中数据
|
||
|
||
{
|
||
|
||
|
||
values.Add((string)rdr["xinghao"]);
|
||
}
|
||
foreach (var item in values)
|
||
{
|
||
comboBox1.Items.Add(item);
|
||
|
||
comboBox1.Text = item;
|
||
}
|
||
string Dectionstr = "D:\\huarui\\model\\best.onnx";
|
||
|
||
//string CountryStr = "D:\\Projects\\Documents\\XKOCR\\chinese.json";
|
||
if (File.Exists(Dectionstr))
|
||
{
|
||
LablePath = "D:\\huarui\\txt\\class.txt";//标签路径
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("检测" + Dectionstr + " " + "模型有无");
|
||
return;
|
||
}
|
||
//初始化检测驱动
|
||
simboObjectDetection.Load(Dectionstr, "CPU", "images", 640, 640);
|
||
|
||
//paddleOcrModel.Load(CountryStr, "CPU");
|
||
|
||
}
|
||
|
||
private void button5_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
int PageNum = 0;
|
||
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
||
{ if (textBox12.Text != "无")
|
||
{ DateTime dt2 = DateTime.Now;
|
||
FileStream baogao_path = new FileStream("D:\\huarui\\txt\\" + dt2.Year + "_"+dt2.Month + "_" + dt2.Day + "_" + dt2.Hour + "_" + dt2.Minute+ "_检测报告.txt", FileMode.Create);
|
||
StreamWriter baogao = new StreamWriter(baogao_path, Encoding.Default);
|
||
string jiancejieguo = "型号:" + textBox12.Text + " 总数: " + textBox13.Text + " 合格数:" + textBox14.Text + " 不合格数:" + textBox15.Text;
|
||
baogao.Write(jiancejieguo);
|
||
baogao.Close();
|
||
baogao_path.Close();
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
allnum = 0;
|
||
oknum = 0;
|
||
ngnum = 0;
|
||
textBox13.Text = "";
|
||
textBox14.Text = "";
|
||
textBox15.Text = "";
|
||
|
||
FileStream yuyan_path = new FileStream("D:\\huarui\\txt/xinghao.txt", FileMode.Create);
|
||
StreamWriter yuyan = new StreamWriter(yuyan_path, Encoding.Default);
|
||
yuyan.Write(comboBox1.Text);
|
||
yuyan.Close();
|
||
yuyan_path.Close();
|
||
string leixing = File.ReadAllText("D:\\huarui\\txt/xinghao.txt");
|
||
DataSet ds = SQLiteHelper.Query($"select * from lungu where xinghao='{leixing}'");
|
||
|
||
//cbx.DataSource = ds;
|
||
DataTableReader rdr = ds.CreateDataReader();
|
||
HashSet<string> values = new System.Collections.Generic.HashSet<string>();
|
||
while (rdr.Read())//读取表中数据
|
||
|
||
{
|
||
|
||
SnaptPage.Text = (string)rdr["chishu"];
|
||
textBox6.Text = (string)rdr["luoju"];
|
||
textBox1.Text = (string)rdr["luosishu"];
|
||
textBox3.Text = (string)rdr["luosichangdu"];
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
bool Ifreturndata = false;
|
||
static int LJDis = 0;
|
||
public void SerialPort_DataReceived(byte[] obj)
|
||
{
|
||
string msg = string.Empty;
|
||
//if (rbStringData.Checked)
|
||
// {
|
||
// msg = Encoding.Default.GetString(obj);
|
||
// }
|
||
// else if (rbHexData.Checked)//16进制
|
||
// {
|
||
//msg = ToHexStrFromByte(obj);
|
||
msg = BitConverter.ToString(obj).Replace('-', ' ');
|
||
// }
|
||
|
||
if (obj.Length == 9)
|
||
{
|
||
byte[] lastData2 = obj.Skip(3).Take(4).ToArray(); // 从第3位起读4位
|
||
// string x0 = obj[3] + obj[4] + obj[5] + obj[6];
|
||
string msg2 = ToHexStrFromByte(lastData2);
|
||
LJDis = HexaToDecimal(msg2);
|
||
FileStream changdu_path = new FileStream("D:\\huarui\\txt\\changdu.txt", FileMode.Create);
|
||
StreamWriter changdu = new StreamWriter(changdu_path, Encoding.Default);
|
||
changdu.Write(LJDis);
|
||
changdu.Close();
|
||
changdu_path.Close();
|
||
}
|
||
else
|
||
{
|
||
LJDis = 0;
|
||
//FileStream changdu_path = new FileStream("D:\\huarui\\txt\\changdu.txt", FileMode.Create);
|
||
//StreamWriter changdu = new StreamWriter(changdu_path, Encoding.Default);
|
||
//changdu.Write(LJDis);
|
||
//changdu.Close();
|
||
//changdu_path.Close();
|
||
}
|
||
Ifreturndata = true;
|
||
//if (tbMessage.InvokeRequired)
|
||
//{
|
||
// tbMessage.Invoke(new Action(() =>
|
||
// {
|
||
// ShowMessage(msg);
|
||
// }));
|
||
//}
|
||
//else
|
||
//{
|
||
// ShowMessage(msg);
|
||
//}
|
||
}
|
||
/// <summary>
|
||
/// 另一种16进制转10进制的处理方式,Multiplier参与*16的循环很巧妙,对Multiplier的处理很推荐,逻辑统一
|
||
/// </summary>
|
||
/// <param name="HexaDecimalString"></param>
|
||
/// <returns></returns>
|
||
public int HexaToDecimal(string HexaDecimalString)
|
||
{
|
||
int Decimal = 0;
|
||
int Multiplier = 1;
|
||
|
||
for (int i = HexaDecimalString.Length - 1; i >= 0; i--)
|
||
{
|
||
Decimal += HexaToDecimal(HexaDecimalString[i]) * Multiplier;
|
||
Multiplier *= 16;
|
||
}
|
||
return Decimal;
|
||
}
|
||
|
||
int HexaToDecimal(char c)
|
||
{
|
||
switch (c)
|
||
{
|
||
case '0':
|
||
return 0;
|
||
case '1':
|
||
return 1;
|
||
case '2':
|
||
return 2;
|
||
case '3':
|
||
return 3;
|
||
case '4':
|
||
return 4;
|
||
case '5':
|
||
return 5;
|
||
case '6':
|
||
return 6;
|
||
case '7':
|
||
return 7;
|
||
case '8':
|
||
return 8;
|
||
case '9':
|
||
return 9;
|
||
case 'A':
|
||
case 'a':
|
||
return 10;
|
||
case 'B':
|
||
case 'b':
|
||
return 11;
|
||
case 'C':
|
||
case 'c':
|
||
return 12;
|
||
case 'D':
|
||
case 'd':
|
||
return 13;
|
||
case 'E':
|
||
case 'e':
|
||
return 14;
|
||
case 'F':
|
||
case 'f':
|
||
return 15;
|
||
}
|
||
return -1;
|
||
}
|
||
/// <summary>
|
||
/// 字节数组转16进制字符串:空格分隔
|
||
/// </summary>
|
||
/// <param name="byteDatas"></param>
|
||
/// <returns></returns>
|
||
public string ToHexStrFromByte(byte[] byteDatas)
|
||
{
|
||
StringBuilder builder = new StringBuilder();
|
||
for (int i = 0; i < byteDatas.Length; i++)
|
||
{
|
||
builder.Append(string.Format("{0:X2}", byteDatas[i]));
|
||
}
|
||
return builder.ToString().Trim();
|
||
}
|
||
|
||
|
||
|
||
public void OnCameraMatOutPut(DateTime dt, Mat cameraMat, int SnapshotCount)
|
||
{
|
||
string fitImageFolder = "D:\\huarui\\pic//";
|
||
string fitImageFolder1 = "D:\\huarui\\pic//" + dt.Year + "_" + dt.Month + "_" + dt.Day+"//";
|
||
if (!Directory.Exists(fitImageFolder1))
|
||
{
|
||
Directory.CreateDirectory(fitImageFolder1);
|
||
}
|
||
|
||
cameraMat.ImWrite(fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second +"num "+allnum+"_1.jpg");
|
||
|
||
cameraMat.ImWrite(fitImageFolder + "1.jpg");
|
||
FileStream pic1_path = new FileStream("D:\\huarui\\txt\\pic1name.txt", FileMode.Create);
|
||
StreamWriter pic1 = new StreamWriter(pic1_path, Encoding.GetEncoding("gb2312"));
|
||
pic1.Write("Current product" + ":" + fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + "num " + allnum+ "_1.jpg");
|
||
pic1.Close();
|
||
pic1_path.Close();
|
||
|
||
|
||
#region 1.预处理
|
||
//Mat PretreatmentMat = new Mat();
|
||
//GetContoursPic(cameraMat, PretreatmentMat);
|
||
#endregion
|
||
|
||
#region 2.YOLO检测
|
||
|
||
|
||
//MLRequest req = new MLRequest();
|
||
//req.currentMat = cameraMat.Clone();
|
||
//req.ResizeWidth = 640;
|
||
//req.ResizeHeight = 640;
|
||
//req.Score = 0.3f;
|
||
//req.in_lable_path = LablePath;//标签路径
|
||
//req.confThreshold = 0.3f;//模型置信度
|
||
//req.iouThreshold = 0.4f;//检测IOU
|
||
//req.out_node_name = "output";
|
||
//MLResult mL = simboObjectDetection.RunInferenceFixed(req);
|
||
//Console.WriteLine(mL.ToString());
|
||
#endregion
|
||
#region 3.OCR识别
|
||
//MLRequest reqcut = new MLRequest();
|
||
//reqcut.currentMat = cameraMat.Clone();
|
||
//MLResult mL = simboObjectDetection.RunInferenceFixed(reqcut);
|
||
#endregion
|
||
|
||
#region 显示结果比对
|
||
|
||
//Mat Image1 = Cv2.ImRead("D:/12345.jpg");
|
||
//Bitmap _nitmap1 = Image1.ToBitmap();
|
||
//pictureBox2.Image = _nitmap1;
|
||
//Bitmap _nitmap2 = mL.ResultMap;
|
||
//pictureBox2.Image = _nitmap2;
|
||
#endregion
|
||
|
||
//显示结果
|
||
|
||
|
||
//pictureBox1.Image = mL.ResultMap;
|
||
//List<DetectionResultDetail> ResultDetails = mL.ResultDetails;
|
||
|
||
}
|
||
public async void yolo(Mat cameraMat)
|
||
{
|
||
DateTime dt = DateTime.Now;
|
||
//string fitImageFolder = "D:\\huarui\\pic//";
|
||
string fitImageFolder1 = "D:\\huarui\\pic//" + dt.Year + "_" + dt.Month + "_" + dt.Day + "//";
|
||
if (!Directory.Exists(fitImageFolder1))
|
||
{
|
||
Directory.CreateDirectory(fitImageFolder1);
|
||
}
|
||
|
||
string onename = fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + "num " + allnum + "_1res.jpg";
|
||
|
||
#region 1.预处理
|
||
//Mat PretreatmentMat = new Mat();
|
||
//GetContoursPic(cameraMat, PretreatmentMat);
|
||
#endregion
|
||
|
||
#region 2.YOLO检测
|
||
|
||
|
||
MLRequest req = new MLRequest();
|
||
req.currentMat = cameraMat.Clone();
|
||
req.ResizeWidth = 640;
|
||
req.ResizeHeight = 640;
|
||
req.Score = 0.75f;
|
||
req.in_lable_path = LablePath;//标签路径
|
||
req.confThreshold = 0.6f;//模型置信度
|
||
req.iouThreshold = 0.3f;//检测IOU
|
||
req.out_node_name = "output";
|
||
MLResult mL = simboObjectDetection.RunInferenceFixed(req);
|
||
int shuliang = mL.ResultDetails.Count;
|
||
Console.WriteLine(shuliang.ToString());
|
||
Bitmap resultImage = mL.ResultMap;
|
||
#endregion
|
||
#region 3.OCR识别
|
||
//MLRequest reqcut = new MLRequest();
|
||
//reqcut.currentMat = cameraMat.Clone();
|
||
//MLResult mL = simboObjectDetection.RunInferenceFixed(reqcut);
|
||
#endregion
|
||
|
||
#region 显示结果比对
|
||
|
||
//Mat Image1 = Cv2.ImRead("D:/12345.jpg");
|
||
//Bitmap _nitmap1 = Image1.ToBitmap();
|
||
//pictureBox2.Image = _nitmap1;
|
||
//Bitmap _nitmap2 = mL.ResultMap;
|
||
//pictureBox2.Image = _nitmap2;
|
||
#endregion
|
||
|
||
//显示结果
|
||
int chishu_shuliang = shuliang - textBox1.Text.ToInt();
|
||
if (chishu_shuliang <= 45)
|
||
{
|
||
chishu_shuliang = 42;
|
||
}
|
||
else if (chishu_shuliang >= 45 && chishu_shuliang <= 49)
|
||
{
|
||
chishu_shuliang = 48;
|
||
}
|
||
else
|
||
{
|
||
chishu_shuliang = shuliang - textBox1.Text.ToInt(); ;
|
||
}
|
||
if (textBox4.InvokeRequired)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
textBox4.Text = chishu_shuliang.ToString();
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
textBox4.Text = "haha";
|
||
}
|
||
|
||
if (textBox2.InvokeRequired)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
textBox2.Text = textBox1.Text;
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
textBox2.Text = "haha";
|
||
}
|
||
await Task.Run(() =>
|
||
{
|
||
if (!this.IsHandleCreated)
|
||
return;
|
||
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
pictureBox1.Image = resultImage;
|
||
}));
|
||
});
|
||
if (pictureBox1.InvokeRequired)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
pictureBox1.Image = mL.ResultMap;
|
||
mL.ResultMap.Save("D:\\huarui\\pic\\1RES.jpg");
|
||
mL.ResultMap.Save(onename);
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
pictureBox1.Image = mL.ResultMap;
|
||
}
|
||
//textBox4.Text = chishu_shuliang.ToString();
|
||
//textBox2.Text = textBox1.Text;
|
||
//pictureBox1.Image = mL.ResultMap;
|
||
|
||
//List<DetectionResultDetail> ResultDetails = mL.ResultDetails;
|
||
|
||
}
|
||
private string luoju_detect(string path)
|
||
{
|
||
//string debugPath = System.Environment.CurrentDirectory; //此c#项目的debug文件夹路径
|
||
string pyexePath = @"D:\huarui\demo.exe";
|
||
//python文件所在路径,一般不使用绝对路径,此处仅作为例子,建议转移到debug文件夹下
|
||
|
||
Process p = new Process();
|
||
p.StartInfo.FileName = pyexePath;//需要执行的文件路径
|
||
p.StartInfo.UseShellExecute = false; //必需
|
||
p.StartInfo.RedirectStandardOutput = true;//输出参数设定
|
||
p.StartInfo.RedirectStandardInput = true;//传入参数设定
|
||
p.StartInfo.CreateNoWindow = true;
|
||
p.StartInfo.Arguments = path;//参数以空格分隔,如果某个参数为空,可以传入””
|
||
|
||
p.Start();
|
||
string output = p.StandardOutput.ReadToEnd();
|
||
//p.WaitForExit();//关键,等待外部程序退出后才能往下执行}
|
||
//Console.Write(output);//输出
|
||
//p.Close();
|
||
return output;
|
||
}
|
||
|
||
//string cam1_savepath = fitImageFolder + comboBox1.Text+dt.Year + dt.Month + dt.Day + dt.Hour + dt.Minute + dt.Second + "_2.jpg";
|
||
public void OnCameraMatOutPut2(DateTime dt, Mat cameraMat, int SnapshotCount)
|
||
{
|
||
string fitImageFolder = "D:\\huarui\\pic//";
|
||
string fitImageFolder1 = "D:\\huarui\\pic//" + dt.Year + "_" + dt.Month + "_" + dt.Day + "//";
|
||
if (!Directory.Exists(fitImageFolder1))
|
||
{
|
||
Directory.CreateDirectory(fitImageFolder1);
|
||
}
|
||
|
||
cameraMat.ImWrite(fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + "num " + allnum+"_2.jpg");
|
||
string twoname = fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + "num " + allnum + "_2res.jpg";
|
||
cameraMat.ImWrite(fitImageFolder + "2.jpg");
|
||
FileStream pic2_path = new FileStream("D:\\huarui\\txt\\pic2name.txt", FileMode.Create);
|
||
StreamWriter pic2 = new StreamWriter(pic2_path, Encoding.GetEncoding("gb2312"));
|
||
pic2.Write("Current product" +":"+ fitImageFolder1 + comboBox1.Text + "_" + dt.Year + "_" + dt.Month + "_" + dt.Day + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second + "num " + allnum+ "_2.jpg");
|
||
pic2.Close();
|
||
pic2_path.Close();
|
||
|
||
#region 1.预处理
|
||
//Mat PretreatmentMat = new Mat();
|
||
//GetContoursPic(cameraMat, PretreatmentMat);
|
||
#endregion
|
||
|
||
#region 2.螺距检测
|
||
//string juli = "";
|
||
string path = "D:\\huarui\\pic\\2.jpg";
|
||
var src_images = Cv2.ImRead(path);
|
||
var src = new Mat();
|
||
Cv2.Rotate(src_images, src, RotateFlags.Rotate90Clockwise);
|
||
src.SaveImage(path);
|
||
luoju_detect(path);
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
#region 显示结果
|
||
string luoju_res = File.ReadAllText("D:\\huarui\\txt/luoju.txt");
|
||
if (luoju_res.ToInt() > 80)
|
||
{
|
||
string ju = "1.5";
|
||
textBox7.Text = ju;
|
||
}
|
||
else
|
||
{
|
||
string ju = "1.25";
|
||
textBox7.Text = ju;
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
//显示结果
|
||
|
||
string cemain = "D:\\huarui\\pic\\2_result.jpg";
|
||
Mat cemian_image = Cv2.ImRead(cemain);
|
||
Cv2.ImWrite(twoname, cemian_image);
|
||
pictureBox2.Image = cemian_image.ToBitmap();
|
||
|
||
}
|
||
int j = 1;
|
||
bool previousState = false; // 上一个状态
|
||
bool currentState = false; // 当前状态
|
||
|
||
// 模拟从PLC读取地址值的函数
|
||
bool ReadPLCAddress()
|
||
{
|
||
|
||
int value = siemensPLCTCP.ReadBool("M10.0");
|
||
if (value == 1)
|
||
return true;
|
||
else
|
||
return false;
|
||
|
||
}
|
||
int allnum = 0;
|
||
int oknum = 0;
|
||
int ngnum = 0;
|
||
|
||
private void detectbutton_Click(object sender, EventArgs e)
|
||
{
|
||
//this.detectbutton.BackColor = System.Drawing.Color.SpringGreen;
|
||
#if false
|
||
hikCamera.Snapshot();
|
||
|
||
hikCamera2.Snapshot();
|
||
//string path = "D:\\2.jpg";
|
||
//string luju = "";
|
||
//luoju_detect(path);
|
||
Mat cameraMat = Cv2.ImRead("D:\\huarui\\pic\\1.jpg");
|
||
yolo(cameraMat);
|
||
if (LJDis <= 0)
|
||
|
||
while (true)
|
||
{
|
||
serialPortBase.Writebty(new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0b }, 0, 8);
|
||
while (true)
|
||
{
|
||
if (Ifreturndata)
|
||
{
|
||
Ifreturndata = false;
|
||
break;
|
||
}
|
||
Thread.Sleep(2);
|
||
}
|
||
if (LJDis > 0)
|
||
{
|
||
break;
|
||
}
|
||
Thread.Sleep(2);
|
||
}
|
||
string lj = File.ReadAllText("D:\\huarui\\txt\\changdu.txt");
|
||
if (lj.ToInt() > 2800)
|
||
{
|
||
textBox5.Text = "31";
|
||
}
|
||
else if (2610 < lj.ToInt() && lj.ToInt() < 2800)
|
||
{
|
||
textBox5.Text = "33";
|
||
}
|
||
else if (2500 < lj.ToInt() && lj.ToInt() < 2600)
|
||
{
|
||
textBox5.Text = "33.5";
|
||
}
|
||
else if (2400 < lj.ToInt() && lj.ToInt() < 2500)
|
||
{
|
||
textBox5.Text = "35";
|
||
}
|
||
else if (2300 < lj.ToInt() && lj.ToInt() < 2400)
|
||
{
|
||
textBox5.Text = "36";
|
||
}
|
||
else if (2200 < lj.ToInt() && lj.ToInt() < 2300)
|
||
{
|
||
textBox5.Text = "36.5";
|
||
}
|
||
else if (1800 < lj.ToInt() && lj.ToInt() < 2200)
|
||
{
|
||
textBox5.Text = "40.5";
|
||
}
|
||
if (textBox4.Text == SnaptPage.Text)
|
||
{
|
||
this.textBox4.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox4.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox2.Text == textBox1.Text)
|
||
{
|
||
this.textBox2.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox2.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox5.Text == textBox3.Text)
|
||
{
|
||
this.textBox5.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox5.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox6.Text == textBox7.Text)
|
||
{
|
||
this.textBox7.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox7.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox4.Text == SnaptPage.Text && textBox2.Text == textBox1.Text && textBox5.Text == textBox3.Text && textBox6.Text == textBox7.Text)
|
||
{
|
||
string ok = "D:\\huarui\\pic\\ok.jpg";
|
||
Mat okimage = Cv2.ImRead(ok);
|
||
pictureBox3.Image = okimage.ToBitmap();
|
||
string pic1_name = File.ReadAllText("D:\\huarui\\txt\\pic1name.txt");
|
||
string pic2_name = File.ReadAllText("D:\\huarui\\txt\\pic2name.txt");
|
||
|
||
textBox9.AppendText(pic1_name + pic2_name + "Detection result:" + "OK" + "\r\n");
|
||
string PLCAdress1 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, true);
|
||
}
|
||
else
|
||
{
|
||
string ng = "D:\\huarui\\pic\\ng.jpg";
|
||
Mat ngimage = Cv2.ImRead(ng);
|
||
pictureBox3.Image = ngimage.ToBitmap();
|
||
string pic1_name = File.ReadAllText("D:\\huarui\\txt\\pic1name.txt", Encoding.GetEncoding("gb2312"));
|
||
string pic2_name = File.ReadAllText("D:\\huarui\\txt\\pic2name.txt", Encoding.GetEncoding("gb2312"));
|
||
textBox9.AppendText(pic1_name + pic2_name + "Detection result:" + "NG" + "\r\n");
|
||
string PLCAdress1 = "M12.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, true);
|
||
string PLCAdress1 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, true);
|
||
}
|
||
|
||
|
||
#else
|
||
try
|
||
{
|
||
Task.Run(() =>
|
||
{
|
||
while (true)
|
||
{
|
||
currentState = ReadPLCAddress();
|
||
|
||
// 检测上升沿
|
||
if (currentState && !previousState)
|
||
{
|
||
autoResetEvent.Set();
|
||
Console.WriteLine("检测到上升沿==触发按钮");
|
||
waitResetEvent.WaitOne();//可以删除
|
||
}
|
||
// 更新上一个状态
|
||
previousState = currentState;
|
||
|
||
// 延迟一段时间,避免频繁读取
|
||
Thread.Sleep(100); // 延迟100毫秒
|
||
}
|
||
});
|
||
|
||
Task.Run(() =>
|
||
{
|
||
while (true)
|
||
{
|
||
autoResetEvent.WaitOne();
|
||
//currentState = ReadPLCAddress();
|
||
|
||
// 检测上升沿
|
||
// if (currentState && !previousState)
|
||
{
|
||
// Console.WriteLine("检测到上升沿==触发按钮");
|
||
try
|
||
{
|
||
#region 检测
|
||
textBox9.AppendText("开始检测" + "\r\n");
|
||
hikCamera.Snapshot();
|
||
|
||
hikCamera2.Snapshot();
|
||
|
||
|
||
//string path = "D:\\2.jpg";
|
||
//string luju = "";
|
||
//luoju_detect(path);
|
||
Mat cameraMat = Cv2.ImRead("D:\\huarui\\pic\\1.jpg");
|
||
yolo(cameraMat);
|
||
// if (LJDis<=0)
|
||
allnum = allnum + 1;
|
||
textBox13.Text = allnum.ToString();
|
||
while (true)
|
||
{
|
||
serialPortBase.Writebty(new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0b }, 0, 8);
|
||
while (true)
|
||
{
|
||
if (Ifreturndata)
|
||
{
|
||
Ifreturndata = false;
|
||
break;
|
||
}
|
||
Thread.Sleep(2);
|
||
}
|
||
if (LJDis > 0)
|
||
{
|
||
break;
|
||
}
|
||
Thread.Sleep(2);
|
||
}
|
||
string lj = File.ReadAllText("D:\\huarui\\txt\\changdu.txt");
|
||
if (lj.ToInt() > 2800)
|
||
{
|
||
textBox5.Text = "31";
|
||
}
|
||
else if (2610 < lj.ToInt() && lj.ToInt() < 2800)
|
||
{
|
||
textBox5.Text = "33";
|
||
}
|
||
else if (2500 < lj.ToInt() && lj.ToInt() < 2600)
|
||
{
|
||
textBox5.Text = "33.5";
|
||
}
|
||
else if (2400 < lj.ToInt() && lj.ToInt() < 2500)
|
||
{
|
||
textBox5.Text = "35";
|
||
}
|
||
else if (2300 < lj.ToInt() && lj.ToInt() < 2400)
|
||
{
|
||
textBox5.Text = "36";
|
||
}
|
||
else if (2200 < lj.ToInt() && lj.ToInt() < 2300)
|
||
{
|
||
textBox5.Text = "36.5";
|
||
}
|
||
else if (1800 < lj.ToInt() && lj.ToInt() < 2200)
|
||
{
|
||
textBox5.Text = "40.5";
|
||
}
|
||
if (textBox4.Text == SnaptPage.Text)
|
||
{
|
||
this.textBox4.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox4.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox2.Text == textBox1.Text)
|
||
{
|
||
this.textBox2.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox2.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox5.Text == textBox3.Text)
|
||
{
|
||
this.textBox5.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox5.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox6.Text == textBox7.Text)
|
||
{
|
||
this.textBox7.BackColor = System.Drawing.Color.SpringGreen;
|
||
}
|
||
else
|
||
{
|
||
this.textBox7.BackColor = System.Drawing.Color.Red;
|
||
}
|
||
if (textBox4.Text == SnaptPage.Text && textBox2.Text == textBox1.Text && textBox5.Text == textBox3.Text && textBox6.Text == textBox7.Text)
|
||
{
|
||
string ok = "D:\\huarui\\pic\\ok.jpg";
|
||
Mat okimage = Cv2.ImRead(ok);
|
||
pictureBox3.Image = okimage.ToBitmap();
|
||
string pic1_name = File.ReadAllText("D:\\huarui\\txt\\pic1name.txt");
|
||
string pic2_name = File.ReadAllText("D:\\huarui\\txt\\pic2name.txt");
|
||
string leixing2 = File.ReadAllText("D:\\huarui\\txt/xinghao.txt");
|
||
textBox12.Text = leixing2;
|
||
oknum = oknum + 1;
|
||
textBox14.Text = oknum.ToString();
|
||
textBox9.AppendText(pic1_name + pic2_name + "Detection result:" + "OK" + "\r\n");
|
||
string PLCAdress1 = "M12.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, false);
|
||
string PLCAdress2 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress2, false);
|
||
string PLCAdress4 = "M14.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress4, false);
|
||
string PLCAdress3 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress3, true);
|
||
}
|
||
else
|
||
{
|
||
string ng = "D:\\huarui\\pic\\ng.jpg";
|
||
Mat ngimage = Cv2.ImRead(ng);
|
||
pictureBox3.Image = ngimage.ToBitmap();
|
||
string pic1_name = File.ReadAllText("D:\\huarui\\txt\\pic1name.txt", Encoding.GetEncoding("gb2312"));
|
||
string pic2_name = File.ReadAllText("D:\\huarui\\txt\\pic2name.txt", Encoding.GetEncoding("gb2312"));
|
||
textBox9.AppendText(pic1_name + pic2_name + "Detection result:" + "NG" + "\r\n");
|
||
string leixing2 = File.ReadAllText("D:\\huarui\\txt/xinghao.txt");
|
||
textBox12.Text = leixing2;
|
||
ngnum = ngnum + 1;
|
||
textBox15.Text = ngnum.ToString();
|
||
string PLCAdress4 = "M14.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress4, false);
|
||
string PLCAdress3 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress3, false);
|
||
string PLCAdress1 = "M12.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, true);
|
||
string PLCAdress2 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress2, true);
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
|
||
|
||
}
|
||
waitResetEvent.Set();//可以删除
|
||
Thread.Sleep(20); // 延迟100毫秒
|
||
|
||
}
|
||
|
||
|
||
|
||
});
|
||
|
||
}
|
||
catch(Exception exx)
|
||
{
|
||
throw exx;
|
||
}
|
||
|
||
|
||
#endif
|
||
|
||
}
|
||
|
||
private void Camopenbutton_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
|
||
}
|
||
|
||
private void opendevicebutton_Click(object sender, EventArgs e)
|
||
{
|
||
string PLCAdress1 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, true);
|
||
this.opendevicebutton.BackColor = System.Drawing.Color.SpringGreen;
|
||
try
|
||
{
|
||
|
||
hikCamera.Start("169.254.177.197");//默认开启相机1
|
||
hikCamera2.Start("169.254.21.198");//默认开启相机2
|
||
serialPortBase.Init("Com3");
|
||
serialPortBase.Start();
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
hikCamera.OnHImageOutput -= OnCameraMatOutPut;
|
||
hikCamera.OnHImageOutput += OnCameraMatOutPut;
|
||
hikCamera2.OnHImageOutput -= OnCameraMatOutPut2;
|
||
hikCamera2.OnHImageOutput += OnCameraMatOutPut2;
|
||
serialPortBase.OnDataReceived -= SerialPort_DataReceived;
|
||
serialPortBase.OnDataReceived += SerialPort_DataReceived;
|
||
//serialPortBase.WriteStr("1222222222222");
|
||
//textBox9.Text.Append("设备开启成功");
|
||
textBox9.AppendText("设备开启成功" + "\r\n");
|
||
}
|
||
private void photobutton_Click(object sender, EventArgs e)
|
||
{
|
||
hikCamera.Snapshot();
|
||
}
|
||
|
||
private void Camclosebutton_Click(object sender, EventArgs e)
|
||
{
|
||
//hikCamera.Stop();
|
||
}
|
||
|
||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
hikCamera.Stop(hikCamera.device);
|
||
hikCamera2.Stop(hikCamera2.device);
|
||
DateTime dt1 = DateTime.Now;
|
||
FileStream baogao_path = new FileStream("D:\\huarui\\txt\\" + dt1.Year + "_" + dt1.Month + "_" + dt1.Day + "_" + dt1.Hour + "_" + dt1.Minute + "_检测报告.txt", FileMode.Create);
|
||
StreamWriter baogao = new StreamWriter(baogao_path, Encoding.Default);
|
||
string jiancejieguo ="型号:"+ textBox12.Text + " 总数: "+textBox13.Text + " 合格数: "+textBox14.Text +" 不合格数: "+ textBox15.Text;
|
||
baogao.Write(jiancejieguo);
|
||
baogao.Close();
|
||
baogao_path.Close();
|
||
FileStream log_path = new FileStream("D:\\huarui\\txt\\" + dt1.Year + dt1.Month + dt1.Day + dt1.Hour + "_log.txt", FileMode.Create);
|
||
StreamWriter log = new StreamWriter(log_path, Encoding.Default);
|
||
log.Write(textBox9.Text);
|
||
log.Close();
|
||
log.Close();
|
||
// hikCamera2.Stop();
|
||
string PLCAdress1 = "M12.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress1, false);
|
||
string PLCAdress2 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress2, false);
|
||
string PLCAdress4 = "M14.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress4, false);
|
||
string PLCAdress3 = "M11.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress3, false);
|
||
}
|
||
|
||
private void groupBox4_Enter(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void groupBox3_Enter(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void label3_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void groupBox2_Enter(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void label1_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void textBox1_TextChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
int value = siemensPLCTCP.ReadBool(PLCAdress);
|
||
textBox8.Text = value.ToString();
|
||
}
|
||
}
|
||
|
||
private void button2_Click(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
bool value = textBox11.Text.ToBool();
|
||
siemensPLCTCP.WriteBool(PLCAdress, value);
|
||
}
|
||
|
||
}
|
||
|
||
private void textBox10_TextChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button1_Click_1(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
int value = siemensPLCTCP.ReadBool(PLCAdress);
|
||
textBox8.Text = value.ToString();
|
||
}
|
||
}
|
||
|
||
private void button2_Click_1(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
bool value = textBox11.Text.ToBool();
|
||
siemensPLCTCP.WriteBool(PLCAdress, value);
|
||
}
|
||
}
|
||
|
||
private void button1_Click_2(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
int value = siemensPLCTCP.ReadBool(PLCAdress);
|
||
textBox8.Text = value.ToString();
|
||
}
|
||
}
|
||
|
||
private void button2_Click_2(object sender, EventArgs e)
|
||
{
|
||
if (siemensPLCTCP != null)
|
||
{
|
||
|
||
string PLCAdress = textBox10.Text.ToString();
|
||
bool value = textBox11.Text.ToBool();
|
||
siemensPLCTCP.WriteBool(PLCAdress, value);
|
||
}
|
||
}
|
||
|
||
private void button3_Click(object sender, EventArgs e)
|
||
{
|
||
string PLCAdress2 = "M13.0";
|
||
siemensPLCTCP.WriteBool(PLCAdress2, false);
|
||
}
|
||
}
|
||
}
|