You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
816 lines
28 KiB
816 lines
28 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using DBFactory;
|
|
using CommonLib;
|
|
using System.Data;
|
|
using System.Runtime.InteropServices;//20151120
|
|
namespace FINSTCPIP
|
|
{
|
|
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
using System.Text;
|
|
using CommonClassLib;
|
|
|
|
|
|
|
|
public static class CClientTCPIP
|
|
{//20130111
|
|
private static Object thisLock = new Object();
|
|
|
|
public static event CDataSourceChangeEventHandler DataChange;
|
|
public static event CUpdateDBEventHandler UpdateDB;
|
|
public static event CWriteDarkCasketEventHandler WriteDarkCasket;
|
|
public static event CSendDeviceOrderEventHandler SendDeviceOrder;
|
|
|
|
|
|
|
|
private static bool IsConnectionSuccessful = false;//20170722
|
|
private static Exception socketexception;
|
|
private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
|
|
|
|
|
public static void OnSendDeviceOrder(CSendDeviceOrderEventArgs e)
|
|
{
|
|
if (SendDeviceOrder != null)
|
|
{
|
|
SendDeviceOrder(null, e);
|
|
}
|
|
}
|
|
public static void OnWriteDarkCasket(CWriteDarkCasketEventArgs e)
|
|
{
|
|
if (WriteDarkCasket != null)
|
|
{
|
|
WriteDarkCasket(null, e);
|
|
}
|
|
}
|
|
public static void OnDataChange(CDataChangeEventArgs e)
|
|
{
|
|
if (DataChange != null)
|
|
{
|
|
DataChange(null, e);
|
|
}
|
|
}
|
|
public static void OnUpdateDB(CUpdateDBChangeEventArgs e)
|
|
{
|
|
if (UpdateDB != null)
|
|
{
|
|
UpdateDB(null, e);
|
|
}
|
|
}
|
|
public static event RefreshMonitorEventHandler RefreshMonitor;
|
|
public static void OnRefreshMonitor(RefreshMonitorEventArgs e)
|
|
{
|
|
if (RefreshMonitor != null)
|
|
{
|
|
RefreshMonitor(e);
|
|
}
|
|
}
|
|
public static DBOperator dbo =new DBOperator();//20130926
|
|
public static DBOperator dboM = CommonClassLib.AppSettings.dboM;//20130926
|
|
static StringBuilder sql = new StringBuilder();
|
|
private static Dictionary<string, int> _initCount = new Dictionary<string, int>();
|
|
public static Dictionary<string, int> FINSTcpIPInitCount
|
|
{
|
|
get { return CClientTCPIP._initCount; }
|
|
set
|
|
{
|
|
DataView dv = dbo.ExceSQL("SELECT F_RemoteIP,F_RemotePort FROM T_Base_Device WHERE (F_DeviceKindIndex = 44 and F_RemoteIP is not null)").Tables[0].DefaultView;
|
|
for (int i = 0; i < dv.Count; i++)
|
|
{
|
|
|
|
if (value.ContainsKey(dv[i][0].ToString()) == false)
|
|
{
|
|
value.Add(dv[i][0].ToString(), 0);
|
|
}
|
|
|
|
}
|
|
CClientTCPIP._initCount = value;
|
|
|
|
}
|
|
}
|
|
static Dictionary<string, bool> _ifInit = new Dictionary<string, bool>();
|
|
public static Dictionary<string, bool> IfInit
|
|
{
|
|
get { return CClientTCPIP._ifInit; }
|
|
set
|
|
{
|
|
DataView dv = dbo.ExceSQL("SELECT F_RemoteIP,F_RemotePort FROM T_Base_Device WHERE (F_DeviceKindIndex = 6 and F_RemoteIP is not null)").Tables[0].DefaultView;
|
|
for (int i = 0; i < dv.Count; i++)
|
|
{
|
|
if (_ifConnect.ContainsKey(dv[i][0].ToString()) == false)
|
|
{
|
|
_ifConnect.Add(dv[i][0].ToString(), false);
|
|
}
|
|
if (value.ContainsKey(dv[i][0].ToString()) == false)
|
|
{
|
|
value.Add(dv[i][0].ToString(), false);
|
|
}
|
|
if (value[dv[i][0].ToString()] == false)
|
|
{
|
|
_ifConnect[dv[i][0].ToString()] = false;
|
|
|
|
}
|
|
}
|
|
CClientTCPIP._ifInit = value;
|
|
}
|
|
}
|
|
|
|
static Dictionary<string, bool> _ifConnect = new Dictionary<string, bool>();
|
|
static string _remoteIP;
|
|
static int _remotePort;
|
|
static Socket clientSocket = null;
|
|
static EndPoint epServer;
|
|
static byte[] _byteData = new byte[6];
|
|
static string _tcpError;
|
|
static Dictionary<string, Socket> clientSockets = new Dictionary<string, Socket>();
|
|
public static string TcpError
|
|
{
|
|
get { return _tcpError; }
|
|
set
|
|
{
|
|
_tcpError = value;
|
|
RefreshMonitorEventArgs rea = new RefreshMonitorEventArgs("tsStatus", string.Format("SocketsTCPIP:{0}", _tcpError));
|
|
OnRefreshMonitor(rea);
|
|
}
|
|
}
|
|
public static byte[] ByteData
|
|
{
|
|
get { return _byteData; }
|
|
set { _byteData = value; }
|
|
}
|
|
|
|
public static Socket ClientSocket
|
|
{
|
|
get { return CClientTCPIP.clientSocket; }
|
|
set { CClientTCPIP.clientSocket = value; }
|
|
}
|
|
private static byte[] GetSocketIOControl()//设置Socket的
|
|
{//20151120张磊调整,设置检测对方网线断开等硬件故障的时间间隔
|
|
uint dummy = 0;
|
|
byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
|
|
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//启用
|
|
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//多长时间后开始第一次探测ms
|
|
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);//探测时间间隔ms
|
|
return inOptionValues;
|
|
}
|
|
public static bool IfConnectServer(string remoteIP)//20170710判断是否连接到AGV控制台
|
|
{
|
|
try
|
|
{
|
|
if (IfInit.ContainsKey(remoteIP) == false)
|
|
{
|
|
return false;
|
|
}
|
|
if (IfInit[remoteIP] == false)
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
else
|
|
{
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 查看指定IP是否连接
|
|
public static bool CheckConnect(string IP)
|
|
{
|
|
if (_ifConnect.ContainsKey(IP))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int GetDeviceKindIdx(int devIdx)
|
|
{
|
|
try
|
|
{
|
|
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_DeviceIndex, F_DeviceKindIndex FROM T_Base_Device WHERE F_DeviceIndex=" + devIdx);
|
|
|
|
DataSet ds = dbo.ExceSQL(sql.ToString());
|
|
DataView dv = ds.Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dv[0]["F_DeviceKindIndex"]);
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return 0;
|
|
//throw ex;
|
|
}
|
|
}
|
|
public static int GetManageTaskIndexfromMonitor(int monitorIdx)
|
|
{
|
|
//20100127
|
|
DataView dv;
|
|
try
|
|
{//20100127
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_ManageTaskIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = ").Append(monitorIdx).Append(")");
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dv[0]["F_ManageTaskIndex"]);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//20100127
|
|
//throw ex;
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dv = null;
|
|
}
|
|
}
|
|
public static int GetManageTaskKindIndexFromMonitor(int monitorIdx)
|
|
{
|
|
//20100127
|
|
DataView dv;
|
|
try
|
|
{//20100127
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_ManageTaskKindIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = ").Append(monitorIdx).Append(")");
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
//throw ex;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dv = null;
|
|
}
|
|
}
|
|
public static int GetDeviceOrderFromMonitor(int MonitorIndex)
|
|
{
|
|
//20100127
|
|
DataView dv;
|
|
try
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("select F_DeviceCommandIndex from T_Monitor_Task where (F_DeviceCommandIndex IS NOT NULL) and F_MonitorIndex=").Append(MonitorIndex);
|
|
|
|
//20100127
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dv[0]["F_DeviceCommandIndex"]);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
//throw ex;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dv = null;
|
|
}
|
|
}
|
|
public static int GetExceptionNOFromManageTask(int FID, int ManTaskKind)
|
|
{
|
|
//20100127
|
|
DataTable dt;
|
|
try
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT FID, F_ManageTaskKindIndex, FExceptionNO FROM T_Manage_Task WHERE (FID = ").Append(FID).Append(") AND (F_ManageTaskKindIndex = ").Append(ManTaskKind).Append(") ");
|
|
;
|
|
//20100127
|
|
dt = dbo.ExceSQL(sql.ToString()).Tables[0];
|
|
if (dt.Rows.Count > 0)
|
|
{
|
|
if (dt.Rows[0]["FExceptionNO"] == DBNull.Value)
|
|
{
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
|
|
return Convert.ToInt32(dt.Rows[0]["FExceptionNO"]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//20100127
|
|
//throw ex;
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dt = null;
|
|
}
|
|
|
|
}
|
|
static int GetDeviceIndexFromAgvActive(int AgvActive)
|
|
{
|
|
//20100127AGV
|
|
DataView dva;
|
|
try
|
|
{
|
|
//20100127
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_AGVGateDeviceIndex, F_Address FROM T_Base_AGV_Gate WHERE (F_Active = ").Append(AgvActive).Append(") ");
|
|
dva = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dva.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dva[0]["F_AGVGateDeviceIndex"]);
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{//20100127
|
|
//throw ex;
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dva = null;
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="AgvAddress"></param>
|
|
/// <param name="ForkIndex">1,前叉;2后叉;3双叉</param>
|
|
/// <returns></returns>
|
|
static int GetDeviceIndexFromAgvAddress(int AgvAddress, int ForkIndex)
|
|
{
|
|
//20110110AGV
|
|
DataView dva;
|
|
try
|
|
{
|
|
string F_UseAwayFork = string.Empty;
|
|
if (ForkIndex == 1)//前叉,远叉
|
|
{
|
|
F_UseAwayFork = " and F_UseAwayFork = '1' ";
|
|
}
|
|
else if (ForkIndex == 2)
|
|
{
|
|
F_UseAwayFork = " and F_UseAwayFork = '0' ";
|
|
}
|
|
else//3
|
|
{
|
|
F_UseAwayFork = " and F_UseAwayFork <> '-' ";
|
|
}
|
|
//20100127
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_AGVGateDeviceIndex, F_Address FROM T_Base_AGV_Gate WHERE (F_Address = ").Append(AgvAddress).Append(") ").Append(F_UseAwayFork);
|
|
dva = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dva.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dva[0]["F_AGVGateDeviceIndex"]);
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//20100127
|
|
//throw ex;
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
}
|
|
finally
|
|
{//20100127
|
|
dva = null;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 是否逻辑有物
|
|
/// </summary>
|
|
/// <param name="deviceindex"></param>
|
|
/// <returns></returns>
|
|
static bool IfHaveLogicGoods(int deviceindex)
|
|
{
|
|
DataView dv = dbo.ExceSQL("SELECT F_HaveGoods FROM T_Base_Device where F_HaveGoods=1 and F_DeviceIndex=" + deviceindex).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
static int GetCorrDeviceIndex(int device)
|
|
{//20110412
|
|
int deviceCorr = 0;
|
|
if ((device % 2) == 1)
|
|
{
|
|
deviceCorr = device - 1;
|
|
}
|
|
else
|
|
{
|
|
deviceCorr = device + 1;
|
|
}
|
|
return deviceCorr;
|
|
}
|
|
static string GetDeviceS7Connection(int deviceindex)
|
|
{
|
|
DataView dv = dbo.ExceSQL("SELECT F_S7Connection FROM T_Base_Device WHERE (F_DeviceIndex = " + deviceindex + ")").Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return dv[0]["F_S7Connection"].ToString();
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 20110405数据视图更新
|
|
/// </summary>
|
|
public static void DataSourceChange()
|
|
{
|
|
//sql.Remove(0, sql.Length);
|
|
//sql.Append("select * from V_Monitor_Task where ").Append(CStaticClass.Monstatus).Append(" order by 设备指令索引 asc ");
|
|
//DataView dvmon = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
//sql.Remove(0, sql.Length);
|
|
//sql.Append("select * from V_Manage_Task where ").Append(CStaticClass.Manstatus);
|
|
//DataView dvman = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
CDataChangeEventArgs e = new CDataChangeEventArgs(null, null);
|
|
OnDataChange(e);
|
|
}
|
|
/// <summary>
|
|
/// 20110608执行SQL语句数组
|
|
/// </summary>
|
|
/// <param name="sqlList"></param>
|
|
public static void SQLString(DBOperator dbo, StringBuilder sql)
|
|
{
|
|
|
|
CUpdateDBChangeEventArgs e = new CUpdateDBChangeEventArgs(dbo, sql);
|
|
OnUpdateDB(e);
|
|
}
|
|
/// <summary>
|
|
/// 20110608报告完成
|
|
/// </summary>
|
|
/// <param name="device"></param>
|
|
/// <param name="task"></param>
|
|
/// <param name="taskstate"></param>
|
|
public static void ActionChange(int device, int task, int taskstate)
|
|
{
|
|
CUpdateDBChangeEventArgs e = new CUpdateDBChangeEventArgs(device, task, taskstate);
|
|
OnUpdateDB(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 20150724报告AGV错误
|
|
/// </summary>
|
|
/// <param name="device"></param>
|
|
/// <param name="AGVNumber"></param>
|
|
/// <param name="deviceState"></param>
|
|
public static void ActionError(int device, byte[] splitbyteValue, byte[] deviceState)
|
|
{
|
|
CUpdateDBChangeEventArgs e = new CUpdateDBChangeEventArgs(device, splitbyteValue, deviceState);
|
|
OnUpdateDB(e);
|
|
}
|
|
/// <summary>
|
|
/// 判断调度表T_Monitor_Task是否存在调度任务的设备和命令
|
|
/// </summary>
|
|
/// <param name="Mankind">调度任务类型</param>
|
|
/// <param name="ManFID">调度任务索引</param>
|
|
/// <param name="DeviceIndex">设备所引</param>
|
|
/// <param name="Order">设备命令</param>
|
|
/// <returns></returns>
|
|
public static bool DeviceAndOrderExitInMonitor(int Mankind, int ManFID, int DeviceIndex, int Order, int ArrowAddress)
|
|
{
|
|
DataView dv;
|
|
try
|
|
{
|
|
if (Order == -1) return true;
|
|
sql.Remove(0, sql.Length);
|
|
switch (GetDeviceKindIdx(DeviceIndex))
|
|
{
|
|
case 1://堆垛机
|
|
sql.Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = ").Append(ManFID).Append(")").Append(
|
|
" AND (F_ManageTASKKINDINDEX = ").Append(Mankind).Append(") AND (F_DeviceIndex = ").Append(DeviceIndex).Append(")").Append(
|
|
" AND (F_DeviceCommandIndex = ").Append(Order).Append(")");
|
|
break;
|
|
case 2://输送机
|
|
sql.Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = ").Append(ManFID).Append(")").Append(
|
|
" AND (F_ManageTASKKINDINDEX = ").Append(Mankind).Append(") AND (F_DeviceIndex = ").Append(DeviceIndex).Append(")").Append(
|
|
" AND (F_DeviceCommandIndex = ").Append(Order).Append(")");
|
|
break;
|
|
case 4://RGV
|
|
sql.Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = ").Append(ManFID).Append(")").Append(
|
|
" AND (F_ManageTASKKINDINDEX = ").Append(Mankind).Append(") AND (F_DeviceIndex = ").Append(DeviceIndex).Append(")").Append(
|
|
" AND (F_DeviceCommandIndex = ").Append(Order).Append(") and F_NumParam1=").Append(ArrowAddress);
|
|
break;
|
|
case 6://AGV
|
|
sql.Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = ").Append(ManFID).Append(")").Append(
|
|
" AND (F_ManageTASKKINDINDEX = ").Append(Mankind).Append(") AND (F_DeviceIndex = ").Append(DeviceIndex).Append(")").Append(
|
|
" AND (F_DeviceCommandIndex = ").Append(Order).Append(")");
|
|
break;
|
|
default:
|
|
sql.Append("SELECT F_MonitorIndex FROM T_Monitor_Task WHERE (F_ManageTaskIndex = ").Append(ManFID).Append(")").Append(
|
|
" AND (F_ManageTASKKINDINDEX = ").Append(Mankind).Append(") AND (F_DeviceIndex = ").Append(DeviceIndex).Append(")").Append(
|
|
" AND (F_DeviceCommandIndex = ").Append(Order).Append(")");
|
|
break;
|
|
}
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TcpError = "DeviceAndOrderExitInMonitor:" + ex.StackTrace + ex.Message;
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
dv = null;
|
|
|
|
}
|
|
}
|
|
|
|
#region 将BYTE数组转换为UInt16数组
|
|
/// <summary>
|
|
/// 将BYTE数组转换为UInt16数组
|
|
/// </summary>
|
|
/// <param name="srcData">需要处理的byte数组</param>
|
|
/// /// <param name="bigEndian">处理的数据是大端</param>
|
|
/// <returns>返回UInt16数组</returns>
|
|
public static UInt16[] ConvertByteToUInt16(byte[] srcData, bool bigEndian = false)
|
|
{
|
|
byte[] chat = null;
|
|
|
|
//判断srcData数组的维数是否为奇数,如果为奇数则补齐最后一位
|
|
if (srcData.Length % 2 == 1)
|
|
{
|
|
chat = new byte[srcData.Length + 1];
|
|
Array.Copy(srcData, 0, chat, 0, srcData.Length);
|
|
|
|
}
|
|
else
|
|
{
|
|
chat = new byte[srcData.Length];
|
|
Array.Copy(srcData, 0, chat, 0, srcData.Length);
|
|
|
|
}
|
|
|
|
UInt16[] newData = new UInt16[chat.Length / 2];
|
|
|
|
int temp = 0;
|
|
for (int i = 0; i < newData.Length; i++)
|
|
{
|
|
if (bigEndian)
|
|
{
|
|
newData[i] = (UInt16)((chat[temp] << 8) + chat[temp + 1]);
|
|
}
|
|
else
|
|
{
|
|
newData[i] = (UInt16)((chat[temp + 1] << 8) + chat[temp]);//C#版:0是低位,1是高位;VC++版:1是低位,0是高位;
|
|
}
|
|
temp += 2;
|
|
}
|
|
|
|
return newData;
|
|
}
|
|
#endregion
|
|
|
|
|
|
public static int GetDeviceIndexFromMonitor(int MonitorIndex)
|
|
{
|
|
//20100108
|
|
DataView dv;
|
|
try
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("select F_DeviceIndex from T_Monitor_Task where (F_DeviceIndex IS NOT NULL) and F_MonitorIndex=").Append(MonitorIndex);
|
|
|
|
//20100108
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
return Convert.ToInt32(dv[0]["F_DeviceIndex"]);
|
|
}
|
|
else
|
|
{
|
|
int []chatnew = new int[1];
|
|
chatnew[0] = MonitorIndex;
|
|
CWriteDarkCasketEventArgs e = new CWriteDarkCasketEventArgs("SocketsTCPIP.CConnectTCPIP", "Receive", MonitorIndex.ToString(), "接受AGV信息处理失败,无该任务!", chatnew);
|
|
OnWriteDarkCasket(e);
|
|
return -1; // -1 修改bba20160622
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
dv = null;
|
|
}
|
|
}
|
|
|
|
public static int[] ConvertByteToInt32(byte[] srcData)
|
|
{
|
|
byte[] chat = null;
|
|
if (srcData.Length % 4 != 0)
|
|
{
|
|
int arrlen = srcData.Length - (srcData.Length % 4);
|
|
chat = new byte[arrlen];
|
|
Array.Copy(srcData, 0, chat, 0, arrlen);
|
|
|
|
}
|
|
else
|
|
{
|
|
chat = new byte[srcData.Length];
|
|
Array.Copy(srcData, 0, chat, 0, srcData.Length);
|
|
|
|
}
|
|
|
|
int[] newData = new int[chat.Length / 4];
|
|
|
|
int temp = 0;
|
|
for (int i = 0; i < newData.Length; i++)
|
|
{
|
|
newData[i] = (int)((chat[temp + 3] << 24) + (chat[temp + 2] << 16) + (chat[temp + 1] << 8) + chat[temp]);//C#版:0是低位,1是高位;VC++版:1是低位,0是高位;
|
|
temp += 4;
|
|
}
|
|
|
|
return newData;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int GetAgvSquence(int agvGate)
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT F_Sequence FROM T_Base_AGV_Gate WHERE (F_AGVGateDeviceIndex = ").Append(agvGate).Append(")");
|
|
object ob = dbo.GetSingle(sql.ToString());
|
|
if (ob != null)
|
|
{
|
|
return Convert.ToInt32(ob);
|
|
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public static int GetFCONTROLTASKTYPEFromManageTask(int taskKindIndex, int Managefid)
|
|
{
|
|
DataView dv;
|
|
try
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("SELECT FCONTROLTASKTYPE FROM T_Manage_Task WHERE (FID = ").Append(Managefid).Append(") AND (F_ManageTaskKindIndex = ").Append(taskKindIndex).Append(")");
|
|
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
if (dv[0]["FCONTROLTASKTYPE"] == DBNull.Value) return -1;
|
|
return Convert.ToInt32(dv[0]["FCONTROLTASKTYPE"]);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//throw ex;
|
|
TcpError = "SocketsTCPIP:GetDeviceKindIdx," + ex.StackTrace + ex.Message;
|
|
return -1;
|
|
}
|
|
finally
|
|
{
|
|
dv = null;
|
|
|
|
}
|
|
}
|
|
|
|
public static int GetAGVServer(string ipaddress)
|
|
{
|
|
//sql.Remove(0, sql.Length);
|
|
//sql.Append("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_RemoteIP = '").Append(ipaddress).Append("')");
|
|
string obj = dbo.GetSingle(string.Format("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_RemoteIP = '{0}')", ipaddress)).ToString();
|
|
if (obj == null)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
return Convert.ToInt32(obj);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// [0]:F_BindingDevice,[1]:F_ManageAskkind,[2]:F_WareHouse,[3]:F_Time,[4]:F_BarCode
|
|
/// </summary>
|
|
/// <param name="devIdx"></param>
|
|
/// <returns></returns>
|
|
public static string[] GetDeviceInfoFromPLCAsk(string devIdx)
|
|
{
|
|
//20100108
|
|
DataView dv;
|
|
string[] _Rstr = new string[5];
|
|
try
|
|
{
|
|
sql.Remove(0, sql.Length);
|
|
sql.Append("select * from T_Base_PLC_Ask where F_BindingDevice=").Append(Convert.ToInt32( devIdx));
|
|
;
|
|
//20100108
|
|
dv = dbo.ExceSQL(string.Format("select * from T_Base_PLC_Ask where F_BindingDevice=", devIdx)).Tables[0].DefaultView;
|
|
if (dv.Count > 0)
|
|
{
|
|
_Rstr[0] = Convert.ToString(dv[0]["F_BindingDevice"]);
|
|
_Rstr[1] = Convert.ToString(dv[0]["F_ManageAskkind"]);
|
|
_Rstr[2] = Convert.ToString(dv[0]["F_WareHouse"]);
|
|
_Rstr[3] = Convert.ToString(dv[0]["F_Time"]);
|
|
_Rstr[4] = Convert.ToString(dv[0]["F_BarCode"]);
|
|
|
|
return _Rstr;
|
|
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception )
|
|
{
|
|
return null;
|
|
//throw ex;
|
|
}
|
|
finally
|
|
{
|
|
dv = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|