public static void ScanThread(object objGroupName)
        {
            string      strGroupName = (string)objGroupName;
            HiPerfTimer timer        = new HiPerfTimer();

            foreach (AlarmBitGroup alarmBitGroup in alarmPlcType.PlcGroupDic[strGroupName].bitGroupList)
            {
                alarmBitGroup.MakeStartAndEndAddr();
            }
            while (true)
            {
                timer.Start();
                try
                {
                    foreach (AlarmBitGroup alarmBitGroup in alarmPlcType.PlcGroupDic[strGroupName].bitGroupList)
                    {
                        alarmBitGroup.AlarmCheck();
                        System.Threading.Thread.Sleep(200);
                    }
                }
                catch
                {
                }

                System.Threading.Thread.Sleep(200);
                //PLCScanTime[strGroupName] = timer.Duration;
                //strScanTime = timer.Duration.ToString("0.0000");
            }
        }
        internal PLCResponse SetWordValue(string[] strArray, PLCDataType dataType, string strValue)
        {
            string      strMessageValue = ConvertValueToMessage(strValue, dataType);
            string      strCommand      = "@" + strArray[0] + "FA000000000" + "0102" + strArray[1].Substring(4) + strMessageValue;
            string      strFcs          = CreateFcsCodeFun(strCommand);
            string      strEndCode      = "*" + "\r";
            string      strMessage      = strCommand + strFcs + strEndCode;
            string      strRecieve;
            HiPerfTimer timeM = new HiPerfTimer();

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    serialPort.Write(strMessage);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                //strRecieve = strRecieve.Trim();
                                if (strRecieve.Length != 27)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, 23);
                                string strFcsReturn     = strRecieve.Substring(23, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch//(Exception ex)
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }
        public static void ScanThread(object objGroupName)
        {
            string      strGroupName = (string)objGroupName;
            HiPerfTimer timer        = new HiPerfTimer();

            while (true)
            {
                timer.Start();
                BitCtrlGroupUnit  bitCtrlGroupUnit  = new BitCtrlGroupUnit();
                WordCtrlGroupUnit wordCtrlGroupUnit = new WordCtrlGroupUnit();
                try
                {
                    foreach (IControlPLC plsControl in PLCControlGroup[strGroupName].PLCControls)
                    {
                        if (plsControl.GetType() == typeof(BitButton))
                        {
                            BitButton bitControl = (BitButton)plsControl;
                            if (bitControl.Visible || bitControl.AlwayFresh)
                            {
                                bitCtrlGroupUnit.addItemToGroup(bitControl);
                            }
                        }
                        if (plsControl.GetType() == typeof(DWordTextBox))
                        {
                            DWordTextBox wordControl = (DWordTextBox)plsControl;
                            if (wordControl.Visible)
                            {
                                wordCtrlGroupUnit.addItemToGroup(wordControl);
                            }
                        }
                    }
                    foreach (BitCtrlGroupItem bitCtrlItem in bitCtrlGroupUnit.bitGroupList)
                    {
                        bitCtrlItem.MakeStartAndEndAddr();
                        bitCtrlItem.UpdateGroupStatus();
                        //System.Threading.Thread.Sleep(10);
                    }
                    foreach (WordCtrlGroupItem wordCtrlItem in wordCtrlGroupUnit.wordGroupList)
                    {
                        wordCtrlItem.MakeStartAndEndAddr();
                        wordCtrlItem.UpdateGroupStatus();
                        //System.Threading.Thread.Sleep(10);
                    }
                }
                catch
                {
                }

                System.Threading.Thread.Sleep(1);
                PLCScanTime[strGroupName] = timer.Duration;
                //strScanTime = timer.Duration.ToString("0.0000");
            }
        }
        public void UpdateGroupStatus()
        {
            string strValue = "";
            int    iWordNo  = 0;
            int    iBitNo   = 0;
            int    iIndex   = 0;

            WorldGeneralLib.PLC.PLCResponse response = 0;
            response = PLC.GetWordS(strPlcName, strWordStartAddress, strWordEndAddress, ref strValue);
            if (response == WorldGeneralLib.PLC.PLCResponse.SUCCESS)
            {
                //01 C 00000 01
                HiPerfTimer timeTemp = new HiPerfTimer();
                timeTemp.Start();
                foreach (KeyValuePair <string, BitButton> keyValuePair in bitCtrlItemDis)
                {
                    iWordNo = int.Parse(keyValuePair.Key.Substring(3, 5));
                    iBitNo  = int.Parse(keyValuePair.Key.Substring(8, 2));
                    iIndex  = (iWordNo - iStartAddr) * 16 + iBitNo;
                    keyValuePair.Value.m_plcRes = response;
                    if (strValue[iIndex] == '1')
                    {
                        keyValuePair.Value.SetDriverStatus(true);
                    }
                    else
                    {
                        keyValuePair.Value.SetDriverStatus(false);
                    }
                }
                foreach (KeyValuePair <string, BitButton> keyValuePair in bitCtrlItemDis)
                {
                    keyValuePair.Value.FreshDriverStatus();
                }
                double Time = timeTemp.Duration;
            }
            else
            {
                foreach (KeyValuePair <string, BitButton> keyValuePair in bitCtrlItemDis)
                {
                    keyValuePair.Value.m_plcRes = response;
                }
                foreach (KeyValuePair <string, BitButton> keyValuePair in bitCtrlItemDis)
                {
                    keyValuePair.Value.FreshDriverStatus();
                }
            }
        }
Beispiel #5
0
        public void UpdateGroupStatus()
        {
            string strValue = "";
            int    iWordNo  = 0;
            int    iIndex   = 0;

            WorldGeneralLib.PLC.PLCResponse response = 0;
            response = PLC.GetWordSS(strPlcName, strWordStartAddress, strWordEndAddress, ref strValue);
            if (response == WorldGeneralLib.PLC.PLCResponse.SUCCESS)
            {
                //01 C 00000 01
                HiPerfTimer timeTemp = new HiPerfTimer();
                timeTemp.Start();
                foreach (KeyValuePair <string, DWordTextBox> keyValuePair in wordCtrlItemDis)
                {
                    iWordNo = int.Parse(keyValuePair.Key.Substring(3, 5));
                    iIndex  = (iWordNo - iStartAddr) * 4;
                    keyValuePair.Value.m_plcRes = response;
                    keyValuePair.Value.SetDriverStatus(strValue.Substring(iIndex, 8));
                }
                foreach (KeyValuePair <string, DWordTextBox> keyValuePair in wordCtrlItemDis)
                {
                    keyValuePair.Value.FreshDriverStatus();
                }
                double Time = timeTemp.Duration;
            }
            else
            {
                foreach (KeyValuePair <string, DWordTextBox> keyValuePair in wordCtrlItemDis)
                {
                    keyValuePair.Value.m_plcRes = response;
                }
                foreach (KeyValuePair <string, DWordTextBox> keyValuePair in wordCtrlItemDis)
                {
                    keyValuePair.Value.FreshDriverStatus();
                }
            }
        }
        internal PLCResponse GetWordSSValue(string strMessage, int iWordCount, out string strValue)
        {
            strValue = "";

            string      strCommand     = strMessage;
            string      strFcs         = CreateFcsCodeFun(strCommand);
            string      strEndCode     = "*" + "\r";
            string      strMessageSend = strCommand + strFcs + strEndCode;
            string      strRecieve;
            HiPerfTimer timeM = new HiPerfTimer();

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    serialPort.Write(strMessageSend);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                //strRecieve = strRecieve.Trim();
                                int iLenthNeed = 27 + iWordCount * 4;
                                if (strRecieve.Length != iLenthNeed)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, iLenthNeed - 4);
                                string strFcsReturn     = strRecieve.Substring(iLenthNeed - 4, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                strValue = strRecieve.Substring(23, iWordCount * 4);
                                //strValue = ConvertMesToBit(strValue, iWordCount);
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch//(Exception ex)
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }
        internal PLCResponse GetBitValue(string[] strArray, out bool bOn)
        {
            bOn = false;
            HiPerfTimer timeM      = new HiPerfTimer();
            string      strCommand = "@" + strArray[0] + "FA000000000" + strArray[1];
            string      strFcs     = CreateFcsCodeFun(strCommand);
            string      strEndCode = "*" + "\r";
            string      strMessage = strCommand + strFcs + strEndCode;
            string      strRecieve;

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    serialPort.Write(strMessage);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                //strRecieve = strRecieve.Trim();
                                if (strRecieve.Length != 29)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, 25);
                                string strFcsReturn     = strRecieve.Substring(25, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                if (strRecieve.Substring(23, 2) == "01")
                                {
                                    bOn = true;
                                }
                                else
                                {
                                    bOn = false;
                                }
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }
        internal PLCResponse SetWordValue(string[] strArray, PLCDataType dataType, string strValue)
        {
            //strValue = "";
            HiPerfTimer timeM = new HiPerfTimer();

            strArray[1] = strArray[1].Replace('R', 'W');
            string strMessageValue = ConvertValueToMessage(strValue, dataType);
            string strCommand      = "";

            if (dataType == PLCDataType.BIN16 || dataType == PLCDataType.HEX16 || dataType == PLCDataType.BIT16 || (dataType == PLCDataType.STRING && strValue.Length <= 2))
            {
                strCommand = "%" + strArray[0] + "#" + strArray[1] + strArray[2] + strArray[3] + strArray[4] + strMessageValue + strMessageValue;
            }
            else
            {
                strCommand = "%" + strArray[0] + "#" + strArray[1] + strArray[2] + strArray[3] + strArray[4] + strMessageValue;
            }
            string strFcs     = CreateFcsCodeFun(strCommand);
            string strEndCode = "\r";
            string strMessage = strCommand + strFcs + strEndCode;
            string strRecieve;

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    serialPort.Write(strMessage);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                //strRecieve = strRecieve.Trim();
                                if (strRecieve.IndexOf("%") < 0 || strRecieve.IndexOf("$") < 0 || strRecieve.Length != 9)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, 6);
                                string strFcsReturn     = strRecieve.Substring(6, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch//(Exception ex)
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }
        internal PLCResponse GetWordValue(string[] strArray, PLCDataType dataType, out string strValue)
        {
            strValue = "";
            HiPerfTimer timeM      = new HiPerfTimer();
            string      strCommand = "%" + strArray[0] + "#" + strArray[1] + strArray[2] + strArray[3] + strArray[4];
            string      strFcs     = CreateFcsCodeFun(strCommand);
            string      strEndCode = "\r";
            string      strMessage = strCommand + strFcs + strEndCode;
            string      strRecieve;

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    System.Threading.Thread.Sleep(1);
                    serialPort.Write(strMessage);
                    System.Threading.Thread.Sleep(1);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                //strRecieve = strRecieve.Trim();
                                if (strRecieve.IndexOf("%") < 0 || strRecieve.IndexOf("$") < 0 || strRecieve.Length != 17)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, 14);
                                string strFcsReturn     = strRecieve.Substring(14, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                strValue = strRecieve.Substring(6, 8);
                                strValue = ConvertMesToValue(strValue, dataType);
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch//(Exception ex)
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }
        internal PLCResponse SetBitValue(string[] strArray, bool bOn)
        {
            HiPerfTimer timeM = new HiPerfTimer();
            string      strOn = "0";

            if (bOn)
            {
                strOn = "1";
            }
            else
            {
                strOn = "0";
            }
            string strCommand = "%" + strArray[0] + "#WCS" + strArray[1] + strOn;
            string strFcs     = CreateFcsCodeFun(strCommand);
            string strEndCode = "\r";
            string strMessage = strCommand + strFcs + strEndCode;
            string strRecieve;

            lock (objLock)
            {
                #region 通信
                try
                {
                    serialPort.ReadExisting();
                    serialPort.Write(strMessage);
                    timeM.Start();
                    strRecieve = serialPort.ReadExisting();
                    while (true)
                    {
                        try
                        {
                            strRecieve = strRecieve + serialPort.ReadExisting();
                            if (strRecieve.IndexOf("\r") > -1)
                            {
                                if (strRecieve.IndexOf("%") < 0 || strRecieve.IndexOf("$") < 0 || strRecieve.Length != 9)
                                {
                                    return(PLCResponse.ERROR);
                                }
                                string strMessageReturn = strRecieve.Substring(0, 6);
                                string strFcsReturn     = strRecieve.Substring(6, 2);
                                strFcs = CreateFcsCodeFun(strMessageReturn);
                                if (strFcs != strFcsReturn)
                                {
                                    return(PLCResponse.FCSERR);
                                }
                                break;
                            }
                            if (timeM.TimeUp(0.5))
                            {
                                return(PLCResponse.TIMEOUT);
                            }
                        }
                        catch
                        {
                            return(PLCResponse.OTHERS);
                        }
                        System.Threading.Thread.Sleep(1);
                    }
                }
                catch
                {
                    return(PLCResponse.OTHERS);
                }
                #endregion
            }
            return(PLCResponse.SUCCESS);
        }