예제 #1
0
        //어플리케이션 데이터 READ 포맷 만들기
        private byte[] CreateReadDataFormat
            (XGT_Request_Func emFunc, XGT_DataType emDatatype, List <XgtAddressData> pAddressList, XGT_MemoryType emMemtype, int pDataCount)
        {
            List <XgtAddressData> lstAddress = new List <XgtAddressData>();
            int vLenth = 0;                                                       //데이타 포맷 프레임의 크기

            byte[] command  = BitConverter.GetBytes((short)emFunc);               //StringToByteArray((int)emFunc, true);  //명령어 읽기,쓰기
            byte[] dataType = BitConverter.GetBytes((short)emDatatype);           //StringToByteArray((int)emDatatype, true);  //데이터 타입

            byte[] reserved   = BitConverter.GetBytes((short)0);                  //예약영역 고정(0x0000)
            byte[] blockcount = BitConverter.GetBytes((short)pAddressList.Count); //블록수

            //프레임 크기 설정 :  명령어(2) + 데이터타입(2) + 예약영역(2) + 블록수 (?) + 변수길이(?) + 변수(?)
            vLenth = command.Length + dataType.Length + reserved.Length + blockcount.Length;

            foreach (XgtAddressData addr in pAddressList)
            {
                string vAddress = CreateValueName(emDatatype, emMemtype, addr.Address);

                //byte[] value = Encoding.ASCII.GetBytes(vAddress);
                //byte[] valueLength = BitConverter.GetBytes((short)value.Length);

                XgtAddressData XgtAddr = new XgtAddressData();
                XgtAddr.AddressString = vAddress;

                lstAddress.Add(XgtAddr);


                vLenth += XgtAddr.AddressByteArray.Length + XgtAddr.LengthByteArray.Length;
            }

            if (XGT_DataType.Continue == emDatatype && XGT_Request_Func.Read == emFunc)
            {
                vLenth += 2;  //연속읽기 인 경우 2바이트 추가.(데이터 갯수)
            }

            byte[] data = new byte[vLenth];


            int idx = 0;

            AddByte(command, ref idx, ref data);
            AddByte(dataType, ref idx, ref data);
            AddByte(reserved, ref idx, ref data);
            AddByte(blockcount, ref idx, ref data);

            foreach (XgtAddressData addr in lstAddress)
            {
                AddByte(addr.LengthByteArray, ref idx, ref data);
                AddByte(addr.AddressByteArray, ref idx, ref data);
            }

            /* 연속 읽기의 경우 읽을 갯수 지정. */
            if (XGT_DataType.Continue == emDatatype)
            {
                //데이터 타입이 연속 읽기 인 경우.
                byte[] vDataCount = BitConverter.GetBytes((short)pDataCount);
                AddByte(vDataCount, ref idx, ref data);
            }


            return(data);
        }
예제 #2
0
        /// <summary>
        /// 받아온 응답 값으로 데이터 분서 메서드.
        /// Read,Write 함수의 finally 구문에서 호출 하도록 작성하시오.
        /// </summary>
        public void MakeData()
        {
            try
            {
                NAK_ErrorCotent = string.Empty;

                List <XgtAddressData> lstData = new List <XgtAddressData>();

                //RX 응답 중 19번째가지는 헤더프레임 정보, 20번째부터 데이터 프레임.
                //받은 응답이 없으면, 즉 에러가 발생시
                if (RX.Length == 0)
                {
                    NAK_ErrorCotent = "서버로 부터 응답을 받지 못했습니다.";
                    return;
                }
                if (RX[20] == (short)XGT_Request_Func.ReadResponse)
                {
                    ResponseType = XGT_Request_Func.ReadResponse;
                }
                if (RX[20] == (short)XGT_Request_Func.WriteResponse)
                {
                    ResponseType = XGT_Request_Func.WriteResponse;
                }

                byte[] vdataType = new byte[2];
                vdataType[0] = RX[22];
                vdataType[1] = RX[23];


                foreach (XGT_DataType item in Enum.GetValues(typeof(XGT_DataType)))
                {
                    string vb = BitConverter.ToString(BitConverter.GetBytes((short)item));
                    string va = BitConverter.ToString(vdataType);
                    if (vb.Equals(va))
                    {
                        DataType = item;
                        break;
                    }
                }


                if (RX[26] != 0x00 || RX[27] != 0x00)
                {
                    //에러응답
                    ResponseStatus = "NAK";
                    DataList       = lstData;
                    //에러메세지 확인
                    switch (RX[28])
                    {
                    case 0x12:
                        NAK_ErrorCotent = "(0x12)연속읽기인데 바이트 타입이 아닌 경우";
                        break;

                    case 0x11:
                        NAK_ErrorCotent = "(0x11)변수명이 4보다 작거나 16보다 큰 경우와 같이 어드레스에 관련된 에러";
                        break;

                    case 0x10:
                        NAK_ErrorCotent = "(0x10)없는 디바이스를 요청하는 경우와 같이 디바이스에 관련된 에러";
                        break;

                    case 0x78:
                        NAK_ErrorCotent = "(0x78)unknown command";
                        break;

                    case 0x77:
                        NAK_ErrorCotent = "(0x77)체크섬 오류";
                        break;

                    case 0x76:
                        NAK_ErrorCotent = "(0x76)length 정보 오류";
                        break;

                    case 0x75:
                        NAK_ErrorCotent = "(0x75) “LGIS-GLOFA”가 아니거나 “LSIS-XGT”가 아닌 경우";
                        break;

                    case 0x24:
                        NAK_ErrorCotent = "(0x24)데이터 타입 에러";
                        break;

                    default:
                        NAK_ErrorCotent = "알려지지 않은 에러코드, LS산전 고객센터에 문의";
                        break;
                    }
                }
                else
                {
                    //28번 index 부터 데이터로 정의
                    int index = 28;

                    //정상응답
                    ResponseStatus = "ACK";
                    byte[] blockCount    = new byte[2]; //블럭카운터
                    byte[] dataByteCount = new byte[2]; //데이터 크기
                    byte[] data          = new byte[2]; //블럭카운터

                    Array.Copy(RX, index, blockCount, 0, 2);
                    BlockCount = BitConverter.ToInt16(blockCount, 0);

                    index = index + 2;

                    //블럭카운터 만큼의 데이터 갯수가 존재한다.

                    //Read일 경우 데이터 생성
                    if (ResponseType == XGT_Request_Func.ReadResponse)
                    {
                        for (int i = 0; i < BlockCount; i++)
                        {
                            Array.Copy(RX, index, dataByteCount, 0, 2);
                            int biteSize = BitConverter.ToInt16(dataByteCount, 0); //데이터 크기.

                            index = index + 2;

                            Array.Copy(RX, index, data, 0, biteSize);

                            index = index + biteSize;  //다음 인덱스

                            string dataContent = BitConverter.ToString(data, 0);

                            XgtAddressData dataValue = new XgtAddressData();
                            dataValue.Data          = dataContent;
                            dataValue.DataByteArray = data;

                            lstData.Add(dataValue);
                        }
                    }
                    DataList = lstData;
                }
            }
            catch (Exception ex)
            {
                Message = "Error: " + ex.Message.ToString();
            }
        }