You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rs.DeweyTester/Rs.DeweyTester/Vision/LoadTakeProductVision.cs

198 lines
6.8 KiB
C#

using Newtonsoft.Json;
using NPOI.SS.UserModel.Charts;
using Rs.Framework;
using Rs.MotionPlat.Commom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Vision
{
/// <summary>
/// 料仓取料拍照
/// </summary>
public class LoadTakeProductVision:BaseVision
{
string data = string.Empty;
static int visionNum = 4;
static ManualResetEvent visionGrabEvent = new ManualResetEvent(false);
static List<VisionResult> visionResultList = new List<VisionResult>();
bool reciveData = false;
public override void VNozzleCalib_DataRecived(Socket arg1, byte[] arg2)
{
if(reciveData)
{
//所有的视觉数据以;作为结束符
foreach (byte b in arg2)
{
if (b != 59)
{
reciveBuffer.Add(b);
}
else
{
data = Encoding.ASCII.GetString(reciveBuffer.ToArray());
//MessageQueue.Instance.Insert(data);
if (data == "M,0")
{
//MessageQueue.Instance.Insert("视觉收到拍照指令");
}
else if(data=="C,0")
{
visionGrabEvent.Set();
}
else
{
visionGrabEvent.Set();
//result = new VisionResult();
//解析拍照结果
// string[] results = data.Split(',');
//switch (results[1])
//{
// case "0":
// result.LocationOK = true;
// result.ScanBarcodeOK = true;
// result.SN = results[2];
// result.Result = true;
// result.OffsetX = double.Parse(results[2]);
// result.OffsetY = double.Parse(results[3]);
// break;
// case "-1":
// result.LocationOK = true;
// result.ScanBarcodeOK = false;
// result.SN = results[2];
// result.Result = true;
// result.OffsetX = double.Parse(results[2]);
// result.OffsetY = double.Parse(results[3]);
// break;
// case "-2":
// result.LocationOK = false;
// result.ScanBarcodeOK = false;
// result.SN = "";
// result.Result = false;
// result.OffsetX = 0.0;
// result.OffsetY = 0.0;
// break;
// case "-3":
// result.LocationOK = false;
// result.ScanBarcodeOK = false;
// result.SN = "";
// result.Result = true;
// result.OffsetX = 0.0;
// result.OffsetY = 0.0;
// break;
// default:
// break;
//}
//visionResultList.Add(result);
//if (visionResultList.Count == visionNum)
//{
// visionGrabEvent.Set();
//}
}
reciveBuffer.Clear();
}
}
}
}
public bool ClearQueue(int timeout = 10000)
{
visionGrabEvent.Reset();
int len = vNozzleCalib.Send("C;");
reciveData = true;
if (len > 0)
{
if (visionGrabEvent.WaitOne(timeout))
{
reciveData = false;
MessageQueue.Instance.Insert($"收到视觉结果:{data}");
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override List<VisionResult> OneGrabFour(int timeout = 10000)
{
visionResultList.Clear();
Ops.On("光源1");
result = null;
visionGrabEvent.Reset();
int len = vNozzleCalib.Send("M;");
reciveData = true;
if (len > 0)
{
if (visionGrabEvent.WaitOne(timeout))
{
reciveData = false;
MessageQueue.Instance.Insert($"收到视觉结果:{data}");
Ops.Off("光源1");
return visionResultList;
}
else
{
Ops.Off("光源1");
return null;
}
}
else
{
Ops.Off("光源1");
return null;
}
}
/// <summary>
/// 一拍十六
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public string OneGrabSixteen(int timeout = 10000)
{
data = "";
visionResultList.Clear();
result = null;
visionGrabEvent.Reset();
int len = vNozzleCalib.Send("M;");
reciveData = true;
if (len > 0)
{
MessageQueue.Instance.Insert($" <<< M;");
if (visionGrabEvent.WaitOne(timeout))
{
reciveData = false;
MessageQueue.Instance.Insert($"收到视觉结果:{data}");
return data;
}
else
{
return null;
}
}
else
{
return null;
}
}
}
}