using System;
using System.Collections.Generic;
using System.Text;
using DBFactory;
using ICommLayer;
using System.Data;
using Microsoft.VisualBasic;
namespace OPCClient
{
    public class CGetDeviceState:IGetDeviceState
    {
        string _commLayerError;

        public string CommLayerError
        {
            get { return _commLayerError; }
            set { _commLayerError = value; }
        }
        DBOperator dbo = CCommonOPCClient.dbo;
        public CGetDeviceState()
        {
            dbo.Open();
        }
        /// <summary>
        ///1、下位机给上位机发送设备(任务)状态信息
        ///检测开关:  0-无货1-有货
        ///确认按钮: 
        ///第1字节:
        ///一个变化的值,每确认一次,该值变化一次,以标志一个
        ///新的确认消息。例如可以是0、1交替变化,也可以是递
        ///增变化。
        ///第2字节: 
        ///1-无货确认  
        ///2-有货确认
        ///其他设备:
        ///第1字	节:设备状态
        ///堆垛机:0-空闲
        ///1-正在运行
        ///2-任务完成
        ///3-要求调度重发命令
        ///4-通知调度取消当前任务
        ///其它值表示错误
        /// 穿梭车:	
        /// 0-空闲
        /// 1-正在运行
        /// 2-完成
        /// 3-要求调度重发命令
        /// 4-通知调度取消当前任务
        /// 其它值表示错误
        /// 输送机:
        /// 0-空闲
        /// 1-正在运行	
        /// 2-完成(只有PC下发的目标设备才允许汇报完成)
        /// 组合机构需要单独定义状态
        /// 其它值表示错误
        /// 第2、3字节: 任务号,即上位机下达任务时的任务号
        /// 第4、5、6、7字节	      
        /// 堆垛机、穿梭车:
        /// 终点X坐标(沿轨道方向)
        /// (第8字节以后部分只对堆垛机有意义)
        /// 第8、9、10、11字节	     
        ///  终点Y坐标(沿高度方向)
        /// 
        /// 2.下位机给上位机发送条码信息
        /// 托盘条码:
        /// 第1-10字节:由左到右依次存放托盘上的10位条形码信息
        /// 烟箱一号工程码:
        /// 第1-9字节: 由左到右依次存放烟箱上的后9位一号工程码信息
        /// 一号工程码是32位,相同品牌烟箱的一号工程码的前23位相同,
        /// 所以只存放后9位条码。每个码垛位进烟口有两个烟箱一号工程码存放区,
        /// 共18个字节,上位机一次将两个烟箱一号工程码的信息同时读取。
        /// 上位机成功读取18个字节后,把这18个字节全部写入零,
        /// 下位机只有检测到18个字节全都是零时才可以写入新的一号工程码信息。
        /// 
        /// 3.下位机给上位机发送现场控制触摸屏申请信号
        /// 第1字节	   申请指令
        /// 1-	重发当前设备指令
        /// 2-	申请修改当前设备所执行任务的目标位置
        /// 第2、3字节	设备索引(当前申请设备的索引)
        /// 第4、5字节	任务号(需要修改目标位置的任务号)
        /// 第6、15字节	托盘条码(当前申请设备上的托盘的条码)
        /// 下位机可能因开关误动作等情况而引起任务记忆丢失,
        /// 此时根据设备上停放托盘的条码可以重新申请获得任务。
        /// 上位机收到此申请后重新发送当前设备的指令。
        /// 有设备发生故障时,下位机可以申请修改任务的目标位置。
        /// 上位机收到此申请后,如果可以找到新的路径,则修改此任务号的目标位置。
        /// 当申请指令为1时,任务号可以是任意值;当申请指令为2时,托盘条码可以是任意值。
        /// </summary>
        /// <param name="DeviceIndex">设备编号</param>
        /// <returns>获得上行消息成功后,发送应答消息后返回22个字的状态</returns>
        public int[] GetDeviceState(int DeviceIndex, int TaskIndex)
        {//20091107
            //string[] itemnames;
            int[] _Rword = new int[20];
            //string[] www;
            //string[] wi;
            //int add = 0;
            //int Bit = 0;
            //string ivalue;
            //List< string> itemvalue = new List<string>();
            Model.MDevice devinfo = Model.CGetInfo.GetDeviceInfo(DeviceIndex);
            //Model.MDevice askdev;
            //string sql = "";
            try
            {


                ////DataView dv = dbo.ExceSQL("SELECT F_DBW2Address,F_DBWGetLength,F_DeviceIndex,F_DeviceKindIndex,F_OPCProgID, F_RemoteIP FROM T_Base_Device where F_DeviceIndex=" + DeviceIndex).Tables[0].DefaultView;
                //if (devinfo.DeviceIndex == 0)
                //{
                //    _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState发生错误:在设备表中没找到设备所引!";

                //    return null;
                //}
                //else
                //{
                //    CCommonOPCClient.Hostname = CommonClassLib.AppSettings.GetValue("HostName");//20090922 devinfo.RemoteIP;
                //    CCommonOPCClient.ProgID = CommonClassLib.AppSettings.GetValue("OPCProgID");
                //    switch (devinfo.DeviceKind.ToString())
                //    {
                //        #region 光电开关总的采集
                //        case "28":
                //            int[] _RRword = new int[(int)devinfo.Dbw2Getlength];
                //            byte[] br = CCommonOPCClient.SyncReadAllItems(devinfo.S7Connection);//20110216
                //            if (br == null)
                //            {
                //                _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //                return null;
                //            }
                //            Array.Copy(br, _RRword, (int)devinfo.Dbw2Getlength);
                //            return _RRword;

                //        #endregion


                //        #region 光电开关及其他
                //        //case "18":
                //        //    Bit = Convert.ToInt16(devinfo.Dbw2Getlength * 8) - 1;//转换成bit0--bit7
                //        //    if (Bit > 7)
                //        //    {
                //        //        add = 1;
                //        //        Bit = Bit - 8;
                //        //    }
                //        //    else
                //        //    {
                //        //        add = 0;
                //        //    }
                //        //    ivalue = CCommonOPCClient.SyncReadItemValue("DB2,X" + Convert.ToString(devinfo.Dbw2Address + add) + "." + Bit.ToString());
                //        //    if ((ivalue == null) || (ivalue == ""))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    if (ivalue.ToLower() == "true")
                //        //    {
                //        //        _Rword[0] = 1;
                //        //    }
                //        //    else
                //        //    {
                //        //        _Rword[0] = 0;
                //        //    }

                //        //    _Rword[1] = 3;//3-检测开关状态
                //        //    _Rword[2] = TaskIndex;//任务号
                //        //    _Rword[3] = 0;
                //        //    _Rword[4] = 0;
                //        //    _Rword[5] = DeviceIndex;//设备号索引

                //        //    break;
                //        //#endregion
                //        //#region 按钮
                //        //case "14":
                //        //    Bit = Convert.ToInt16(devinfo.Dbw2Getlength * 8) - 1;//转换成bit0--bit7
                //        //    if (Bit > 7)
                //        //    {
                //        //        add = 1;
                //        //        Bit = Bit - 8;
                //        //    }
                //        //    else
                //        //    {
                //        //        add = 0;
                //        //    }
                //        //    ivalue = CCommonOPCClient.SyncReadItemValue("DB2,X" + Convert.ToString(devinfo.Dbw2Address + add) + "." + Bit.ToString());
                //        //    if ((ivalue == null) || (ivalue == ""))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }

                //        //    _Rword[0] = Convert.ToInt32(ivalue);
                //        //    _Rword[1] = 3;//3-检测开关状态
                //        //    _Rword[2] = TaskIndex;//任务号
                //        //    _Rword[3] = 0;
                //        //    _Rword[4] = 0;
                //        //    _Rword[5] = DeviceIndex;//设备号索引

                //        //    break;
                //        //#endregion


                //        //#region 现场控制触摸屏申请信息(16个字节)
                //        //case "22":
                //        //    itemnames = new string[14];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,int" + Convert.ToString((devinfo.Dbw2Address + 2));
                //        //    itemnames[3] = "DB2,int" + Convert.ToString((devinfo.Dbw2Address + 4));
                //        //    for (int i = 6; i < 16; i++)
                //        //    {
                //        //        itemnames[i - 2] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + i));
                //        //    }
                //        //    object[] obj = CCommonOPCClient.SyncReadItemValues(itemnames);

                //        //    if ((obj == null))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    if (obj[0].ToString() != "1") return null;
                //        //    _Rword[0] = TaskIndex;
                //        //    _Rword[1] = DeviceIndex;
                //        //    _Rword[2] = Convert.ToInt32(obj[1]);
                //        //    _Rword[3] = Convert.ToInt32(obj[2]);
                //        //    _Rword[4] = Convert.ToInt32(obj[3]);
                //        //    askdev = Model.CGetInfo.GetDeviceInfo(_Rword[3]);
                //        //    //_Rword[2];//命令字
                //        //    //_Rword[3];//设备索引(当前申请设备的索引)
                //        //    //_Rword[4];//任务号(需要修改目标位置的任务号)
                //        //    int bcz = 0;
                //        //    www = new string[1];
                //        //    wi = new string[1] { itemnames[0] };
                //        //    string barcode = string.Empty;
                //        //    for (int j = 6; j < 16; j++)
                //        //    {
                //        //        bcz = bcz + Convert.ToInt32(obj[j - 2]);
                //        //        barcode += Convert.ToChar(obj[j - 2]).ToString().ToUpper();
                //        //        _Rword[j - 1] = Convert.ToInt32(obj[j - 2]);
                //        //    }
                //        //    #region 申请重发当前设备指令(1)

                //        //    if (_Rword[2] == 1)//命令字
                //        //    {
                //        //        if (_Rword[4] == 0)//无任务号
                //        //        {
                //        //            if (bcz == 0)//无条码号
                //        //            {
                //        //                //32申请的任务号和条形码都不存在
                //        //                www[0] = "32";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            else//有条码号
                //        //            {
                //        //                #region 有条码号
                //        //                //DataView dv = dbo.ExceSQL("SELECT F_BarCode, F_Askkind, F_DeviceIndex FROM T_Base_PLC_Ask where F_DeviceIndex=" + DeviceIndex).Tables[0].DefaultView;
                //        //                //if (dv.Count > 0)
                //        //                //{
                //        //                //    if (barcode == dv[i]["F_BarCode"].ToString())
                //        //                //    {
                //        //                //        return null;
                //        //                //    }
                //        //                //    else
                //        //                //    {

                //        //                if (askdev.UseCommonDB == "1")
                //        //                {
                //        //                    sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                     "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_TxtParam = '" +
                //        //                     barcode + "'   order by F_MonitorIndex asc";
                //        //                }
                //        //                else
                //        //                {
                //        //                    sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                     "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_TxtParam = '" +
                //        //                     barcode + "' and F_DeviceIndex=" + _Rword[3] + " order by F_MonitorIndex asc";
                //        //                }
                //        //                DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;
                //        //                if (dvbc.Count > 0)
                //        //                {
                //        //                    if (askdev.UseCommonDB == "1")
                //        //                    {
                //        //                        dbo.ExceSQL("update T_Monitor_Task set F_AheadDetect='',F_DeviceIndex=" + _Rword[3] + " where F_MonitorIndex=" + dvbc[0]["F_MonitorIndex"] + "");
                //        //                    }

                //        //                    www[0] = "4";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                    return _Rword;
                //        //                }
                //        //                else
                //        //                {
                //        //                    //35申请的设备号和条形码不匹配
                //        //                    www[0] = "35";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                    return null;
                //        //                }
                //        //                //    }
                //        //                //}
                //        //                #endregion
                //        //            }
                //        //        }
                //        //        else//有任务号
                //        //        {
                //        //            #region 有任务号
                //        //            //DataView dv = dbo.ExceSQL("SELECT F_TaskIndex, F_Askkind, F_DeviceIndex FROM T_Base_PLC_Ask where F_DeviceIndex=" + DeviceIndex).Tables[0].DefaultView;
                //        //            //if (dv.Count > 0)
                //        //            //{
                //        //            //    if (_Rword[4] == dv[i]["F_TaskIndex"].ToString())
                //        //            //    {
                //        //            //        return null;
                //        //            //    }
                //        //            //    else
                //        //            //    {
                //        //            if (askdev.UseCommonDB == "1")
                //        //            {
                //        //                sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                        "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_MonitorIndex = '" +
                //        //                        _Rword[4] + "'   order by F_MonitorIndex asc";
                //        //            }
                //        //            else
                //        //            {
                //        //                sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                        "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_MonitorIndex = '" +
                //        //                        _Rword[4] + "' and F_DeviceIndex=" + _Rword[3] + " order by F_MonitorIndex asc";
                //        //            }
                //        //            DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;
                //        //            if (dvbc.Count > 0)
                //        //            {
                //        //                if (askdev.UseCommonDB == "1")
                //        //                {
                //        //                    dbo.ExceSQL("update T_Monitor_Task set F_AheadDetect='',F_DeviceIndex=" + _Rword[3] + " where F_MonitorIndex=" + dvbc[0]["F_MonitorIndex"] + "");
                //        //                }
                //        //                //RestoreDevice(Convert.ToInt32(dvbc[0]["F_MonitorIndex"]));
                //        //                www[0] = "4";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                ////记录任务号
                //        //                //dbo.ExceSQL("update T_Base_PLC_Ask set F_TaskIndex='" + _Rword[4]
                //        //                //    + "' where F_DeviceIndex=" + DeviceIndex);
                //        //                return _Rword;
                //        //            }
                //        //            else
                //        //            {
                //        //                //36申请的设备号和任务号不匹配
                //        //                www[0] = "36";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            //    }
                //        //            //}
                //        //            #endregion
                //        //        }

                //        //    }

                //        //    #endregion

                //        //    #region 申请修改当前设备所执行任务目标位置(2)

                //        //    else if (_Rword[2] == 2)//命令字
                //        //    {
                //        //        //申请修改当前设备所执行任务目标位置的F_TaskIndex重复时不允许相应申请

                //        //    }

                //        //    #endregion

                //        //    #region 上报完成(3)
                //        //    else if (_Rword[2] == 3)//命令字
                //        //    {
                //        //        if (_Rword[4] == 0)//无任务号
                //        //        {
                //        //            if (bcz == 0)//无条码号
                //        //            {
                //        //                //32申请的任务号和条形码都不存在
                //        //                www[0] = "32";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            else//有条码号
                //        //            {
                //        //                #region 有条码号
                //        //                sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                     "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_TxtParam = '" +
                //        //                     barcode + "'  order by F_MonitorIndex asc";
                //        //                DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;

                //        //                if (dvbc.Count > 0)
                //        //                {
                //        //                    www[0] = "2";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);

                //        //                    return _Rword;
                //        //                }
                //        //                else
                //        //                {
                //        //                    //30申请的条形码不存在
                //        //                    www[0] = "30";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                    return null;
                //        //                }
                //        //                #endregion
                //        //            }
                //        //        }
                //        //        else
                //        //        {

                //        //            #region 有任务号

                //        //            sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                    "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_MonitorIndex = '" +
                //        //                    _Rword[4] + "'  order by F_MonitorIndex asc";

                //        //            DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;

                //        //            if (dvbc.Count > 0)
                //        //            {
                //        //                www[0] = "2";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);

                //        //                return _Rword;
                //        //            }
                //        //            else
                //        //            {
                //        //                //31申请的任务号不存在
                //        //                www[0] = "31";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            //    }
                //        //            //}
                //        //            #endregion
                //        //        }
                //        //    }
                //        //    #endregion

                //        //    #region 删除当前设备所执行的任务(4)(托盘拿走)
                //        //    else if (_Rword[2] == 4)//命令字
                //        //    {
                //        //        if (_Rword[4] == 0)//无任务号
                //        //        {
                //        //            if (bcz == 0)//无条码号
                //        //            {
                //        //                //32申请的任务号和条形码都不存在
                //        //                www[0] = "32";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            else//有条码号
                //        //            {
                //        //                #region 有条码号
                //        //                sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                     "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_TxtParam = '" +
                //        //                     barcode + "'  order by F_MonitorIndex asc";
                //        //                DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;

                //        //                if (dvbc.Count > 0)
                //        //                {
                //        //                    www[0] = "2";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);

                //        //                    return _Rword;
                //        //                }
                //        //                else
                //        //                {
                //        //                    //30申请的条形码不存在
                //        //                    www[0] = "30";
                //        //                    CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                    return null;
                //        //                }
                //        //                #endregion
                //        //            }
                //        //        }
                //        //        else
                //        //        {

                //        //            #region 有任务号

                //        //            sql = "SELECT F_MonitorIndex,F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2," +
                //        //                            "F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6 FROM T_Monitor_Task WHERE F_MonitorIndex = '" +
                //        //                            _Rword[4] + "' order by F_MonitorIndex asc";

                //        //            DataView dvbc = dbo.ExceSQL(sql).Tables[0].DefaultView;
                //        //            if (dvbc.Count > 0)
                //        //            {
                //        //                www[0] = "3";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);

                //        //                return _Rword;
                //        //            }
                //        //            else
                //        //            {
                //        //                //31申请的任务号不存在
                //        //                www[0] = "31";
                //        //                CCommonOPCClient.SyncWriteAllItemValue(wi, www);
                //        //                return null;
                //        //            }
                //        //            #endregion
                //        //        }
                //        //    }
                //        //    #endregion
                //        //    break;
                //        //#endregion


                //        //#region 机器人码垛信息
                //        //case "24":
                //        //    itemnames = new string[3];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 2));
                //        //    obj = CCommonOPCClient.SyncReadItemValues(itemnames);

                //        //    if ((obj == null))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    if (obj[0].ToString() != "1") return null;
                //        //    _Rword[0] = TaskIndex;
                //        //    _Rword[1] = DeviceIndex;
                //        //    _Rword[2] = Convert.ToInt32(obj[1]);//机器人动作:1-取货完成;2-放货完成;3-托盘码垛完成
                //        //    _Rword[3] = Convert.ToInt32(obj[2]);//托盘码垛数量
                //        //    www = new string[1];
                //        //    wi = new string[1] { itemnames[0] };
                //        //    www[0] = "2";
                //        //    CCommonOPCClient.SyncWriteAllItemValue(wi, www);

                //        //    break;
                //        //#endregion

                //        //#region 其他设备
                //        //case "1"://堆垛机
                //        //    itemnames = new string[5];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,int" + Convert.ToString((devinfo.Dbw2Address + 2));
                //        //    itemnames[3] = "DB2,DWORD" + Convert.ToString((devinfo.Dbw2Address + 4));
                //        //    itemnames[4] = "DB2,DWORD" + Convert.ToString((devinfo.Dbw2Address + 8));
                //        //    itemvalue = CCommonOPCClient.SyncReadAllItems(itemnames);
                //        //    if (itemvalue.Count == 0)
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    _Rword[0] = Convert.ToInt32(itemvalue[0]);//读写标志
                //        //    _Rword[1] = Convert.ToInt32(itemvalue[1]);//状态
                //        //    _Rword[2] = Convert.ToInt32(itemvalue[2]);//任务号
                //        //    _Rword[3] = Convert.ToInt32(itemvalue[3]);//X坐标
                //        //    _Rword[4] = Convert.ToInt32(itemvalue[4]);//Y坐标
                //        //    _Rword[5] = DeviceIndex;//设备号索引
                //        //    break;
                //        //case "2"://输送机
                //        //    itemnames = new string[3];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,int" + Convert.ToString((devinfo.Dbw2Address + 2));

                //        //    itemvalue = CCommonOPCClient.SyncReadAllItems(itemnames);
                //        //    if ((itemvalue == null) || (itemvalue.Count == 0))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    _Rword[0] = Convert.ToInt32(itemvalue[0]);//读写标志
                //        //    _Rword[1] = Convert.ToInt32(itemvalue[1]);//状态
                //        //    _Rword[2] = Convert.ToInt32(itemvalue[2]);//任务号
                //        //    _Rword[3] = 0;
                //        //    _Rword[4] = 0;
                //        //    _Rword[5] = DeviceIndex;//设备号索引
                //        //    break;
                //        //case "4"://RGV
                //        //    itemnames = new string[4];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,int" + Convert.ToString((devinfo.Dbw2Address + 2));
                //        //    itemnames[3] = "DB2,DWORD" + Convert.ToString((devinfo.Dbw2Address + 4));

                //        //    itemvalue = CCommonOPCClient.SyncReadAllItems(itemnames);
                //        //    if ((itemvalue == null) || (itemvalue.Count == 0))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    _Rword[0] = Convert.ToInt32(itemvalue[0]);//读写标志
                //        //    _Rword[1] = Convert.ToInt32(itemvalue[1]);//状态
                //        //    _Rword[2] = Convert.ToInt32(itemvalue[2]);//任务号
                //        //    _Rword[3] = Convert.ToInt32(itemvalue[3]);//X坐标
                //        //    _Rword[4] = 0;
                //        //    _Rword[5] = DeviceIndex;//设备号索引
                //        //    break;
                //        //case "13"://机器人申请残盘数量
                //        //    itemnames = new string[4];
                //        //    itemnames[0] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //        //    itemnames[1] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 1));
                //        //    itemnames[2] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 2));
                //        //    itemnames[3] = "DB2,byte" + Convert.ToString((devinfo.Dbw2Address + 3));

                //        //    itemvalue = CCommonOPCClient.SyncReadAllItems(itemnames);
                //        //    if ((itemvalue == null) || (itemvalue.Count == 0))
                //        //    {
                //        //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        //        return null;
                //        //    }
                //        //    if (itemvalue[0] != "1") return null;
                //        //    _Rword[0] = Convert.ToInt32(itemvalue[1]);//1#码垛位
                //        //    _Rword[1] = Convert.ToInt32(itemvalue[2]);//2#码垛位
                //        //    _Rword[2] = Convert.ToInt32(itemvalue[3]);//3#码垛位
                //        //    _Rword[3] = 0;
                //        //    _Rword[4] = 0;
                //        //    _Rword[5] = DeviceIndex;//设备号索引
                //        //    break;
                //        #endregion
                //        default:
                //            break;
                //    }
                //}
                return _Rword;
            }
            catch (Exception ex)
            {
                _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState发生错误:" + ex.Message;
                return null;
            }
            finally
            {
                //itemnames=null;
                _Rword = null;
                //www=null;
                //wi=null;
                //ivalue=null;
                //itemvalue =null;
                devinfo = null;
                //askdev=null;
                //sql = null;
            }
            
        }
        public string GetStringData(int DeviceIndex, int TaskIndex)
        {//20091107
            //string headstr = "";
            //string[] itemnames;
            //string retdata = "";
            //string[] witemnames;
            //byte[] itemvalue;
            //object[] obj;
            Model.MDevice devinfo = Model.CGetInfo.GetDeviceInfo(DeviceIndex);
            try
            {

                //if (devinfo.DeviceIndex == 0)
                //{
                //    _commLayerError = "OPCClient.CGetDeviceState.GetStringData发生错误:在设备表中没找到设备所引!";

                //    return null;
                //}
                //else
                //{
                //    CCommonOPCClient.Hostname = CommonClassLib.AppSettings.GetValue("HostName");//20090922 devinfo.RemoteIP;
                //    CCommonOPCClient.ProgID = CommonClassLib.AppSettings.GetValue("OPCProgID");
                //    CCommonOPCClient.PlcConnectionID = devinfo.S7Connection;//20110216
                //    witemnames = new string[1] { Model.CGeneralFunction.DBGet+",byte" + Convert.ToString((devinfo.Dbw2Address + 0)) };
                //    switch (devinfo.DeviceKind.ToString())
                //    {

                //        #region 托盘条码信息(10个字节)
                //        case "7":
                //            itemnames = new string[11];
                //            itemnames[0] = Model.CGeneralFunction.DBGet+",byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //            for (int i = 1; i < 11; i++)
                //            {
                //                itemnames[i] = Model.CGeneralFunction.DBGet+",byte" + Convert.ToString((devinfo.Dbw2Address + i));
                //            }
                //            obj = CCommonOPCClient.SyncReadItemValues(itemnames);
                //            if ((obj == null))
                //            {
                //                _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //                return null;
                //            }
                //            if (obj[0].ToString() != "1") return null;
                //            itemvalue = new byte[obj.Length];
                //            Array.Copy(obj, itemvalue, obj.Length);

                //            headstr = itemvalue[0].ToString();
                //            for (int i = 1; i <= 10; i++)
                //            {
                //                retdata += Convert.ToChar(itemvalue[i]);

                //            }


                //            break;
                //        #endregion

                //        #region 烟箱一号工程码(两组共32个字节+一个标志头)
                //        case "21":
                //            itemnames = new string[33];
                //            itemnames[0] = Model.CGeneralFunction.DBGet+",byte" + Convert.ToString((devinfo.Dbw2Address + 0));
                //            for (int i = 1; i < 33; i++)
                //            {
                //                itemnames[i] = Model.CGeneralFunction.DBGet+",byte" + Convert.ToString((devinfo.Dbw2Address + i));
                //            }
                //            obj = CCommonOPCClient.SyncReadItemValues(itemnames);
                //            if ((obj == null))
                //            {
                //                _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //                return null;
                //            }
                //            if (obj[0].ToString() != "1") return null;
                //            itemvalue = new byte[obj.Length];
                //            Array.Copy(obj, itemvalue, obj.Length);

                //            headstr = itemvalue[0].ToString();
                //            for (int i = 1; i <= 32; i++)
                //            {
                //                retdata += Convert.ToChar(itemvalue[i]);

                //            }
                //            break;
                //        #endregion

                //        default:
                //            break;
                //    }
                //}
                //if (headstr == "1")
                //{
                //    string[] wv = new string[1] { "2" };
                //    if (CCommonOPCClient.SyncWriteAllItemValue(witemnames, wv) == false)
                //    {
                //        _commLayerError = "OPCClient.CGetDeviceState.GetDeviceState:" + CCommonOPCClient.OpcError;
                //        return null;
                //    }
                //    else
                //    {
                //        return retdata;
                //    }
                //}
                //else
                //{
                //    return null;
                //}
                return null;
            }
            catch (Exception ex)
            {
                _commLayerError = "OPCClient.CGetDeviceState.GetStringData发生错误:" + ex.Message;
                return null;
            }
            finally
            {
                //headstr = null;
                //itemnames=null;
                //retdata = null;
                //witemnames=null;
                //itemvalue=null;
                //obj = null ;
                devinfo = null;
            }
        }
    }
}