1062 lines
34 KiB
C#
1062 lines
34 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Net.NetworkInformation;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Linq;
|
||
using DH.Commons.Base;
|
||
using DH.Commons.Enums;
|
||
using HslCommunication;
|
||
using HslCommunication.Enthernet;
|
||
using HslCommunication.Profinet.XINJE;
|
||
using OpenCvSharp;
|
||
|
||
namespace DH.Devices.PLC
|
||
{
|
||
public class XinJEPLCTcpNet : PLCBase
|
||
{
|
||
private static XinJEPLCTcpNet _instance;
|
||
public static XinJEPLCTcpNet Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
_instance = new XinJEPLCTcpNet();
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
private XinJETcpNet TcpNet = new XinJETcpNet();
|
||
|
||
public override bool PLCConnect()
|
||
{
|
||
try
|
||
{
|
||
TcpNet.IpAddress = IP;
|
||
TcpNet.Port = Port;
|
||
TcpNet.ConnectTimeOut = 5000;
|
||
TcpNet.ReceiveTimeOut = 10000;
|
||
TcpNet.SleepTime = 0;
|
||
TcpNet.AddressStartWithZero = true;
|
||
TcpNet.IsStringReverse = false;
|
||
TcpNet.DataFormat = HslCommunication.Core.DataFormat.ABCD;
|
||
TcpNet.Station = 1;
|
||
TcpNet.Series = XinJESeries.XD;
|
||
|
||
PLCItem itemSpeed = PLCItemList.FirstOrDefault(u => u.Name == "转盘速度");
|
||
if(itemSpeed== null)
|
||
{
|
||
return false;
|
||
}
|
||
OperateResult ret = TcpNet.ConnectServer();
|
||
if (ret.IsSuccess)
|
||
{
|
||
Connected = true;
|
||
CountToZero();
|
||
TcpNet.Write("M122", true);
|
||
MonitorPieces();
|
||
TurntableStop();
|
||
PrepareMotion();//心跳监听
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
|
||
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public override ushort ReadShort(string address)
|
||
{
|
||
try
|
||
{
|
||
// 读取Int变量
|
||
var result = TcpNet.ReadInt16(address);
|
||
if (result.IsSuccess)
|
||
{
|
||
return (ushort)result.Content;
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public override int ReadInt(string address)
|
||
{
|
||
try
|
||
{
|
||
// 读取Int变量
|
||
var result = TcpNet.ReadInt32(address);
|
||
if (result.IsSuccess)
|
||
{
|
||
return result.Content;
|
||
}
|
||
else
|
||
{
|
||
|
||
return -1;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
|
||
public override float ReadFloat(string address)
|
||
{
|
||
try
|
||
{
|
||
// 读取Float变量
|
||
var result = TcpNet.ReadFloat(address);
|
||
if (result.IsSuccess)
|
||
{
|
||
return result.Content;
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
public override bool ReadBool(string address)
|
||
{
|
||
try
|
||
{
|
||
// 读取Bool变量
|
||
var result = TcpNet.ReadBool(address);
|
||
if (result.IsSuccess)
|
||
{
|
||
return result.Content;
|
||
}
|
||
else
|
||
{
|
||
return false; // 或者可以考虑返回其他的错误标识符
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false; // 或者返回其他的错误标识符
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 写单独地址 short 值
|
||
/// </summary>
|
||
/// <param name="address">地址</param>
|
||
/// <param name="writeValue">要写入的 int 值</param>
|
||
/// <param name="waitForReply">是否等待回复</param>
|
||
public override bool WriteShort(string address, short writeValue, bool waitForReply = true)
|
||
{
|
||
if (string.IsNullOrEmpty(address))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int repeatTime = 3;
|
||
Stopwatch sw = new Stopwatch();
|
||
sw.Start(); // 启动计时
|
||
|
||
do
|
||
{
|
||
try
|
||
{
|
||
var result = TcpNet.Write(address, (short)writeValue);
|
||
if (result.IsSuccess)
|
||
{
|
||
sw.Stop();
|
||
//LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{Name} {address},写入 {writeValue} 完成,耗时:{sw.ElapsedMilliseconds} ms");
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
repeatTime--;
|
||
if (repeatTime <= 0)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
} while (repeatTime > 0);
|
||
|
||
sw.Stop();
|
||
return false;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 写单独地址 int 值
|
||
/// </summary>
|
||
/// <param name="address">地址</param>
|
||
/// <param name="writeValue">要写入的 int 值</param>
|
||
/// <param name="waitForReply">是否等待回复</param>
|
||
public override bool WriteInt(string address, int writeValue, bool waitForReply = true)
|
||
{
|
||
if (string.IsNullOrEmpty(address))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int repeatTime = 3;
|
||
Stopwatch sw = new Stopwatch();
|
||
sw.Start(); // 启动计时
|
||
|
||
do
|
||
{
|
||
try
|
||
{
|
||
var result = TcpNet.Write(address, writeValue);
|
||
if (result.IsSuccess)
|
||
{
|
||
sw.Stop();
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
repeatTime--;
|
||
if (repeatTime <= 0)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
} while (repeatTime > 0);
|
||
|
||
sw.Stop();
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 写单独地址 Dint 值
|
||
/// </summary>
|
||
/// <param name="address">地址</param>
|
||
/// <param name="writeValue">要写入的 Dint 值</param>
|
||
/// <param name="waitForReply">是否等待回复</param>
|
||
public override bool WriteDInt(string address, int writeValue, bool waitForReply = true)
|
||
{
|
||
if (string.IsNullOrEmpty(address))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int repeatTime = 3;
|
||
Stopwatch sw = new Stopwatch();
|
||
sw.Start(); // 启动计时
|
||
|
||
do
|
||
{
|
||
try
|
||
{
|
||
string result = Regex.Replace(address, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
string address1 = "HD" + r.ToString();
|
||
short high = (short)(writeValue >> 16); // 高 16 位
|
||
short low = (short)(writeValue & 0xFFFF); // 低 16 位
|
||
TcpNet.Write(address1, high);
|
||
Thread.Sleep(10);
|
||
var res = TcpNet.Write(address, low);
|
||
if (res.IsSuccess)
|
||
{
|
||
sw.Stop();
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
repeatTime--;
|
||
if (repeatTime <= 0)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
} while (repeatTime > 0);
|
||
|
||
sw.Stop();
|
||
return false;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 写单独地址 float 值
|
||
/// </summary>
|
||
/// <param name="address">地址</param>
|
||
/// <param name="writeValue">要写入的 float 值</param>
|
||
/// <param name="waitForReply">是否等待回复</param>
|
||
public override bool WriteFloat(string address, float writeValue, bool waitForReply = true)
|
||
{
|
||
if (string.IsNullOrEmpty(address))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int repeatTime = 3;
|
||
Stopwatch sw = new Stopwatch();
|
||
sw.Start(); // 启动计时
|
||
|
||
do
|
||
{
|
||
try
|
||
{
|
||
var result = TcpNet.Write(address, writeValue);
|
||
if (result.IsSuccess)
|
||
{
|
||
sw.Stop();
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
repeatTime--;
|
||
if (repeatTime <= 0)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
} while (repeatTime > 0);
|
||
|
||
sw.Stop();
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 写单独地址 bool 值
|
||
/// </summary>
|
||
/// <param name="address">地址</param>
|
||
/// <param name="writeValue">要写入的 bool 值</param>
|
||
/// <param name="waitForReply">是否等待回复</param>
|
||
public override bool WriteBool(string address, bool writeValue, bool waitForReply = true)
|
||
{
|
||
if (string.IsNullOrEmpty(address))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int repeatTime = 3;
|
||
Stopwatch sw = new Stopwatch();
|
||
sw.Start(); // 启动计时
|
||
|
||
do
|
||
{
|
||
try
|
||
{
|
||
var result = TcpNet.Write(address, writeValue);
|
||
if (result.IsSuccess)
|
||
{
|
||
sw.Stop();
|
||
return true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
repeatTime--;
|
||
if (repeatTime <= 0)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
} while (repeatTime > 0);
|
||
|
||
sw.Stop();
|
||
Thread.Sleep(10);
|
||
return false;
|
||
}
|
||
|
||
|
||
public override bool PLCDisConnect()
|
||
{
|
||
if (Connected)
|
||
{
|
||
TurntableStop();
|
||
var res = TcpNet.ConnectClose();
|
||
if (res.IsSuccess)
|
||
{
|
||
Connected = false;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
|
||
private void MonitorPieces()
|
||
{
|
||
|
||
ThreadStart ts = new ThreadStart(MonitorPiecesImpl);
|
||
Thread th = new Thread(ts);
|
||
th.Priority = ThreadPriority.AboveNormal;
|
||
th.IsBackground = false;
|
||
th.Start();
|
||
|
||
}
|
||
|
||
|
||
public TaskFactory _taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);
|
||
|
||
private Dictionary<ushort, uint> piecesCountDic = new Dictionary<ushort, uint>();
|
||
private uint piecesCount = 0;
|
||
|
||
/// <summary>
|
||
/// int,int 轴号 捕获位置
|
||
/// </summary>
|
||
public event Action<int, uint> OnNewPieces;
|
||
|
||
public void NewPieces(int axisIndex, uint pieceNumber)
|
||
{
|
||
_taskFactory.StartNew(() =>
|
||
{
|
||
Thread.CurrentThread.Priority = ThreadPriority.Highest;
|
||
|
||
OnNewPieces?.Invoke(axisIndex, pieceNumber);
|
||
});
|
||
}
|
||
public async Task HeartbeatAsync1()
|
||
{
|
||
|
||
|
||
|
||
|
||
while (Connected)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "心跳地址");
|
||
if (pLCItem == null)
|
||
return;
|
||
string HeartbeatAddress = pLCItem.Type + pLCItem.Address;
|
||
TcpNet.Write(HeartbeatAddress, true);
|
||
await Task.Delay(900); // 非阻塞,等待1秒
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 入料监听
|
||
/// </summary>
|
||
/// <param name="axisIndex"></param>
|
||
private void MonitorPiecesImpl()
|
||
{
|
||
|
||
PLCItem pLCItem= PLCItemList.FirstOrDefault(u => u.Name == "产品计数");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Count = pLCItem.Type + pLCItem.Address;
|
||
DateTime startTime = DateTime.Now;
|
||
DateTime endTime = startTime;
|
||
TimeSpan timeSpan = endTime - startTime;
|
||
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
|
||
//while (CurrentState != DeviceState.DSClose && CurrentState != DeviceState.DSExcept && CurrentState != DeviceState.DSUninit)
|
||
while (Connected)
|
||
{
|
||
Stopwatch sw = new Stopwatch();
|
||
uint tmpPieceNumber = 0;
|
||
sw.Start();
|
||
|
||
// var ret = TcpNet.ReadUInt16("D1016");
|
||
var ret = TcpNet.ReadUInt16(Count);
|
||
|
||
sw.Stop();
|
||
if (ret.IsSuccess)
|
||
{
|
||
tmpPieceNumber = ret.Content;
|
||
}
|
||
|
||
if (ret.IsSuccess && ret.Content > piecesCount)
|
||
{
|
||
sw.Start();
|
||
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} 板卡{station}产品入列触发{tmpPieceNumber}");
|
||
//LogAsync(DateTime.Now, LogLevel.Information, $"转盘{0}产品入列 {piecesCountDic[0]} size:{sum}");
|
||
if (tmpPieceNumber != piecesCount + 1)
|
||
{
|
||
//LogAsync(DateTime.Now, LogLevel.Information, $"入列触发丢失\t{tmpPieceNumber}");
|
||
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}\t板卡{station}产品入列触发丢失,{piecesCountDic[station]}\t{tmpPieceNumber}");
|
||
|
||
}
|
||
piecesCount = tmpPieceNumber;
|
||
//NewPieces(ai, piecesCountDic[station]);
|
||
NewPieces(1, piecesCount);
|
||
sw.Stop();
|
||
startTime = DateTime.Now;
|
||
//if (idalarm)
|
||
//{
|
||
// idalarm = false;
|
||
// AlarmVibration(false);
|
||
//}
|
||
|
||
}
|
||
|
||
Thread.Sleep(1);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 转盘开启操作
|
||
/// </summary>
|
||
public void TurntableOpen()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "计数清零");
|
||
if (pLCItem == null)
|
||
return;
|
||
string CountToZero = pLCItem.Type + pLCItem.Address;
|
||
PLCItem DiskSpeedItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘速度");
|
||
if (DiskSpeedItem == null)
|
||
return;
|
||
string diskSpeedadress = DiskSpeedItem.Type + DiskSpeedItem.Address;
|
||
int diskSpeedValue= Convert.ToInt32( DiskSpeedItem.Value);
|
||
PLCItem DiskDirectionItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘方向");
|
||
if (DiskDirectionItem == null)
|
||
return;
|
||
string diskDirectionadress = DiskDirectionItem.Type + DiskDirectionItem.Address;
|
||
bool Direction =Convert.ToBoolean( DiskDirectionItem.Value);
|
||
PLCItem DiskOpenItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘使能");
|
||
if (DiskOpenItem == null)
|
||
return;
|
||
string diskopenadress = DiskOpenItem.Type + DiskOpenItem.Address;
|
||
PLCItem DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘启动");
|
||
if (DiskRunItem == null)
|
||
return;
|
||
string diskadress = DiskRunItem.Type + DiskRunItem.Address;
|
||
|
||
|
||
WriteBool(CountToZero, true);
|
||
Thread.Sleep(10);
|
||
WriteBool("M10", false);
|
||
Thread.Sleep(10);
|
||
//速度
|
||
TcpNet.Write(diskSpeedadress, (ushort)diskSpeedValue);
|
||
Thread.Sleep(10);
|
||
//方向
|
||
WriteBool(diskDirectionadress, Direction);
|
||
Thread.Sleep(10);
|
||
//使能
|
||
WriteBool(diskopenadress, true);
|
||
Thread.Sleep(10);
|
||
//启动
|
||
WriteBool(diskadress, true);
|
||
|
||
|
||
//WriteBool("M122", true);
|
||
//Thread.Sleep(10);
|
||
//WriteBool("M10", false);
|
||
//Thread.Sleep(10);
|
||
////速度
|
||
//TcpNet.Write("HD10", (ushort)10000);
|
||
//Thread.Sleep(10);
|
||
////方向
|
||
//WriteBool("M1", Direction);
|
||
//Thread.Sleep(10);
|
||
////使能
|
||
//WriteBool("M2", true);
|
||
//Thread.Sleep(10);
|
||
////启动
|
||
//WriteBool("M0", true);
|
||
Thread.Sleep(10);
|
||
// _mainMotion.CurrentState = DeviceState.DSOpen;
|
||
piecesCount = 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 转盘停止操作
|
||
/// </summary>
|
||
public void TurntableStop()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "计数清零");
|
||
if (pLCItem == null)
|
||
return;
|
||
string CountToZero = pLCItem.Type + pLCItem.Address;
|
||
PLCItem DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘启动");
|
||
if (DiskRunItem == null)
|
||
return;
|
||
string diskadress = DiskRunItem.Type + DiskRunItem.Address;
|
||
PLCItem DiskOpenItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘使能");
|
||
if (DiskOpenItem == null)
|
||
return;
|
||
string diskopenadress = DiskOpenItem.Type + DiskOpenItem.Address;
|
||
|
||
WriteBool(CountToZero, true);
|
||
Thread.Sleep(50);
|
||
WriteBool(diskadress, false);
|
||
Thread.Sleep(50);
|
||
WriteBool(diskopenadress, false);
|
||
Thread.Sleep(50);
|
||
WriteBool("M10", false);
|
||
|
||
//WriteBool("M122", true);
|
||
//Thread.Sleep(50);
|
||
//WriteBool("M0", false);
|
||
//Thread.Sleep(50);
|
||
//WriteBool("M2", false);
|
||
//Thread.Sleep(50);
|
||
//WriteBool("M50", false);
|
||
piecesCount = 0;
|
||
}
|
||
|
||
private void PrepareMotion()
|
||
{
|
||
|
||
|
||
|
||
|
||
//心跳
|
||
//if (X018PLCConfig.Heartbeat)
|
||
//{
|
||
|
||
Task.Run(async () => await HeartbeatAsync1());
|
||
//}
|
||
|
||
////写入工件最大值、最小值
|
||
ProjectValue();
|
||
|
||
|
||
////写入工位脉冲
|
||
Workstation1Pulse();
|
||
Workstation2Pulse();
|
||
Workstation3Pulse();
|
||
Workstation4Pulse();
|
||
Workstation5Pulse();
|
||
|
||
////写入吹气时间
|
||
ChuiQiTime();
|
||
|
||
|
||
////写入吹气脉冲
|
||
OKPulse();
|
||
NGPulse();
|
||
//if (_GC01Driver == null)
|
||
//{
|
||
// _GC01Driver = DeviceCollection.FirstOrDefault(u => u is GC01Driver) as GC01Driver;
|
||
//}
|
||
//if (_GC01Driver == null)
|
||
//{
|
||
// throw new ProcessException($"未能获取激光位移传感器驱动");
|
||
//}
|
||
//if (_vibrationDriver == null)
|
||
//{
|
||
// _vibrationDriver = DeviceCollection.FirstOrDefault(u => u is JYDAMDriver) as JYDAMDriver;
|
||
//}
|
||
//if (_vibrationDriver == null)
|
||
//{
|
||
// throw new ProcessException($"未能获取振动盘控制器驱动");
|
||
//}
|
||
|
||
// ResetTimer = new Timer(FullResetProcessExcute, null, -1, -1);
|
||
|
||
//feedingProductTimer = new Timer(FeedingProductTriggerExcute, null, -1, -1);
|
||
//feedingProductTimerTimer = new Timer(UpdateFeedingProductTrigger, null, -1, -1);
|
||
|
||
//_mainMotion.OnAxisPositionChanged -= MainMotion_OnAxisPositionChanged;
|
||
//_mainMotion.OnAxisPositionChanged += MainMotion_OnAxisPositionChanged;
|
||
|
||
//_mainMotion.OnCapturePositionChanged -= MainMotion_OnCapturePositionChanged;
|
||
//_mainMotion.OnCapturePositionChanged += MainMotion_OnCapturePositionChanged;
|
||
|
||
// _mainMotion.OnNewPieces -= MainMotion_NewPieces;
|
||
// _mainMotion.OnNewPieces += MainMotion_NewPieces;
|
||
|
||
//_mainMotion.OnAlarmVibrationDisk -= MainMotion_AlarmVibrationDisk;
|
||
//_mainMotion.OnAlarmVibrationDisk += MainMotion_AlarmVibrationDisk;
|
||
// PrepareLightIndexes();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 挡料电机操作
|
||
/// true: 顺时针
|
||
/// False: 逆时针
|
||
/// </summary>
|
||
/// <param name="u"></param>
|
||
public void FeedingMotor( bool direction)
|
||
{
|
||
|
||
|
||
// 设置最大等待时间,假设为 3 秒
|
||
int timeout = 3000;
|
||
int elapsedTime = 0;
|
||
int checkInterval = 100; // 每次检查等待 100ms
|
||
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点");
|
||
if (pLCItem == null)
|
||
return;
|
||
PLCItem zerospeeditem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点速度");
|
||
if (zerospeeditem == null)
|
||
return;
|
||
|
||
PLCItem CunSpeed = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机速度");
|
||
if (CunSpeed == null)
|
||
return;
|
||
|
||
PLCItem CunClockwiseItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机顺时针");
|
||
if (CunClockwiseItem == null)
|
||
return;
|
||
|
||
PLCItem CunCounterclockwiseItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机逆时针");
|
||
if (CunCounterclockwiseItem == null)
|
||
return;
|
||
|
||
PLCItem CunPosItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机位置");
|
||
if (CunPosItem == null)
|
||
return;
|
||
|
||
string CunToZero = pLCItem.Type + pLCItem.Address;
|
||
string CunToZeroSpeed = zerospeeditem.Type + zerospeeditem.Address;
|
||
string CunSpeedadress = CunSpeed.Type + CunSpeed.Address;
|
||
string CunClockwise = CunClockwiseItem.Type + CunClockwiseItem.Address;
|
||
string CunCounterclockwise = CunCounterclockwiseItem.Type + CunCounterclockwiseItem.Address;
|
||
string CunPos = CunPosItem.Type + CunPosItem.Address;
|
||
short zerospeed = (short)Convert.ToInt32(zerospeeditem.Value);
|
||
short cunSpeed = (short)Convert.ToInt32(CunSpeed.Value);
|
||
short u = (short)Convert.ToInt32(CunPosItem.Value);
|
||
// WriteBool(CountToZero, true);
|
||
|
||
// 检查是否不在原点,如果不在,则回原点
|
||
if (!ReadBool(CunToZero))
|
||
{
|
||
WriteShort(CunToZeroSpeed, (short)zerospeed); // 速度
|
||
Thread.Sleep(10);
|
||
// 发送回原点指令
|
||
WriteBool(CunToZero, true);
|
||
Thread.Sleep(1000); // 给设备一些时间响应
|
||
|
||
// 等待回到原点
|
||
while (!ReadBool(CunToZero))
|
||
{
|
||
if (elapsedTime >= timeout)
|
||
{
|
||
break;
|
||
}
|
||
|
||
Thread.Sleep(checkInterval);
|
||
elapsedTime += checkInterval;
|
||
}
|
||
}
|
||
|
||
// 无论是刚回到原点还是已经在原点,执行目标位置、速度和方向设置
|
||
|
||
|
||
|
||
WriteShort(CunSpeedadress, (short)cunSpeed);
|
||
Thread.Sleep(2000);
|
||
string dir = string.Empty;
|
||
if (direction)
|
||
{
|
||
WriteBool(CunClockwise, true); // 顺时针转动
|
||
dir = "顺时针";
|
||
}
|
||
else
|
||
{
|
||
WriteBool(CunCounterclockwise, true); // 逆时针转动
|
||
dir = "逆时针";
|
||
}
|
||
Thread.Sleep(10);
|
||
WriteShort(CunPos, (short)u); // 目标位置
|
||
Thread.Sleep(2000);
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 计数清零
|
||
/// </summary>
|
||
public void CountToZero()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "计数清零");
|
||
if (pLCItem == null)
|
||
return;
|
||
string CountToZero = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(CountToZero, true);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void RedLight(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "指示灯红");
|
||
if (pLCItem == null)
|
||
return;
|
||
string RedLight = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(RedLight, b);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void GreenLight(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "指示灯绿");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Light = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(Light, b);
|
||
// WriteBool(IIConfig.GreenLight, b);
|
||
Thread.Sleep(10);
|
||
|
||
}
|
||
|
||
public void YellowLight(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "指示灯黄");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Light = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(Light, b);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void Buzzer(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "蜂鸣器");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Light = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(Light, b);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void Belt(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "皮带");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Light = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(Light, b);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
|
||
public void Workstation1Pulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "工位1");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Workstation1Pulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse=Convert.ToInt32(pLCItem.Value);
|
||
|
||
string result = Regex.Replace(Workstation1Pulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(Workstation1Pulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void Workstation2Pulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "工位2");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Workstation1Pulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse=Convert.ToInt32(pLCItem.Value);
|
||
string result = Regex.Replace(Workstation1Pulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(Workstation1Pulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
public void Workstation3Pulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "工位3");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Workstation1Pulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse = Convert.ToInt32(pLCItem.Value);
|
||
|
||
string result = Regex.Replace(Workstation1Pulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(Workstation1Pulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
public void Workstation4Pulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "工位4");
|
||
if (pLCItem == null)
|
||
return;
|
||
string Workstation1Pulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse = Convert.ToInt32(pLCItem.Value);
|
||
string result = Regex.Replace(Workstation1Pulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(Workstation1Pulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void Workstation5Pulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "工位5");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
string Workstation1Pulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse = Convert.ToInt32(pLCItem.Value);
|
||
string result = Regex.Replace(Workstation1Pulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(Workstation1Pulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
|
||
|
||
public void ProjectValue()
|
||
{
|
||
PLCItem pLCItemmax = PLCItemList.FirstOrDefault(u => u.Name == "工件最大值");
|
||
if (pLCItemmax == null)
|
||
return;
|
||
PLCItem pLCItemmin = PLCItemList.FirstOrDefault(u => u.Name == "工件最小值");
|
||
if (pLCItemmin == null)
|
||
return;
|
||
int productMax =Convert.ToInt32( pLCItemmax.Value);
|
||
int productMin = Convert.ToInt32( pLCItemmin.Value);
|
||
|
||
string ProductMax = pLCItemmax.Type + pLCItemmax.Address;
|
||
string ProductMin = pLCItemmin.Type + pLCItemmin.Address;
|
||
WriteShort(ProductMax, (short)productMax);
|
||
Thread.Sleep(10);
|
||
WriteShort(ProductMin, (short)productMin);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void OKPulse()
|
||
{
|
||
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "OK脉冲");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
string OKPulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse =Convert.ToInt32( pLCItem.Value);
|
||
string result = Regex.Replace(OKPulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(OKPulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void NGPulse()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "NG脉冲");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
string NGPulse = pLCItem.Type + pLCItem.Address;
|
||
int Pulse=Convert.ToInt32(pLCItem.Value);
|
||
string result = Regex.Replace(NGPulse, @"\D", "");
|
||
int r = Convert.ToInt32(result) + 1;
|
||
result = "HD" + r.ToString();
|
||
short high = (short)(Pulse >> 16); // 高 16 位
|
||
short low = (short)(Pulse & 0xFFFF); // 低 16 位
|
||
WriteShort(result, high);
|
||
Thread.Sleep(10);
|
||
WriteShort(NGPulse, low);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
|
||
public void TurnClear(bool b)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "转盘清料");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
string TurnClear = pLCItem.Type + pLCItem.Address;
|
||
|
||
WriteBool(TurnClear, b);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
public void OpenHeartbeat(bool v)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "心跳功能");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
string Heartbeat = pLCItem.Type + pLCItem.Address;
|
||
|
||
WriteBool(Heartbeat, v);
|
||
Thread.Sleep(10);
|
||
|
||
|
||
}
|
||
|
||
public void Vibratory(bool v)
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "振动盘");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
|
||
string Vibratory = pLCItem.Type + pLCItem.Address;
|
||
WriteBool(Vibratory, v);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
|
||
public void ChuiQiTime()
|
||
{
|
||
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "吹气时间");
|
||
if (pLCItem == null)
|
||
return;
|
||
|
||
|
||
string ChuiQiTime = pLCItem.Type + pLCItem.Address;
|
||
short time = (short)Convert.ToInt32(pLCItem.Value);
|
||
WriteShort(ChuiQiTime, time);
|
||
Thread.Sleep(10);
|
||
}
|
||
|
||
}
|
||
}
|