C# HSMS 통신 로그 부 예제

2021. 8. 10. 10:39프로그래밍

반응형

이전 작업한 HSMS 드라이브에서 로그 기록 부분을 올립니다.

 

HSMS 사양에 맞춰 파싱하여 기록하는 구조입니다.

 

참고바랍니다.

 

public partial class PCOM_Net
    {
        /// <summary>
        /// PCOM LOG SET Method
        /// </summary>
        /// <param name="nMsgID">MessageID</param>
        /// <param name="sLog">Log Write Var</param>
        /// <param name="bDir"> Receive = true, Send = false </param>
        private void SetPcomLog(int nMsgID, ref string sLog, bool bDir)
        {
            clsSECSMessage csm;
            string btemp = string.Empty;                      
            StringBuilder sb = new StringBuilder();
            int LengthByte = 0;
            int nLen = 0;

            try
            {
                csm = clsDataManager.MessageList[nMsgID];
                btemp = Convert.ToString(csm.dataBuffer[0], 2).PadLeft(8, '0');
                LengthByte = Convert.ToInt32(btemp.Substring(6, 2), 2);
                nLen = clsMSG_PARSE.getItemCnt(csm.dataBuffer, LengthByte);

                if (bDir == true)
                {
                    sb.Append("[SECS-II:Rcv]" + Environment.NewLine);
                }
                else
                {
                    sb.Append("[SECS-II:Send]" + Environment.NewLine);
                }
                sb.Append("\t" + "S" + csm.Stream + ", F" + csm.Function);
                sb.Append(" [" + "],");     
                sb.Append(" Device ID = " + csm.DeviceID + ",");
                sb.Append(" WBit = " + csm.Wbit + ",");
                sb.Append(" SystemBytes = " + csm.SystemByte + Environment.NewLine);

                DBLogParsing(nMsgID, csm.dataBuffer, ref sb, 0);

                sLog = sb.ToString();

            }
            catch (Exception ex)
            {
                clsMSGLOG.SetLOG(MSGLOGTYPE.SYSTEMERROR, ex.ToString());
            }
        }

        /// <summary>
        /// databuffer에 있는 Item들을 Log형식으로 추출 (Recursive)
        /// </summary>
        /// <param name="nMsgID">MessageID</param>
        /// <param name="dataBuffer">SECS-II Message Databuffer</param>
        /// <param name="sb">Log Write Var</param>
        /// <param name="tabCnt">Log Spacing Count</param>
        private void DBLogParsing(int nMsgID, List<byte> dataBuffer, ref StringBuilder sb, int tabCnt)
        {
            string btemp = string.Empty;
            string sAscii = string.Empty;            
            int LengthByte = 0;
            int nLen = 0;

            btemp = Convert.ToString(dataBuffer[0], 2).PadLeft(8, '0');
            LengthByte = Convert.ToInt32(btemp.Substring(6, 2), 2);
            nLen = clsMSG_PARSE.getItemCnt(dataBuffer, LengthByte);

            for (int nLoop = 0; nLoop <= tabCnt; nLoop++)
            {
                sb.Append("\t");
            }
            if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_List))
            {
                GetListItem(nMsgID, ref nLen); //check
                sb.Append("LIST, " + nLen + Environment.NewLine);
                tabCnt++;
                if (dataBuffer.Count > 0) // Count만 사용 하므로 int로 대체.
                {
                    for (int nLoop = 0; nLoop < nLen; nLoop++)
                    {
                        DBLogParsing(nMsgID, dataBuffer, ref sb, tabCnt);
                    }
                    tabCnt--;
                }
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_ASCII))
            {
                GetAsciiItem(nMsgID, ref sAscii, ref nLen); //check
                sb.Append("ASCII,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<" + sAscii + ">" + Environment.NewLine);               
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_Binary))
            {
                byte[] aBinary = new byte[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetBinaryItem(nMsgID, aBinary, ref nLen); //check
                sb.Append("BINARY,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if(aBinary.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aBinary.Length; nLoop++)
                    {
                        sb.Append(aBinary[nLoop]);
                        if(nLoop + 1 != aBinary.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_Boolean))
            {
                bool[] aBool = new bool[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetBoolItem(nMsgID, aBool, ref nLen); //check
                sb.Append("BOOL,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aBool.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aBool.Length; nLoop++)
                    {
                        sb.Append(aBool[nLoop]);
                        if (nLoop + 1 != aBool.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_JIS8))
            {
                GetJis8Item(nMsgID, ref sAscii, ref nLen); //check
                sb.Append("JIS8,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<" + sAscii + ">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_1byteSInt))
            {
                sbyte[] aSbyte = new sbyte[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetI1Item(nMsgID, aSbyte, ref nLen);
                sb.Append("INT1,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aSbyte.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aSbyte.Length; nLoop++)
                    {
                        sb.Append(aSbyte[nLoop]);
                        if (nLoop + 1 != aSbyte.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_1byteUInt))
            {
                byte[] aByte = new byte[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetU1Item(nMsgID, aByte, ref nLen);
                sb.Append("UINT1,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aByte.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aByte.Length; nLoop++)
                    {
                        sb.Append(aByte[nLoop]);
                        if (nLoop + 1 != aByte.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_2byteSInt))
            {
                short[] aShort = new short[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetI2Item(nMsgID, aShort, ref nLen);
                sb.Append("INT2,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aShort.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aShort.Length; nLoop++)
                    {
                        sb.Append(aShort[nLoop]);
                        if (nLoop + 1 != aShort.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_2byteUInt))
            {
                ushort[] aUshort = new ushort[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetU2Item(nMsgID, aUshort, ref nLen);
                sb.Append("UINT2,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aUshort.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aUshort.Length; nLoop++)
                    {
                        sb.Append(aUshort[nLoop]);
                        if (nLoop + 1 != aUshort.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_4byteSInt))
            {
                int[] aInt = new int[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetI4Item(nMsgID, aInt, ref nLen);
                sb.Append("INT4,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aInt.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aInt.Length; nLoop++)
                    {
                        sb.Append(aInt[nLoop]);
                        if (nLoop + 1 != aInt.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_4byteUInt))
            {
                uint[] aUint = new uint[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetU4Item(nMsgID, aUint, ref nLen);
                sb.Append("UINT4,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aUint.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aUint.Length; nLoop++)
                    {
                        sb.Append(aUint[nLoop]);
                        if (nLoop + 1 != aUint.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_8byteSInt))
            {
                long[] aLong = new long[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetI8Item(nMsgID, aLong, ref nLen);
                sb.Append("INT8,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aLong.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aLong.Length; nLoop++)
                    {
                        sb.Append(aLong[nLoop]);
                        if (nLoop + 1 != aLong.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_8byteUInt))
            {
                ulong[] aUlong = new ulong[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetU8Item(nMsgID, aUlong, ref nLen);
                sb.Append("UINT8,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aUlong.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aUlong.Length; nLoop++)
                    {
                        sb.Append(aUlong[nLoop]);
                        if (nLoop + 1 != aUlong.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_4byteFloat))
            {
                float[] aFloat = new float[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetF4Item(nMsgID, aFloat, ref nLen);
                sb.Append("FLOAT4,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aFloat.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aFloat.Length; nLoop++)
                    {
                        sb.Append(aFloat[nLoop]);
                        if (nLoop + 1 != aFloat.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
            else if (btemp.Substring(0, 6) == StringEnum.GetStringValue(FormatCode.FormatCode_8byteFloat))
            {
                double[] aDouble = new double[Convert.ToInt32(GetCurrentItemCount(nMsgID))];
                GetF8Item(nMsgID, aDouble, ref nLen);
                sb.Append("FLOAT8,\t\t\t" + nLen + ",\t" + "[" + "]\t" + "<");
                if (aDouble.Length > 0)
                {
                    for (int nLoop = 0; nLoop < aDouble.Length; nLoop++)
                    {
                        sb.Append(aDouble[nLoop]);
                        if (nLoop + 1 != aDouble.Length)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append(">" + Environment.NewLine);
            }
        }
    }
반응형