Пример #1
0
        private void GetVarValue(CMessage nMsg, byte[] Datebuff)
        {
            lock (this)
            {
                int iQuCommand = nMsg.sSendMsg.DataBuffer[8] * 256 * 256 * 256 + nMsg.sSendMsg.DataBuffer[9] * 256 * 256 + nMsg.sSendMsg.DataBuffer[10] * 256 + nMsg.sSendMsg.DataBuffer[11];
                int iReCommand = Datebuff[0] * 256 * 256 * 256 + Datebuff[1] * 256 * 256 + Datebuff[2] * 256 + Datebuff[3];
                int Errorcode  = Datebuff[4] * 256 * 256 * 256 + Datebuff[5] * 256 * 256 + Datebuff[6] * 256 + Datebuff[7];
                if (Errorcode > 0)
                {
                    string sErr = "";
                    switch (Errorcode)
                    {
                    case 1: sErr = "The header is not ‘FINS’ (ASCII code)."; break;

                    case 2: sErr = "The data length is too long."; break;

                    case 3: sErr = "The command is not supported."; break;

                    case 32: sErr = "All connections are in use."; break;

                    case 33: sErr = "The specified node is already connected."; break;

                    case 34: sErr = Errorcode.ToString() + "(" + Errorcode.ToString("X2") + "H):Attempt to access a protected node from an unspecified IP address."; break;

                    case 35: sErr = Errorcode.ToString() + "(" + Errorcode.ToString("X2") + "H):The client FINS node address is out of range."; break;

                    case 36: sErr = Errorcode.ToString() + "(" + Errorcode.ToString("X2") + "H):The same FINS node address is being used by the client and server."; break;

                    case 37: sErr = Errorcode.ToString() + "(" + Errorcode.ToString("X2") + "H):All the node addresses available for allocation have been used."; break;

                    default: sErr = "No Details."; break;
                    }
                    CAlarmMsgEventArgs ee = new CAlarmMsgEventArgs();
                    ee.Date_Time  = DateTime.Now;
                    ee.Recorder   = "PLC报警:" + Errorcode.ToString() + "," + sErr;
                    ee.priority   = EAlarmPriority.PRIORITY_2;
                    ee.eAlarmType = EAlarmType.StationState;
                    ee.StaName    = Name;
                    staAlarm.OnAlarmEvent(ee);
                    Debug.WriteLine("CProtcolFINS.Errorcode" + sErr);

                    if (CommStateE == ECommSatate.Failure)
                    {
                        CommStateE = ECommSatate.Normal;//子站置通信恢复
                        CAlarmMsgEventArgs eea = new CAlarmMsgEventArgs();
                        eea.Date_Time  = DateTime.Now;
                        eea.Recorder   = Description + "通信恢复";
                        eea.priority   = EAlarmPriority.PRIORITY_1;
                        eea.eAlarmType = EAlarmType.StationState;
                        eea.StaName    = Name;
                        staAlarm.OnAlarmEvent(eea);
                    }
                    else if (CommStateE != ECommSatate.Normal)
                    {
                        CommStateE = ECommSatate.Normal;//子站置通信恢复
                    }
                    DelayTime          = 0;
                    present_MsgFailRep = 0;
                    present_DevFailNum = 0;
                    return;
                }
                switch (iReCommand)
                {
                case 1:
                    break;

                case 2:
                    if (iReCommand == 2)
                    {
                        if (Datebuff[18] != nMsg.sSendMsg.DataBuffer[26] || Datebuff[19] != nMsg.sSendMsg.DataBuffer[27])
                        {
                            return;
                        }
                        if (Datebuff[18] == 1 && Datebuff[19] == 1)    //读
                        {
                            int iStart    = Convert.ToInt32(nMsg.Starting);
                            int iReadType = (int)((iStart + 1) / 1000);

                            if (iReadType < 8)
                            {
                                iStart = iStart - (iReadType - 1) * 1000;
                                if (iReadType == 1)
                                {
                                    for (int i = 0; i < 50; i++)
                                    {
                                        int n = i * 2 + 22;
                                        System[i] = Datebuff[n] * 256 + Datebuff[n + 1];
                                    }
                                }
                                //foreach (KeyValuePair<string, CVar> kvp in nMsg.ListMsgVar)
                                foreach (CVar nVar in StaDevice.ListDevVar)
                                {
                                    if (nVar.DAType != EDAType.DA_YC)
                                    {
                                        continue;
                                    }
                                    int   iIndex = nVar.ByteAddr - iStart;
                                    int   iBit   = nVar.BitAddr;
                                    int   iLen   = nVar.Length;
                                    Int64 Value  = 0;
                                    int   nByte  = iIndex * 2 + 22;
                                    if (nByte + iLen / 8 > Datebuff.Length || nByte < 0 || iIndex < 0)
                                    {
                                        continue;
                                    }
                                    switch (iLen)
                                    {
                                    case 1:
                                        int aa = (int)(Math.Pow(2, iBit));
                                        if (nVar.bHighLow == 1)
                                        {
                                            Value = (Datebuff[nByte] * 256 + Datebuff[nByte + 1]) & aa;
                                        }
                                        else
                                        {
                                            Value = (Datebuff[nByte] + Datebuff[nByte + 1] * 256) & aa;
                                        }
                                        if (Value > 0)
                                        {
                                            Value = 1;
                                        }
                                        break;

                                    case 8:
                                        aa = (int)((iBit + 1) / 8);
                                        if (nVar.bHighLow == 1)
                                        {
                                            Value = Datebuff[nByte + 1 - aa];
                                        }
                                        else
                                        {
                                            Value = Datebuff[nByte + aa];
                                        }
                                        break;

                                    case 16:
                                        if (nVar.bHighLow == 1)
                                        {
                                            Value = Datebuff[nByte] * 256 + Datebuff[nByte + 1];
                                        }
                                        else
                                        {
                                            Value = Datebuff[nByte] + Datebuff[nByte + 1] * 256;
                                        }
                                        break;

                                    case 32:
                                        if (nVar.bHighLow == 1)
                                        {
                                            Value = Datebuff[nByte] * 256 * 256 * 256 + Datebuff[nByte + 1] * 256 * 256 + Datebuff[nByte + 2] * 256 + Datebuff[nByte + 3];
                                        }
                                        else
                                        {
                                            Value = Datebuff[nByte] + Datebuff[nByte + 1] * 256 + Datebuff[nByte + 2] * 256 * 256 + Datebuff[nByte + 3] * 256 * 256 * 256;
                                        }
                                        break;

                                    default:
                                        break;
                                    }
                                    if (iReadType == 1)
                                    {
                                        nVar.SetValue(Value);
                                    }
                                    else if (iReadType == 2)
                                    {
                                        nVar.PLCValue[iReadType] = (Int64)Math.Round(Value * nVar.RatioValue + nVar.BaseValue);
                                    }
                                    else
                                    {
                                        nVar.PLCValue[iReadType] = (Int64)Value;
                                    }
                                }
                                CheckRunState();
                            }
                            else if (iReadType == 8)    //DO
                            {
                                foreach (CVar nVar in StaDevice.ListDevVar)
                                {
                                    if (nVar.DAType != EDAType.DA_YX)
                                    {
                                        continue;
                                    }

                                    int   iIndex = nVar.ByteAddr - iStart;
                                    int   iBit   = nVar.BitAddr;
                                    int   iLen   = nVar.Length;
                                    Int64 Value  = 0;
                                    int   nByte  = iIndex * 2 + 22;
                                    if (nByte + iLen / 8 > Datebuff.Length || nByte < 0)
                                    {
                                        continue;
                                    }

                                    int aa = (int)(Math.Pow(2, iBit));
                                    if (nVar.bHighLow == 1)
                                    {
                                        Value = (Datebuff[nByte] * 256 + Datebuff[nByte + 1]) & aa;
                                    }
                                    else
                                    {
                                        Value = (Datebuff[nByte] + Datebuff[nByte + 1] * 256) & aa;
                                    }
                                    if (Value > 0)
                                    {
                                        Value = 1;
                                    }

                                    nVar.SetValue(Value);
                                }
                            }
                            else if (iReadType == 9)    //DI
                            {
                                foreach (CVar nVar in StaDevice.ListDevVar)
                                {
                                    if (nVar.DAType != EDAType.DA_YX)
                                    {
                                        continue;
                                    }
                                    int   iIndex = nVar.ByteAddr - iStart;
                                    int   iBit   = nVar.BitAddr;
                                    int   iLen   = nVar.Length;
                                    Int64 Value  = 0;
                                    int   nByte  = iIndex * 2 + 22;
                                    if (nByte + iLen / 8 > Datebuff.Length || nByte < 0)
                                    {
                                        continue;
                                    }

                                    int aa = 1 << iBit;

                                    if (nVar.bHighLow == 1)
                                    {
                                        Value = (Datebuff[nByte] * 256 + Datebuff[nByte + 1]) & aa;
                                    }
                                    else
                                    {
                                        Value = (Datebuff[nByte] + Datebuff[nByte + 1] * 256) & aa;
                                    }
                                    if (Value > 0)
                                    {
                                        Value = 1;
                                    }

                                    nVar.SetValue(Value);
                                }
                            }
                        }
                        else if (Datebuff[18] == 1 && Datebuff[19] == 2)    //写
                        {
                            //Debug.WriteLine("CProtcolFINS.PLC0102");
                        }
                    }
                    break;

                default:
                    break;
                }
                if (CommStateE == ECommSatate.Failure)
                {
                    CommStateE = ECommSatate.Normal;//子站置通信恢复
                    CAlarmMsgEventArgs ee = new CAlarmMsgEventArgs();
                    ee.Date_Time  = DateTime.Now;
                    ee.Recorder   = Description + "通信恢复";
                    ee.priority   = EAlarmPriority.PRIORITY_1;
                    ee.eAlarmType = EAlarmType.StationState;
                    ee.StaName    = Name;
                    //staAlarm.OnAlarmEvent(ee);
                }
                else if (CommStateE != ECommSatate.Normal)
                {
                    CommStateE = ECommSatate.Normal;//子站置通信恢复
                }
                DelayTime          = 0;
                present_MsgFailRep = 0;
                present_DevFailNum = 0;
            }
        }
Пример #2
0
        private bool GetSendMsg()
        {
            try
            {
                CMessage sSend = new CMessage();
                lock (this)
                {
                    if (ListImmSendMsg.Count > 0)
                    {
                        sSend       = (CMessage)ListImmSendMsg[0];
                        LastSendMsg = sSend.Clone();
                        //Write(sSend.sSendMsg);
                        ListImmSendMsg.RemoveAt(0);
                        PortEvents[1].Set();
                        return(true);
                    }
                }
                //return true;
                CStation nSta = (CStation)ListStation[PresentSendStaIndex];
                if (nSta.Driver == "真空硅")
                {
                    int iii = 0;
                }
                if (nSta == null)
                {
                    PresentSendStaIndex++;
                    PresentSendStaIndex = PresentSendStaIndex % ListStation.Count;
                    return(false);
                }
                if (nSta.CommStateE == ECommSatate.Failure)    //当前发送子站为通信不正常时
                {
                    nSta.present_DevFailCyc++;                 //当前发送子站等待周期+1
                    //Debug.WriteLine(nSta.Name + " 4:" + nSta.present_MsgFailWaitCyc.ToString());
                    if (nSta.present_DevFailCyc <= DevFailCyc) //不超过最大等待周期时继续下一个子站
                    {
                        //Debug.WriteLine(nSta.Name + " 5:" + PresentSendStaIndex.ToString());
                        PresentSendStaIndex++;
                        PresentSendStaIndex = PresentSendStaIndex % ListStation.Count;
                        //Debug.WriteLine(nSta.Name + " 6:" + PresentSendStaIndex.ToString());
                        GetSendMsg();
                        return(false);
                    }
                    else//否则开始重新发送该子站,重新计算超时
                    {
                        nSta.present_MsgFailRep = 0;
                        nSta.present_DevFailNum = 0;
                        nSta.present_DevFailCyc = 0;
                    }
                }
                sSend = nSta.GetNextLoopMsg();
                if (sSend.sSendMsg.Length > 0)
                {
                    LastSendMsg = sSend;//.Clone();
                    PortEvents[1].Set();
                }

                PresentSendStaIndex++;
                PresentSendStaIndex = PresentSendStaIndex % ListStation.Count;
            }
            catch (Exception e)
            {
                Debug.WriteLine("GetSendMsg" + e.Message);
            }
            return(true);
        }
Пример #3
0
        //界面操作
        public void SendAODO(string sVar, int iValue, string sType)
        {
            if (sType == "SY")
            {
                SSend_Message temSet = new SSend_Message();
                int           iLen;
                CMessage      nMsg = new CMessage();

                temSet.Length     = 36;
                temSet.DataBuffer = new byte[temSet.Length];
                for (int i = 0; i < 4; i++)
                {
                    temSet.DataBuffer[i] = FINSTCPHeader[i];
                }
                iLen = temSet.Length - 8;
                temSet.DataBuffer[4]  = (byte)(iLen >> 24);
                temSet.DataBuffer[5]  = (byte)(iLen >> 16);
                temSet.DataBuffer[6]  = (byte)(iLen >> 8);
                temSet.DataBuffer[7]  = (byte)iLen;
                temSet.DataBuffer[11] = 2;
                for (int i = 0; i < 10; i++)
                {
                    temSet.DataBuffer[i + 16] = FINSHeader[i];
                }
                temSet.DataBuffer[26] = 1;
                temSet.DataBuffer[27] = 2;
                temSet.DataBuffer[28] = 130;

                iLen = Convert.ToInt32(sVar);
                temSet.DataBuffer[29] = (byte)(iLen >> 8);
                temSet.DataBuffer[30] = (byte)iLen;
                temSet.DataBuffer[33] = 1;
                temSet.DataBuffer[34] = (byte)(iValue >> 8);
                temSet.DataBuffer[35] = (byte)iValue;

                nMsg.sSendMsg = temSet;
                ListImmSendMsg.Add(nMsg);

                CAlarmMsgEventArgs e = new CAlarmMsgEventArgs();
                e.Date_Time  = DateTime.Now;
                e.Recorder   = GetMsg(sVar, iValue);
                e.priority   = EAlarmPriority.PRIORITY_1;
                e.eAlarmType = EAlarmType.ManualAct;
                e.StaName    = Name;
                if (e.Recorder != "")
                {
                    staAlarm.OnAlarmEvent(e);
                }
                return;
            }
            foreach (CVar nVar in StaDevice.ListDevVar)
            {
                if (nVar.Name == sVar)
                {
                    SSend_Message temSet = new SSend_Message();
                    int           iLen;
                    CMessage      nMsg = new CMessage();
                    switch (sType)
                    {
                    case "AO":    //AO 需要处理变比
                        iValue            = (int)Math.Round(iValue / nVar.RatioValue);
                        temSet.Length     = 36;
                        temSet.DataBuffer = new byte[temSet.Length];
                        for (int i = 0; i < 4; i++)
                        {
                            temSet.DataBuffer[i] = FINSTCPHeader[i];
                        }
                        iLen = temSet.Length - 8;
                        temSet.DataBuffer[4]  = (byte)(iLen >> 24);
                        temSet.DataBuffer[5]  = (byte)(iLen >> 16);
                        temSet.DataBuffer[6]  = (byte)(iLen >> 8);
                        temSet.DataBuffer[7]  = (byte)iLen;
                        temSet.DataBuffer[11] = 2;
                        for (int i = 0; i < 10; i++)
                        {
                            temSet.DataBuffer[i + 16] = FINSHeader[i];
                        }
                        temSet.DataBuffer[26] = 1;
                        temSet.DataBuffer[27] = 2;
                        temSet.DataBuffer[28] = 130;

                        iLen = nVar.ByteAddr + 1000;
                        temSet.DataBuffer[29] = (byte)(iLen >> 8);
                        temSet.DataBuffer[30] = (byte)iLen;
                        temSet.DataBuffer[33] = 1;
                        temSet.DataBuffer[34] = (byte)(iValue >> 8);
                        temSet.DataBuffer[35] = (byte)iValue;

                        nMsg.sSendMsg = temSet;
                        ListImmSendMsg.Add(nMsg); Debug.WriteLine("SendAODO:AO.ListImmSendMsg");
                        break;

                    case "AO2":    //AO2 不需要处理变比
                        temSet.Length     = 36;
                        temSet.DataBuffer = new byte[temSet.Length];
                        for (int i = 0; i < 4; i++)
                        {
                            temSet.DataBuffer[i] = FINSTCPHeader[i];
                        }
                        iLen = temSet.Length - 8;
                        temSet.DataBuffer[4]  = (byte)(iLen >> 24);
                        temSet.DataBuffer[5]  = (byte)(iLen >> 16);
                        temSet.DataBuffer[6]  = (byte)(iLen >> 8);
                        temSet.DataBuffer[7]  = (byte)iLen;
                        temSet.DataBuffer[11] = 2;
                        for (int i = 0; i < 10; i++)
                        {
                            temSet.DataBuffer[i + 16] = FINSHeader[i];
                        }
                        temSet.DataBuffer[26] = 1;
                        temSet.DataBuffer[27] = 2;
                        temSet.DataBuffer[28] = 130;

                        iLen = nVar.ByteAddr + 1000;
                        temSet.DataBuffer[29] = (byte)(iLen >> 8);
                        temSet.DataBuffer[30] = (byte)iLen;
                        temSet.DataBuffer[33] = 1;
                        temSet.DataBuffer[34] = (byte)(iValue >> 8);
                        temSet.DataBuffer[35] = (byte)iValue;

                        nMsg.sSendMsg = temSet;
                        ListImmSendMsg.Add(nMsg); Debug.WriteLine("SendAODO:AO2.ListImmSendMsg");
                        break;

                    case "DO":    //DO
                        int iByte = nVar.ByteAddr - 1000;
                        int iBit  = nVar.BitAddr;
                        if (nVar.GetBoolValue())
                        {
                            iValue = 0;
                        }
                        else
                        {
                            iValue = 1;
                        }
                        temSet.Length     = 35;
                        temSet.DataBuffer = new byte[temSet.Length];
                        for (int i = 0; i < 4; i++)
                        {
                            temSet.DataBuffer[i] = FINSTCPHeader[i];
                        }
                        iLen = temSet.Length - 8;
                        temSet.DataBuffer[4]  = (byte)(iLen >> 24);
                        temSet.DataBuffer[5]  = (byte)(iLen >> 16);
                        temSet.DataBuffer[6]  = (byte)(iLen >> 8);
                        temSet.DataBuffer[7]  = (byte)iLen;
                        temSet.DataBuffer[11] = 2;
                        for (int i = 0; i < 10; i++)
                        {
                            temSet.DataBuffer[i + 16] = FINSHeader[i];
                        }
                        temSet.DataBuffer[26] = 1;
                        temSet.DataBuffer[27] = 2;
                        temSet.DataBuffer[28] = 2;

                        temSet.DataBuffer[29] = (byte)(iByte >> 8);
                        temSet.DataBuffer[30] = (byte)iByte;
                        temSet.DataBuffer[31] = (byte)(iBit);
                        temSet.DataBuffer[33] = 1;
                        //iValue = 0;
                        temSet.DataBuffer[34] = (byte)iValue;

                        nMsg.sSendMsg = temSet;
                        ListImmSendMsg.Add(nMsg); Debug.WriteLine("SendAODO:DO.ListImmSendMsg");
                        // ListImmSendMsg.Add(SendMag8000);
                        break;

                    case "DO2":    //DO
                        iByte             = nVar.ByteAddr - 1000;
                        iBit              = nVar.BitAddr;
                        temSet.Length     = 35;
                        temSet.DataBuffer = new byte[temSet.Length];
                        for (int i = 0; i < 4; i++)
                        {
                            temSet.DataBuffer[i] = FINSTCPHeader[i];
                        }
                        iLen = temSet.Length - 8;
                        temSet.DataBuffer[4]  = (byte)(iLen >> 24);
                        temSet.DataBuffer[5]  = (byte)(iLen >> 16);
                        temSet.DataBuffer[6]  = (byte)(iLen >> 8);
                        temSet.DataBuffer[7]  = (byte)iLen;
                        temSet.DataBuffer[11] = 2;
                        for (int i = 0; i < 10; i++)
                        {
                            temSet.DataBuffer[i + 16] = FINSHeader[i];
                        }
                        temSet.DataBuffer[26] = 1;
                        temSet.DataBuffer[27] = 2;
                        temSet.DataBuffer[28] = 2;

                        temSet.DataBuffer[29] = (byte)(iByte >> 8);
                        temSet.DataBuffer[30] = (byte)iByte;
                        temSet.DataBuffer[31] = (byte)(iBit);
                        temSet.DataBuffer[33] = 1;
                        //iValue = 0;
                        temSet.DataBuffer[34] = (byte)iValue;

                        nMsg.sSendMsg = temSet;
                        ListImmSendMsg.Add(nMsg); Debug.WriteLine("SendAODO:DO2.ListImmSendMsg");
                        // ListImmSendMsg.Add(SendMag8000);
                        break;
                    }
                }
            }
        }
Пример #4
0
        private void GetVarValue(CMessage nMsg, byte[] Datebuff)
        {
            lock (this)
            {
                foreach (CVar nVar in nMsg.ListMsgVar.Values)
                {
                    int   iIndex = nVar.ByteAddr - Convert.ToInt32(nMsg.Starting);
                    int   iBit = nVar.BitAddr;
                    int   iLen = nVar.Length;
                    Int64 Value = 0, Value1 = 0;

                    switch (Convert.ToInt32(nMsg.Function))
                    {
                    case 1:
                    case 2:
                        int nByte = (iIndex >> 3) + 3;
                        Value = (Datebuff[nByte] >> (iIndex % 8)) & 1;
                        nVar.SetValue(Value);
                        break;

                    case 3:
                    case 4:
                        nByte = iIndex * 2 + 3;
                        if (iLen <= 16)
                        {
                            if (nVar.bHighLow == 1)
                            {
                                Value1 = (Datebuff[nByte] << 8) + Datebuff[nByte + 1];
                            }
                            else
                            {
                                Value1 = Datebuff[nByte] + (Datebuff[nByte + 1] << 8);
                            }
                            Value = (Value1 >> iBit) & ((1 << iLen) - 1);
                        }
                        else if (iLen <= 32)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                Value1 += Datebuff[nByte + nVar.bHighByte[i]] << (8 * i);
                            }
                            if (iLen < 32)
                            {
                                Value = (Value1 >> iBit) & ((1 << iLen) - 1);
                            }
                            else
                            {
                                Value = Value1;
                            }
                        }
                        else
                        {
                            Value = 0;
                        }
                        nVar.SetValue(Value);
                        break;

                    case 5:
                    case 6:

                        break;

                    case 7:
                        break;

                    default:
                        break;
                    }
                }
            }
        }
Пример #5
0
        //从工程中加载所有设备所有属性信息,包括变量,报文,图案等
        public bool GetDevicesALLInfo()
        {
            //先加载设备信息
            foreach (CDevice cnode in ListDevice)
            {
                cnode.ListDevVar.Clear();
            }

            //加载设备变量(CDeviceVar,categogy = 0)
            XmlDocument MyXmlDoc = new XmlDocument();

            MyXmlDoc.Load(sIOPath);

            string     xpath     = "IO/VariableInf_Table";
            XmlElement childNode = (XmlElement)MyXmlDoc.SelectSingleNode(xpath);

            foreach (XmlElement item in childNode.ChildNodes)
            {
                foreach (CDevice cnode in ListDevice)
                {
                    if (cnode.Driver == item.GetAttribute("Driver"))
                    {
                        CVar node = new CVar();
                        node.LoadFromNode(item);
                        cnode.ListDevVar.Add(node);
                    }
                }
            }

            //CMessage 请求读报文
            MyXmlDoc.Load(sIOPath);

            xpath     = "IO/MessageInf_Table";
            childNode = (XmlElement)MyXmlDoc.SelectSingleNode(xpath);
            foreach (XmlElement item in childNode.ChildNodes)
            {
                foreach (CDevice nDev in ListDevice)
                {
                    if (nDev.Driver == item.GetAttribute("Driver"))
                    {
                        CMessage nMsg = new CMessage();
                        nMsg.LoadFromNode(item);
                        switch (nMsg.MsgType)
                        {
                        case EMsgType.Msg_Loop:
                            nDev.ListMsgLoop.Add(nMsg);
                            break;

                        case EMsgType.Msg_Time:
                            nDev.ListMsgTime.Add(nMsg);
                            break;

                        case EMsgType.Msg_Call:
                            nDev.ListMsgCall.Add(nMsg);
                            break;

                        default:
                            nDev.ListMsgOther.Add(nMsg);
                            break;
                        }
                    }
                }
            }
            return(true);
        }
Пример #6
0
        private void GetVarValue(CMessage nMsg, byte[] Datebuff)
        {
            lock (this)
            {
                foreach (CVar nVar in nMsg.ListMsgVar.Values)
                {
                    try
                    {
                        int   iIndex = nVar.ByteAddr - Convert.ToInt32(nMsg.Starting);
                        int   iBit = Convert.ToInt32(nVar.BitAddr);
                        int   iLen = nVar.Length;
                        Int64 Value = 0, Value1 = 0;

                        switch (Convert.ToInt32(nMsg.Function))
                        {
                        case 1:
                        case 2:
                            //     00 01 02 03 04 05 06 07 08 09 10 11
                            //(006)06 02 00 00 00 99
                            //(023)06 02 14 EC 9B CA 85 8E 35 EA 5D D7 5F 27 C3 A5 08 8E 3C F1 39 25 00
                            int nByte = (iIndex >> 3) + 3;
                            Value = (Datebuff[nByte] >> (iIndex % 8)) & 1;
                            nVar.SetValue(Value);
                            break;

                        case 3:
                        case 4:
                            //     00 01 02 03 04 05 06 07 08 09 10 11
                            //(006)03 03 00 41 00 40
                            //(131)03 03 80 00 55 00 72 00 77 ......
                            nByte = iIndex * 2 + 3;
                            if (iLen <= 16)
                            {
                                if (nVar.bHighLow == 1)
                                {
                                    Value1 = (Datebuff[nByte] << 8) + Datebuff[nByte + 1];
                                }
                                else
                                {
                                    Value1 = Datebuff[nByte] + (Datebuff[nByte + 1] << 8);
                                }
                                Value = (Value1 >> iBit) & ((1 << iLen) - 1);
                            }
                            else if (iLen <= 32)
                            {
                                if (nVar.bHighLow == 1)
                                {
                                    Value1 = (Datebuff[nByte] << 24) + (Datebuff[nByte + 1] << 16) + (Datebuff[nByte + 2] << 8) + Datebuff[nByte + 3];
                                }
                                else
                                {
                                    Value1 = Datebuff[nByte] + (Datebuff[nByte + 1] << 8) + (Datebuff[nByte + 2] << 16) + (Datebuff[nByte + 3] << 24);
                                }
                                if (iLen < 32)
                                {
                                    Value = (Value1 >> iBit) & ((1 << iLen) - 1);
                                }
                                else
                                {
                                    Value = Value1;
                                }
                            }
                            else
                            {
                                Value = 0;
                            }
                            nVar.SetValue(Value);
                            break;

                        case 5:
                        case 6:
                            //(006)05 05 00 6E FF 00
                            //(006)05 05 00 6E FF 00
                            break;

                        case 7:
                            break;

                        default:
                            break;
                        }
                    }
                    catch (Exception ee)
                    {
                        nVar.SetValue(0);
                    }
                }
            }
        }
Пример #7
0
        public bool PortDataRecv(CMessage sSend, byte[] cRecv, int iRecvLen)
        {
            //     00 01 02 03 04 05 06 07 08 09 10 11
            //(012)00 07 00 00 00 06 03 03 00 41 00 40
            //(137)00 07 00 00 00 83 03 03 80 00 55 00 72 00 77 .......

            //(012)01 2F 00 00 00 06 06 02 00 00 00 99
            //(029)01 2F 00 00 00 17 06 02 14 EC 9B CA 85 8E 35 EA 5D D7 5F 27 C3 A5 08 8E 3C F1 39 25 00
            //(012)49 A4 00 00 00 06 05 05 00 6E FF 00
            //(012)49 A4 00 00 00 06 05 05 00 6E FF 00
            //(012)49 E5 00 00 00 06 05 06 00 11 00 00
            //(012)49 E5 00 00 00 06 05 06 00 11 00 00

            try
            {
                if (iRecvLen < 10)
                {
                    ListStrMsg.Add("PortDataRecv:false:iRecvLen < 10");
                    return(false);
                }
                //Debug.WriteLine(iSendNum.ToString());
                for (int i = 0; i < iRecvLen - 10; i++)
                {
                    if (cRecv[i] * 256 + cRecv[i + 1] == iSendNum)
                    {
                        int iLen = cRecv[i + 5];
                        if (iRecvLen - i < iLen + 6)
                        {
                            ListStrMsg.Add("PortDataRecv:false:iRecvLen - i < iLen + 6");
                            return(false);
                        }

                        if (CommStateE == ECommSatate.Failure)
                        {
                            SaveDebugMsg("正常");
                            CommStateE = ECommSatate.Normal;//子站置通信恢复
                            CAlarmMsgEventArgs ee = new CAlarmMsgEventArgs();
                            ee.Date_Time  = DateTime.Now;
                            ee.Recorder   = Description + "通信恢复";
                            ee.priority   = EAlarmPriority.PRIORITY_1;
                            ee.eAlarmType = EAlarmType.StationState;
                            ee.StaName    = Name;
                            staAlarm.OnAlarmEvent(ee);
                        }
                        else if (CommStateE != ECommSatate.Normal)
                        {
                            SaveDebugMsg("正常");
                            CommStateE = ECommSatate.Normal;//子站置通信恢复
                        }
                        DelayTime          = 0;
                        present_MsgFailRep = 0;
                        present_DevFailNum = 0;

                        byte[] Datebuff = new byte[iLen];
                        Array.Copy(cRecv, i + 6, Datebuff, 0, iLen);

                        GetVarValue(sSend, Datebuff);
                        return(true);
                    }
                }
                ListStrMsg.Add("PortDataRecv:false:cRecv[i] * 256 + cRecv[i + 1] == iSendNum");
                return(false);
            }
            catch (Exception ex)
            {
                ListStrMsg.Add("CProtcolModbusTCP.PortDataRecv:" + ex.Message);
                Debug.WriteLine("CProtcolModbusTCP.PortDataRecv:" + ex.Message);
                string sShow = "";
                for (int i = 0; i < iRecvLen; i++)
                {
                    sShow += cRecv[i].ToString("X2") + " ";
                }
                ListStrMsg.Add(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow);
                Debug.WriteLine(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow);
                return(false);
            }
        }