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.

195 lines
5.5 KiB
C#

using Newtonsoft.Json;
using Rs.Framework;
using Rs.MotionPlat.Commom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Vision
{
public enum EVisionScene
{
/// <summary>
/// 一拍四
/// </summary>
OneGrabFour = 6051,
/// <summary>
/// 有无料
/// </summary>
HaveOrNot,
/// <summary>
/// 单个扫描
/// </summary>
SingleScanBarcode = 6055,
/// <summary>
/// 料盘定位
/// </summary>
TrayLocation,
/// <summary>
/// 吸嘴标定
/// </summary>
NozzleCalib,
/// <summary>
/// 治具放料定位
/// </summary>
FixtureDumpProduct
}
public class VisionResult
{
/// <summary>
/// 拍照结果是否OK
/// </summary>
public bool Result { get; set; }
/// <summary>
/// 是否有物料
/// </summary>
public bool HasProduct { get; set; } = false;
/// <summary>
/// 定位是否成功
/// </summary>
public bool LocationOK { get; set; }
/// <summary>
/// 扫码是否成功
/// </summary>
public bool ScanBarcodeOK { get; set; }
/// <summary>
/// SN
/// </summary>
public string SN { get; set; }
/// <summary>
/// 拍照X偏移
/// </summary>
public double OffsetX { get; set; }
/// <summary>
/// 拍照Y偏移
/// </summary>
public double OffsetY { get; set; }
public string Products { get; set; }
}
public class BaseVision
{
public string Name { get; set; }
protected VisionResult result = new VisionResult();
protected TcpClientHelper vNozzleCalib = new TcpClientHelper();
protected ManualResetEvent NozzleCalibReciveEvent = new ManualResetEvent(false);
protected List<byte> reciveBuffer = new List<byte>();
public void Init(string name,int port)
{
Name = name;
vNozzleCalib.Connect("127.0.0.1", port);
vNozzleCalib.OnConnected += VNozzleCalib_OnConnected;
vNozzleCalib.OnDisconnected += VNozzleCalib_OnDisconnected;
vNozzleCalib.DataRecived += VNozzleCalib_DataRecived;
}
private void VNozzleCalib_OnDisconnected(System.Net.Sockets.Socket obj)
{
MessageQueue.Instance.Insert($"{Name} vision disconnect");
}
private void VNozzleCalib_OnConnected(System.Net.Sockets.Socket obj)
{
MessageQueue.Instance.Insert($"{Name} vision connect success");
}
public virtual void VNozzleCalib_DataRecived(System.Net.Sockets.Socket arg1, byte[] arg2)
{
//所有的视觉数据以;作为结束符
foreach (byte b in arg2)
{
if (b != 59)
{
reciveBuffer.Add(b);
}
else
{
string data = Encoding.ASCII.GetString(reciveBuffer.ToArray());
MessageQueue.Instance.Insert(data);
if (data == "M,0")
{
MessageQueue.Instance.Insert("视觉收到拍照指令");
}
else
{
result = new VisionResult();
//解析拍照结果
string[] results = data.Split(',');
if (results[1] == "-1")
{
result.Result = false;
}
else
{
result.Result = true;
result.OffsetX = double.Parse(results[2]);
result.OffsetY = double.Parse(results[3]);
}
NozzleCalibReciveEvent.Set();
}
reciveBuffer.Clear();
}
}
}
public virtual VisionResult Grab(int timeout=3000,string command="")
{
Ops.On("光源1");
result = null;
NozzleCalibReciveEvent.Reset();
int len = 0;
if (string.IsNullOrEmpty(command))
{
len = vNozzleCalib.Send("M;");
}
else
{
len = vNozzleCalib.Send($"{command}");
}
if (len > 0)
{
if (NozzleCalibReciveEvent.WaitOne(timeout))
{
MessageQueue.Instance.Insert($"收到视觉结果:{JsonConvert.SerializeObject(result)}");
Ops.Off("光源1");
return result;
}
else
{
Ops.Off("光源1");
return null;
}
}
else
{
Ops.Off("光源1");
return null;
}
}
public virtual List<VisionResult> OneGrabFour(int timeout = 3000)
{
return null;
}
public virtual int Send(string msg)
{
if(vNozzleCalib.IsConnected)
{
return vNozzleCalib.Send(msg);
}
return -1;
}
}
}