Exemple #1
0
        public static void UpdateStringToBits(PlcMemArea memArea, string S)
        {
            string MemArea = null;

            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemArea = "C";
                break;

            case PlcMemArea.WR:
                MemArea = "W";
                break;

            case PlcMemArea.HR:
                MemArea = "H";
                break;

            case PlcMemArea.AR:
                MemArea = "A";
                break;

            case PlcMemArea.DM:
                MemArea = "D";
                break;

            case PlcMemArea.WR_bit:
                MemArea = "W";
                break;

            case PlcMemArea.HR_bit:
                MemArea = "H";
                break;

            default:
                break;
            }
            if (S.Length == 512 * OmronFINsClass.Width_Word)
            {
                int startingId = 0;
                if (memArea == PlcMemArea.HR_bit)
                {
                    startingId = OmronFINsClass.StartingId_HR;
                }
                else if (memArea == PlcMemArea.WR_bit)
                {
                    startingId = OmronFINsClass.StartingId_WR;
                }
                for (int i = 0; i < S.Length / OmronFINsClass.Width_Word; i++)
                {
                    for (int j = 0; j < OmronFINsClass.Width_Word; j++)
                    {
                        PlcDataMapper.PlcBits.Find(b => b.MemArea == MemArea &&
                                                   b.WordAddress == startingId + i &&
                                                   b.BitAddress == j).SetValue(S.Substring(OmronFINsClass.Width_Word * i + j, 1));
                    }
                }
            }
        }
Exemple #2
0
        public static List <PlcWordModel> ConvWordStrToWordList(PlcMemArea memArea, string S)
        {
            if (S.Length > 512 * 4)
            {
                return(null);
            }
            else
            {
                List <PlcWordModel> retList = new List <PlcWordModel>();

                for (int i = 0; i < S.Length / 4 - 1; i++)
                {
                    retList.Add(new PlcWordModel(memArea, i, S.Substring(4 + 4 * i, 4)));
                }
                return(retList);
            }
        }
Exemple #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="memArea">Bit memory area, WR or HR</param>
        /// <param name="wordAddress">Word address that bit is belonged to 0-511</param>
        /// <param name="bitAddress">Bit address 0-15</param>
        /// <param name="value">Bit value 0 or 1</param>
        public PlcBitModel(PlcMemArea memArea, int wordAddress, int bitAddress, int value)
        {
            if (wordAddress < OmronFINsClass.Size_WR && bitAddress < OmronFINsClass.Width_Word)
            {
                switch (memArea)
                {
                case PlcMemArea.CIO:
                    MemArea = "C";
                    break;

                case PlcMemArea.WR:
                    MemArea = "W";
                    break;

                case PlcMemArea.HR:
                    MemArea = "H";
                    break;

                case PlcMemArea.AR:
                    MemArea = "A";
                    break;

                case PlcMemArea.DM:
                    MemArea = "D";
                    break;

                case PlcMemArea.WR_bit:
                    MemArea = "W";
                    break;

                default:
                    break;
                }
                WordAddress    = wordAddress;
                BitAddress     = bitAddress;
                WordAddressStr = OmronFINsProcessor.HexConvert10(wordAddress.ToString()).PadLeft(4, '0');
                BitAddressStr  = OmronFINsProcessor.HexConvert10(bitAddress.ToString()).PadLeft(2, '0');
                Value          = value;
            }
        }
Exemple #4
0
        public static string GenerateStrFromWords(PlcMemArea memArea)
        {
            string retStr  = null;
            string MemArea = null;

            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemArea = "C";
                break;

            case PlcMemArea.WR:
                MemArea = "W";
                break;

            case PlcMemArea.HR:
                MemArea = "H";
                break;

            case PlcMemArea.AR:
                MemArea = "A";
                break;

            case PlcMemArea.DM:
                MemArea = "D";
                break;

            default:
                break;
            }

            foreach (var word in PlcDataMapper.PlcDMWords)
            {
                if (MemArea == word.MemoryArea)
                {
                    retStr += word.ValueStrHex;
                }
            }
            return(retStr);
        }
Exemple #5
0
        public PlcWordModel(PlcMemArea memArea, int wordAddress, string valueStrHex)
        {
            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemoryArea = "C";
                break;

            case PlcMemArea.WR:
                MemoryArea = "W";
                break;

            case PlcMemArea.HR:
                MemoryArea = "H";
                break;

            case PlcMemArea.AR:
                MemoryArea = "A";
                break;

            case PlcMemArea.DM:
                MemoryArea = "D";
                break;

            default:
                break;
            }
            WordAddress = wordAddress;
            for (int i = 0; i < OmronFINsClass.Width_Word; i++)
            {
                Bits.Add(0);
                plcBits.Add(new PlcBitModel(MemoryArea, wordAddress, i));
            }
            SetValue(valueStrHex);
            SetAllBitsValue();
        }
Exemple #6
0
        public static void WordsToBits(PlcMemArea memArea)
        {
            string MemArea = null;
            int    size    = 0;
            List <PlcWordModel> wordList = new List <PlcWordModel>();

            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemArea = "C";
                break;

            case PlcMemArea.WR:
                MemArea  = "W";
                size     = OmronFINsClass.Size_WR;
                wordList = PlcDataMapper.PlcWRWords;
                break;

            case PlcMemArea.HR:
                MemArea  = "H";
                size     = OmronFINsClass.Size_HR;
                wordList = PlcDataMapper.PlcHRWords;
                break;

            case PlcMemArea.AR:
                MemArea = "A";
                break;

            case PlcMemArea.DM:
                MemArea = "D";
                break;

            case PlcMemArea.WR_bit:
                MemArea  = "W";
                size     = OmronFINsClass.Size_WR;
                wordList = PlcDataMapper.PlcWRWords;
                break;

            case PlcMemArea.HR_bit:
                size     = OmronFINsClass.Size_HR;
                wordList = PlcDataMapper.PlcHRWords;
                MemArea  = "H";
                break;

            default:
                break;
            }

            Stopwatch stopwatch = new Stopwatch();

            foreach (var word in wordList)
            {
                for (int j = 0; j < OmronFINsClass.Width_Word; j++)
                {
                    stopwatch.Start();
                    PlcDataMapper.PlcBits.Find(b => b.MemArea == MemArea &&
                                               b.WordAddress == word.WordAddress && b.BitAddress == j).SetValue(word.Bits[j]);
                    stopwatch.Stop();
                }
                //for (int j = 0; j < OmronFINsClass.Width_Word; j++)
                //{
                //    PlcDataMapper.PlcBits=word.plcBits;
                //}

                //PlcDataMapper.PlcBits.FindAll(b => b.MemArea == MemArea) = word.plcBits;
            }
        }
Exemple #7
0
        public static void UpdateStringToWords(PlcMemArea memArea, SortedList <int, string> sList)
        {
            string MemArea = null;
            List <PlcWordModel> listInUse    = new List <PlcWordModel>();
            List <PlcBitModel>  bitListInUse = new List <PlcBitModel>();

            switch (memArea)
            {
            case PlcMemArea.WR:
                listInUse = PlcDataMapper.PlcWRWords;
                MemArea   = "W";
                break;

            case PlcMemArea.HR:
                listInUse = PlcDataMapper.PlcHRWords;
                MemArea   = "H";
                break;

            case PlcMemArea.DM:
                listInUse = PlcDataMapper.PlcDMWords;
                MemArea   = "D";
                break;

            case PlcMemArea.CIO:
                break;

            case PlcMemArea.WR_bit:
                bitListInUse = PlcDataMapper.PlcBits;
                MemArea      = "W";
                break;

            case PlcMemArea.HR_bit:
                bitListInUse = PlcDataMapper.PlcBits;
                MemArea      = "H";
                break;

            case PlcMemArea.AR:
                break;

            default:
                //listInUse = new List<PlcWordModel>();
                break;
            }

            if (memArea == PlcMemArea.DM)
            {
                foreach (var addrStr in sList)
                {
                    int address = addrStr.Key;
                    for (int i = 0; i < addrStr.Value.Length / 4; i++)
                    {
                        listInUse.Find(x => x.WordAddress == address)
                        .SetValue(addrStr.Value.Substring(4 * i, 4));
                        address++;
                    }
                }
            }
            if (memArea == PlcMemArea.HR_bit || memArea == PlcMemArea.WR_bit)
            {
                foreach (var addrStr in sList)
                {
                    int wordAddress = addrStr.Key;
                    //int bitAddress = 0;
                    int    bitAddress = 15;//WRstr format is MSB->LSB
                    string binStr     = BinaryConvert16(addrStr.Value);
                    for (int i = 0; i < binStr.Length; i++)
                    {
                        PlcBitModel tempBit = PlcDataMapper.PlcBits.FirstOrDefault(x => x.MemArea == MemArea && x.WordAddress == wordAddress &&
                                                                                   x.BitAddress == bitAddress);
                        if (tempBit != null)
                        {
                            tempBit.SetValue(binStr.Substring(i, 1));
                        }
                        bitAddress--;
                        //if (bitAddress==16)
                        if (bitAddress == -1)
                        {
                            wordAddress++;
                            //bitAddress = 0;
                            bitAddress = 15;
                        }
                    }
                }
            }

            //  if (memArea == PlcMemArea.DM)
            // {


            //for (int i = 0; i < S.Length / 4 - 1; i++)
            //{
            //    PlcDataMapper.PlcDMWords.Find(x => x.MemoryArea == MemArea && x.WordAddress == OmronFINsClass.StartingId_DM + i)
            //        .SetValue(S.Substring(4 * i, 4));
            //    retList.Add(new PlcWordModel(memArea, i, S.Substring(4 + 4 * i, 4)));
            //}
            // }
            //else if (memArea == PlcMemArea.HR)
            //{
            //    foreach (var addrStr in sList)
            //    {
            //        int address = addrStr.Key;
            //        for (int i = 0; i < addrStr.Value.Length / 4; i++)
            //        {
            //            PlcDataMapper.PlcHRWords.Find(x => x.WordAddress == address)
            //                .SetValue(addrStr.Value.Substring(4 * i, 4));
            //            address++;
            //        }
            //    }
            //    //for (int i = 0; i < S.Length / 4 - 1; i++)
            //    //{
            //    //    PlcDataMapper.PlcHRWords.Find(x => x.MemoryArea == MemArea && x.WordAddress == OmronFINsClass.StartingId_HR + i)
            //    //        .SetValue(S.Substring(4 * i, 4));
            //    //    //retList.Add(new PlcWordModel(memArea, i, S.Substring(4 + 4 * i, 4)));
            //    //}
            //}
            //else if (memArea == PlcMemArea.WR)
            //{
            //    foreach (var addrStr in sList)
            //    {
            //        int address = addrStr.Key;
            //        for (int i = 0; i < addrStr.Value.Length / 4; i++)
            //        {
            //            PlcDataMapper.PlcWRWords.Find(x => x.WordAddress == address)
            //                .SetValue(addrStr.Value.Substring(4 * i, 4));
            //            address++;
            //        }
            //    }
            //}
        }
Exemple #8
0
        /// <summary>
        /// Generic read data as bits string
        /// </summary>
        /// <param name="MemoryArea">PLC memory area: CIO,WR,HR,AR,DM</param>
        /// <param name="WordAddressBegin">The PLC decimal word address string of first bit to read to reside in</param>
        /// <param name="BitAddressBegin">The PLC decimal bit address, first</param>
        /// <param name="WordAddressEnd">The PLC decimal word address string of last bit to read to reside in</param>
        /// <param name="BitAddressEnd">The PLC decimal bit address, last</param>
        /// <param name="PlcPort">Plc serial port</param>
        /// <returns>Data string read from PLC</returns>
        public static string GenericBitsRdPLC(PlcMemArea MemoryArea, string WordAddressBegin,
                                              string BitAddressBegin, string WordAddressEnd, string BitAddressEnd, SerialPort PlcPort)
        {
            string MA    = null;
            string count = null;

            switch (MemoryArea)
            {
            case PlcMemArea.CIO:
                MA = "B0";
                break;

            case PlcMemArea.WR:
                MA = OmronFINsClass.WR;
                break;

            case PlcMemArea.WR_bit:
                MA = OmronFINsClass.WR_bit;
                break;

            case PlcMemArea.HR:
                MA = "B2";
                break;

            case PlcMemArea.AR:
                MA = "B3";
                break;

            case PlcMemArea.DM:
                MA = "82";
                break;

            case PlcMemArea.HR_bit:
                MA = OmronFINsClass.HR_bit;
                break;

            default:
                break;
            }
            //count = Convert.ToString((Convert.ToInt32(xEndAddress, 16) - Convert.ToInt32(xBeginAddress, 16) + 1), 16).PadLeft(4, '0');
            count = HexConvert10(((int.Parse(WordAddressEnd) - int.Parse(WordAddressBegin)) * OmronFINsClass.Width_Word
                                  + int.Parse(BitAddressEnd) - int.Parse(BitAddressBegin) + 1).ToString()).PadLeft(4, '0');
            string xBeginAddress = HexConvert10(WordAddressBegin).PadLeft(4, '0') + HexConvert10(BitAddressBegin).PadLeft(2, '0');

            try
            {
                SendPLC(OmronFINsClass.FINSh + OmronFINsClass.Rd + MA + xBeginAddress + count, PlcPort);
                Thread.Sleep(10);
                string result = ReadPLC(PlcPort);
                if (result == null)
                {
                    return(null);
                }
                else
                {
                    return(result);
                }
            }
            catch (Exception exp)
            {
                return(null);
            }
        }
Exemple #9
0
        /// <summary>
        /// Generic read data as word string from PLC memory area
        /// </summary>
        /// <param name="MemoryArea">PLC memory area: CIO,WR,HR,AR,DM</param>
        /// <param name="xBeginAddress">The PLC decimal address string of first word to read</param>
        /// <param name="xEndAddress">The PLC decimal address string of last word to read</param>
        /// <param name="PlcPort">PLC serial port</param>
        /// <returns>Data string read from PLC</returns>
        public static string GenericRdPLC(PlcMemArea MemoryArea, string BeginAddress, string EndAddress, System.IO.Ports.SerialPort PlcPort)
        {
            string MA    = null;
            string count = null;

            switch (MemoryArea)
            {
            case PlcMemArea.CIO:
                MA = "B0";
                break;

            case PlcMemArea.WR:
                MA = OmronFINsClass.WR;
                break;

            case PlcMemArea.WR_bit:
                MA = OmronFINsClass.WR_bit;
                break;

            case PlcMemArea.HR:
                MA = "B2";
                break;

            case PlcMemArea.AR:
                MA = "B3";
                break;

            case PlcMemArea.DM:
                MA = "82";
                break;
            }
            //count = Convert.ToString((Convert.ToInt32(xEndAddress, 16) - Convert.ToInt32(xBeginAddress, 16) + 1), 16).PadLeft(4, '0');
            count = HexConvert10((int.Parse(EndAddress) - int.Parse(BeginAddress) + 1).ToString()).PadLeft(4, '0');
            string xBeginAddress = HexConvert10(BeginAddress).PadLeft(4, '0').PadRight(6, '0');

            //= string.Format("{0:x}", BeginAddress).PadRight(6, '0');
            try
            {
                Stopwatch stopwatch = new Stopwatch();

                //string debugSendStr = OmronFINsClass.FINSh + OmronFINsClass.Rd + MA + xBeginAddress + count;

                SendPLC(OmronFINsClass.FINSh + OmronFINsClass.Rd + MA + xBeginAddress + count, PlcPort);


                //Thread.Sleep(10);
                //stopwatch.Start();
                string result = ReadPLC(PlcPort);
                //stopwatch.Stop();
                if (result == null)
                {
                    return(null);
                }
                else
                {
                    return(result);
                }
            }
            catch (Exception exp)
            {
                return(null);
            }
        }
Exemple #10
0
        /// <summary>
        /// Write a single bit data to PLC
        /// </summary>
        /// <param name="MemoryArea">Target memory area</param>
        /// <param name="WordxAddress">Target word hex address</param>
        /// <param name="BitxAddress">Target bit hex address</param>
        /// <param name="value">Value to write 0/1 will be converted to 00/01</param>
        /// <param name="PlcPort">Plc serial port</param>
        /// <param name="expMsg">Exception message</param>
        /// <returns>If successed</returns>
        public static bool BitWrtPLC(PlcMemArea MemoryArea, string WordxAddress, string BitxAddress, int value, SerialPort PlcPort, out string expMsg)
        {
            string MA    = null;
            string count = "0001";

            switch (MemoryArea)
            {
            case PlcMemArea.CIO:
                MA = "B0";
                break;

            case PlcMemArea.WR:
                MA = OmronFINsClass.WR;
                break;

            case PlcMemArea.WR_bit:
                MA = OmronFINsClass.WR_bit;
                break;

            case PlcMemArea.HR:
                MA = "B2";
                break;

            case PlcMemArea.AR:
                MA = "B3";
                break;

            case PlcMemArea.DM:
                MA = "82";
                break;

            case PlcMemArea.HR_bit:
                MA = OmronFINsClass.HR_bit;
                break;

            default:
                break;
            }

            try
            {
                SendPLC(OmronFINsClass.FINSh + OmronFINsClass.Wrt + MA + WordxAddress + BitxAddress
                        + count + value.ToString().PadLeft(2, '0'), PlcPort);
                //Thread.Sleep(10);
                string RXS = ReadPLC(PlcPort);
                if (RXS == "")
                {
                    expMsg = null;
                    return(true);
                }

                else
                {
                    expMsg = RXS;
                    return(false);
                }
            }
            catch (Exception exp)
            {
                expMsg = exp.Message;
                return(false);
            }
        }
Exemple #11
0
        /// <summary>
        /// Generic write data string to PLC memory area
        /// </summary>
        /// <param name="MemoryArea">PLC memory area: CIO,WR,HR,AR,DM</param>
        /// <param name="xBeginAddress">The PLC hex address hhhh of first word to write</param>
        /// <param name="xEndAddress">The PLC hex address hhhh of last word to write</param>
        /// <param name="StoWrt">Data string to be sent</param>
        /// <param name="PlcPort">PLC serial port name</param>
        /// <returns>True: write successful False: Write failed</returns>
        public static bool GenericWrtPLC(PlcMemArea MemoryArea, string BeginAddress, string EndAddress, string StoWrt, SerialPort PlcPort, out string expMsg)
        {
            string MA    = null;
            string count = null;

            switch (MemoryArea)
            {
            case PlcMemArea.CIO:
                MA = "B0";
                break;

            case PlcMemArea.WR:
                MA = OmronFINsClass.WR;
                break;

            case PlcMemArea.WR_bit:
                MA = OmronFINsClass.WR_bit;
                break;

            case PlcMemArea.HR:
                MA = "B2";
                break;

            case PlcMemArea.AR:
                MA = "B3";
                break;

            case PlcMemArea.DM:
                MA = "82";
                break;
            }
            //count = Convert.ToString((Convert.ToInt32(xEndAddress, 16) - Convert.ToInt32(xBeginAddress, 16) + 1), 16).PadLeft(4, '0');
            count = HexConvert10((int.Parse(EndAddress) - int.Parse(BeginAddress) + 1).ToString()).PadLeft(4, '0');
            //xBeginAddress = xBeginAddress.PadRight(6, '0');
            string xBeginAddress = HexConvert10(BeginAddress).PadLeft(4, '0').PadRight(6, '0');

            try
            {
                //Stopwatch stopwatch = new Stopwatch();

                SendPLC(OmronFINsClass.FINSh + OmronFINsClass.Wrt + MA + xBeginAddress + count + StoWrt, PlcPort);//<80ms

                //Thread.Sleep(10);
                //stopwatch.Start();
                string RXS = ReadPLC(PlcPort);
                //stopwatch.Stop();
                if (RXS == "")
                {
                    expMsg = null;
                    return(true);
                }

                else
                {
                    expMsg = RXS;
                    return(false);
                }
            }
            catch (Exception exp)
            {
                expMsg = exp.Message;
                return(false);
            }
        }
Exemple #12
0
        /// <summary>
        /// Create the complete BitModel list, with Word address/str and Bit address/str
        /// </summary>
        /// <param name="plcBitList"></param>
        /// <param name="MemArea"></param>
        /// <param name="startingWordId"></param>
        /// <param name="startingBitId"></param>
        /// <returns></returns>
        public static List <PlcBitModel> PopulatePlcBitList_singleType(List <PlcBitModel> plcBitList, PlcMemArea memArea, int startingWordId, int startingBitId)
        {
            int    listSize;
            string MemArea = null;

            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemArea  = "C";
                listSize = 0;
                break;

            case PlcMemArea.WR:
                MemArea  = "W";
                listSize = OmronFINsClass.Size_WR * OmronFINsClass.Width_Word;
                break;

            case PlcMemArea.HR:
                MemArea  = "H";
                listSize = OmronFINsClass.Size_HR * OmronFINsClass.Width_Word;
                break;

            case PlcMemArea.AR:
                MemArea  = "A";
                listSize = 0;
                break;

            case PlcMemArea.DM:
                MemArea  = "D";
                listSize = 0;
                break;

            default:
                MemArea  = "";
                listSize = 0;
                break;
            }

            List <PlcBitModel> retPlcBitList = CreateList <PlcBitModel>(listSize);

            foreach (var item in retPlcBitList)
            {
                if (startingBitId == OmronFINsClass.Width_Word)
                {
                    startingBitId = 0;
                    startingWordId++;
                }
                item.SetMemArea(MemArea);
                item.SetWordAddress(startingWordId);
                item.SetBitAddress(startingBitId);
                item.SetAddressStr();

                startingBitId++;
            }
            plcBitList.AddRange(retPlcBitList);
            return(plcBitList);
        }
Exemple #13
0
        /// <summary>
        /// Populate the full PCL Word list of a certain memory area
        /// </summary>
        /// <param name="plcWordList">The PLC Word list to be filled</param>
        /// <param name="MemArea">Word memory area</param>
        /// <param name="startingId">Starting Word address</param>
        /// <returns></returns>
        public static List <PlcWordModel> PopulatePlcWordList_singleType(List <PlcWordModel> plcWordList, PlcMemArea memArea, int startingId)
        {
            int    listSize;
            string MemArea = null;

            switch (memArea)
            {
            case PlcMemArea.CIO:
                MemArea  = "C";
                listSize = 0;
                break;

            case PlcMemArea.WR:
                MemArea  = "W";
                listSize = OmronFINsClass.Size_WR;
                break;

            case PlcMemArea.HR:
                MemArea  = "H";
                listSize = OmronFINsClass.Size_HR;
                break;

            case PlcMemArea.AR:
                MemArea  = "A";
                listSize = 0;
                break;

            case PlcMemArea.DM:
                MemArea  = "D";
                listSize = OmronFINsClass.Size_DM;
                break;

            default:
                MemArea  = "";
                listSize = 0;
                break;
            }


            List <PlcWordModel> retPlcWordList = CreateList <PlcWordModel>(listSize);

            foreach (var item in retPlcWordList)
            {
                item.SetMemoryArea(MemArea);
                item.SetWordAddress(startingId);
                startingId++;
            }
            plcWordList.AddRange(retPlcWordList);
            return(plcWordList);
        }