using RGD.Common;
using RGD.DataService;
using RGD.DBUtility;
using RGD.OPCClient;
using System;
namespace RGD.WCS
{
///
/// Creator:rgd
/// 命令开关等全局变量的定义
///
public static class CStaticClass
{
//public static string[] TestLaneCell = new string[3];
//public static int[] TestLane = new int[3];
//public static bool StopTestLane = false;
//public static bool TestOut = true;
//public static bool TestIn = true;
//public static int[] testWorkstation = new int[48];
//public static bool IfRemind = true;
//public static System.Windows.Forms.Form FormMonitor = null;
//public static int gcCount = 0;
//public static Dictionary serialports = new Dictionary();
///
/// 日志黑匣子保存时间
///
public static string SaveDays = "30";//20110603
///
/// CParsePLCData数据解析 时间标识
///
public static DateTime routetime = DateTime.Now;
///
/// CParsePLCData数据解析 时间标识
///
public static DateTime reporttime = DateTime.Now;
public static Model.MDevice devinfo28;
public static Model.MDevice devinfo;
public static ISendDeviceOrder sdo;
public static IGetDeviceState gds;
#region 公共参数
private static string _userID;
///
/// 用户编号
///
public static string UserID
{
get { return CStaticClass._userID; }
set { CStaticClass._userID = value; }
}
private static string _movedDeviceAheadTrigger = "1";
///
/// 移动设备是否被提前触发将取
///
public static string MovedDeviceAheadTrigger
{
get { return CStaticClass._movedDeviceAheadTrigger; }
set
{
if ((value == "0") || (value == "1"))
{
CStaticClass._movedDeviceAheadTrigger = value;
}
else
{
CStaticClass._movedDeviceAheadTrigger = "1";
}
}
}
private static string _outDetectArrowIdleGoods = "1";
///
/// 堆垛机出库是否检测目标输送机空闲和有物
///
public static string OutDetectArrowIdleGoods
{
get { return CStaticClass._outDetectArrowIdleGoods; }
set
{
if ((value == "0") || (value == "1"))
{
CStaticClass._outDetectArrowIdleGoods = value;
}
else
{
CStaticClass._outDetectArrowIdleGoods = "1";
}
}
}
private static string _deviceErrorAutoModifyRoutePath = "0";
///
/// 设备发生故障自动变更路径
///
public static string DeviceErrorAutoModifyRoutePath
{
get { return CStaticClass._deviceErrorAutoModifyRoutePath; }
set
{
if ((value == "0") || (value == "1"))
{
CStaticClass._deviceErrorAutoModifyRoutePath = value;
}
else
{
CStaticClass._deviceErrorAutoModifyRoutePath = "0";
}
}
}
private static string _doubleForkWaitTime = "120";
///
/// 双叉关联入库任务允许等待的时间单位(秒):默认120秒
///
public static string DoubleForkWaitTime
{
get { return CStaticClass._doubleForkWaitTime; }
set
{
if (Microsoft.VisualBasic.Information.IsNumeric(_doubleForkWaitTime) == true)
{
CStaticClass._doubleForkWaitTime = value;
}
else
{
CStaticClass._doubleForkWaitTime = "120";
}
}
}
private static string _agvAllowSingleFork = "0";
///
/// AGV是否允许单叉动作,:默认双叉一起动作 0
///
public static string AGVAllowSingleFork
{
get { return CStaticClass._agvAllowSingleFork; }
set
{
if ((value == "0") || (value == "1"))
{
CStaticClass._agvAllowSingleFork = value;
}
else
{
CStaticClass._agvAllowSingleFork = "0";
}
}
}
private static bool _Order;
///
/// 命令开关是否打开
///
public static bool Order
{
get { return CStaticClass._Order; }
set { CStaticClass._Order = value; }
}
private static string _routeSearchMode = "0";
///
/// 路径搜索方式:0,按照最优路径选择固定路线;1,每个正在工作的节点作为起始点,向末端节点重新搜索下一个最优路径的节点
///
public static string RouteSearchMode
{
get { return CStaticClass._routeSearchMode; }
set
{
if ((value == "0") || (value == "1"))
{
CStaticClass._routeSearchMode = value;
}
else
{
CStaticClass._routeSearchMode = "0";
}
}
}
private static int _MessageIndex;
///
/// 监控发送消息索引
///
public static int MessageIndex
{
get { return CStaticClass._MessageIndex; }
set
{
if (value > 240)
{
value = 1;
}
CStaticClass._MessageIndex = value;
}
}
#endregion 公共参数
#region OPC加载初始化
public static void ReConnect()
{
OPCClient.CCommonOPCClient.ConnectCount = 0;
}
#endregion OPC加载初始化
#region 任务队列与指令队列相关
private static string _manstatus = " F_Status<>-1 ";//默认,显示调度任务全部队列
private static string _monstatus = " F_Status<>-1 ";//默认,显示设备指令全部队列
private static bool _realRefresh = true;
///
/// 显示调度任务全部队列" F_Status<>-1 ";等待队列" F_Status=0 ";运行队列" F_Status=1 ";
///
public static string Manstatus
{
get { return CStaticClass._manstatus; }
set { CStaticClass._manstatus = value; }
}
///
/// 显示设备指令全部队列" F_Status<>-1 ";等待队列" F_Status=0 ";运行队列" F_Status=1 ";
///
public static string Monstatus
{
get { return CStaticClass._monstatus; }
set { CStaticClass._monstatus = value; }
}
///
/// 是否实时刷新监控和调度任务列表
///
public static bool RealRefresh
{
get { return CStaticClass._realRefresh; }
set { CStaticClass._realRefresh = value; }
}
#endregion 任务队列与指令队列相关
#region OPC数据操作--获取所有数据--按条件获取部分--
public static int[] AllReturns;
public static bool GetAllReturnFlag = false;
///
/// 取得所有光电开关的信号 持续循环
///
public static void GetAllReturns()
{
try
{
GetAllReturnFlag = true;
if (devinfo28.DeviceIndex == 11001)
{
return;
}
gds = CommModeCreate.CreateGetDeviceState(devinfo28.DeviceIndex);
AllReturns = gds.GetDeviceState(devinfo28.DeviceIndex);
//AllReturns = new int[100] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
if (AllReturns == null)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得所有光电开关的信号时:" + gds.CommLayerError) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得所有光电开关的信号时:" + gds.CommLayerError, true);
}
}
//Thread.Sleep(500);//探物与完成信号错位
//LogUtil.WriteLog("", "执行GetAllReturns:"+ AllReturns.Length);
GetAllReturnFlag = false;
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得所有光电开关的信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得所有光电开关的信号时:" + ex.Message, true);
}
return;
}
}
///
/// 获取拆叠盘完成信号
///
/// 设备编号
///
public static int GetOverlapStatus(int deviceIdx)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
#region 叠盘完成信号
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
int flag = CStaticClass.AllReturns[temp];//叠盘完成标志
int sendouttype = CStaticClass.AllReturns[temp + 1];//申请离开类型,拆盘2单次完成,3整垛完成
// return 3; // 测试时,自动报叠盘完成
if (flag == 0 || sendouttype < 3)// || boxnumber == 0)
{
return 0;
}
else
{
return sendouttype;
}
#endregion 叠盘完成信号
}
else
{
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得叠盘完成信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得叠盘完成信号时:" + ex.Message, true);
}
return 0;
}
}
///
/// 获取拆盘箱号 应该是计数
///
///
///
public static int GetboxNum(int deviceIdx)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
#region 拆盘箱号
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
int boxNum = (CStaticClass.AllReturns[temp + 4] << 8) + CStaticClass.AllReturns[temp + 5];
return boxNum;
#endregion 拆盘箱号
}
else
{
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得叠盘完成信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得叠盘完成信号时:" + ex.Message, true);
}
return 0;
}
}
///
/// 获得拆盘任务号
///
///
///
public static int Gettaskno(int deviceIdx)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
#region 拆盘任务号
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
int taskno = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];
return taskno;
#endregion 拆盘任务号
}
else
{
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得叠盘完成信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得叠盘完成信号时:" + ex.Message, true);
}
return 0;
}
}
///
/// 获取扫码头 是否完成信号 到达视觉前
///
///
///
public static int GetScannerStatus(int deviceIdx)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
#region 扫码完成
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
int flag = CStaticClass.AllReturns[temp];//扫码完成标志
return flag;
#endregion 扫码完成
}
else
{
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得扫码完成信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得扫码完成信号时:" + ex.Message, true);
}
return 0;
}
}
///
/// 获取扫码头 数据 拿到箱号
///
///
///
public static string GetStringData(int deviceIdx)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
if (devinfo.Dbw2Address != -1)
{
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
decimal glen = devinfo.Dbw2Getlength;
string strdata = "";
string headstr = CStaticClass.AllReturns[temp].ToString();
if (headstr != "2") return null;
switch (devinfo.DeviceKind.ToString())
{
#region 字符串信息
case "7"://条码扫描器
for (int i = 1; i <= 12; i++)//20110401
{
strdata += Convert.ToChar(CStaticClass.AllReturns[temp + i + 1]);
if (strdata == "?")
{
return "";
}
}
strdata = strdata.ToUpper();
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), strdata);
break;
#endregion 字符串信息
}
return strdata;
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("CStaticClass取得设备字符串信息时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "CStaticClass取得设备字符串信息时:" + ex.Message, true);
}
return null;
}
}
///
/// 货物探测标识 获取某输送线是否有物
///
///
///
public static int GetDevicePhotoelectric(string devicePhotoelectricBit)
{
try
{
if (devinfo28.DeviceKind != 28)
{
return -1;
}
if (CStaticClass.AllReturns == null)
{
return -1;
}
#region 获取开关量的值
char[] dd = new char[1] { '.' };
string[] DS = devicePhotoelectricBit.Split(dd);
int deviceIdx = 0; int devbit = 0;
int.TryParse(DS[0], out deviceIdx);
int.TryParse(DS[1], out devbit);
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
if (devinfo == null)
{
return -1;
}
int glen = 0;
int IfVis = 0;
if (devinfo.SplitByte != -1)
{
int temp = devinfo.SplitByte - devinfo28.Dbw2Address;
temp += devbit / 8;
glen = devbit % 8;
if (temp >= 0)
{
IfVis = CCarryConvert.GetBitFromInteger(CStaticClass.AllReturns[temp], glen);
return IfVis;
}
else
{
return -1;
}
}
else
{
return -1;
}
#endregion 获取开关量的值
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得指定光电开关的信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得指定光电开关的信号时:" + ex.Message, true);
}
return -1;
}
}
///
/// 发送命令前检测 根据设备号和bit位,取得F_SplitByte里的光电bit值:0,1
///
///
///
///
public static int GetDevicePhotoelectric(int deviceIdx, int abit)
{
try
{
if (devinfo28.DeviceKind == 28)
{
if (CStaticClass.AllReturns != null)
{
#region 获取开关量的值
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
int glen = 0;
int IfVis = 0;
if (devinfo.SplitByte != -1)
{
int temp = devinfo.SplitByte - devinfo28.Dbw2Address;
if (abit > 7)
{
glen = abit - 8;
temp = temp + 1;
}
else
{
glen = abit;
}
if (temp >= 0)
{
IfVis = CCarryConvert.GetBitFromInteger(CStaticClass.AllReturns[temp], glen);
//if (dvle[0]["F_DeviceKindIndex"].ToString() == "18")
//{//配合孙宇光电开关0有探物,1无探物,在这里取反符合正常逻辑
// if (IfVis == 1)
// {
// IfVis = 0;
// }
// else
// {
// IfVis = 1;
// }
//}
return IfVis;
}
else
{
return -1;
}
}
else
{
return -1;
}
#endregion 获取开关量的值
}
else
{
return -1;
}
}
else
{
return -1;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("取得指定光电开关的信号时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "取得指定光电开关的信号时:" + ex.Message, true);
}
return -1;
}
}
///
/// 取得指定设备索引的设备状态:[0]读写标志;[1]状态;[2]任务号;[3]X坐标;[4]Y坐标;[5]设备索引
///
/// 设备索引
///
public static int[] GetDeviceState(int deviceIdx)
{
int[] states = new int[6];
//string headstr;
string[] wv = new string[1] { "2" };
string[] witemnames;
try
{
if (devinfo28.DeviceKind == 28)//虚拟设备 数据存放区
{
if (CStaticClass.AllReturns != null)
{
#region 获取设备状态
devinfo = BaseDeviceService.GetDeviceInfo(deviceIdx);
if (devinfo.Dbw2Address != -1)
{
int temp = devinfo.Dbw2Address - devinfo28.Dbw2Address;
decimal glen = devinfo.Dbw2Getlength;
states = new int[6];
wv = new string[1] { "2" };
witemnames = new string[1] { "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0)) };
sdo = CommModeCreate.CreateSendDeviceOrder(deviceIdx);
switch (devinfo.DeviceKind.ToString())
{
#region
case "1":
#region 堆垛机
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];//任务号
states[3] = CStaticClass.AllReturns[temp + 7] + (CStaticClass.AllReturns[temp + 6] << 8) + (CStaticClass.AllReturns[temp + 5] << 16) + (CStaticClass.AllReturns[temp + 4] << 32);//X坐标
states[4] = CStaticClass.AllReturns[temp + 11] + (CStaticClass.AllReturns[temp + 10] << 8) + (CStaticClass.AllReturns[temp + 9] << 16) + (CStaticClass.AllReturns[temp + 8] << 32);//Y坐标
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)//返回信息与内存不一致 需要更新
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
DbHelperSQL.Query("update t_base_device set f_currentlocation='" + states[3] + "," + states[4] + "' where f_deviceindex=" + deviceIdx);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion 堆垛机
case "2":
#region 输送机
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];//任务号
states[3] = 0;
states[4] = 0;
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion 输送机
case "34":
#region AGV
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];//任务号
states[3] = 0;
states[4] = 0;
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion AGV
case "5":
#region 拆盘机和叠盘机
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = 0;
states[3] = 0;
states[4] = 0;
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion 拆盘机和叠盘机
case "32":
#region 拣选模组
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];//任务号
states[3] = CStaticClass.AllReturns[temp + 4]; ;//完成位置
states[4] = 0;
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion 拣选模组
case "33":
#region 贴标机
states[0] = CStaticClass.AllReturns[temp];//读写标志
states[1] = CStaticClass.AllReturns[temp + 1];//状态
states[2] = (CStaticClass.AllReturns[temp + 2] << 8) + CStaticClass.AllReturns[temp + 3];//任务号
states[3] = CStaticClass.AllReturns[temp + 5];//贴标位置
states[4] = (CStaticClass.AllReturns[temp + 6] << 8) + CStaticClass.AllReturns[temp + 7];
states[5] = deviceIdx;//设备号索引
if (IsEquals(devinfo.ReturnMessage, states, 3) == false)
{
devinfo.ReturnMessage = states;
BaseDeviceService.SetDeviceMessage(devinfo);
string sss = "";
for (int j = 0; j <= 5; j++)
{
sss += "**" + states[j].ToString();
}
CCarryConvert.WriteDarkCasket("OPCClient", "CGetDeviceState", deviceIdx.ToString(), sss);
}
break;
#endregion 贴标机
#endregion 获取设备状态
}
return states;
}
else
{
return null;
}
#endregion OPC数据操作--获取所有数据--按条件获取部分--
}
else
{
return null;
}
}
else
{
return null;
}
}
catch (Exception ex)
{
if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("CStaticClass取得指定设备状态时:" + ex.Message) < 0)
{
FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "CStaticClass取得指定设备状态时:" + ex.Message, true);
}
return null;
}
finally
{
states = null;
wv = null;
witemnames = null;
}
}
#endregion
#region 公共方法
///
/// 比较两个值型一维数组变量是否值相等
///
/// 值型一维数组1
/// 值型一维数组2
/// 比较结果,相等则true,否则false
public static bool IsEquals(Array array1, Array array2, int arraylength)
{
//新增加arraylength
//比较类型是否一样
if ((array1 == null) || (array2 == null)) return false;
if (!Object.ReferenceEquals(array1.GetType(), array2.GetType()))
{
return false;
}
//比较长度是否一样
if (array1.GetLength(0) != array2.GetLength(0))
{
return false;
}
//比较成员是否对应相等
ValueType v1, v2;
for (int i = 0; i < arraylength; i++)
{
v1 = (ValueType)array1.GetValue(i);
v2 = (ValueType)array2.GetValue(i);
if (!v1.Equals(v2))
{
return false;
}
}
return true;
}
#endregion
}
}