Ejemplo n.º 1
0
        //어플리케이션 데이터 WRITE 포맷 만들기
        private byte[] CreateWriteDataFormat
            (XGT_Request_Func emFunc, XGT_DataType emDatatype, List <XgtAddressData> pAddressList, XGT_MemoryType emMemtype, int pDataCount)
        {
            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;

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

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

                addr.AddressString = vAddress;


                object oData       = new object(); //입력받은 값이 숫자형인지 문자형이지 확실치 않아 Object 로 선언
                int    oDataLength = 0;            //입력받은 값의 바이트 배열의 크기.

                //데이터 쓰기일 경우 입력 데이터의 크기를 구한다.
                int    nInput   = 0;            //입력받은 데이터가 숫자형일경우 받을 변수
                string strInput = string.Empty; //입력받은 데이터가 문자형일 경우 받을 변수.

                if (!int.TryParse(addr.Data, out nInput))
                {
                    //문자형일 경우
                    strInput = addr.Data;
                    oData    = Encoding.ASCII.GetBytes(strInput);
                }
                else
                {
                    //숫자형일 경우
                    oData = BitConverter.GetBytes((short)nInput);
                }

                if (emDatatype == XGT_DataType.Bit)
                {
                    addr.DataByteArray    = new byte[1];
                    addr.DataByteArray[0] = ((byte[])oData)[0];
                }
                else
                {
                    addr.DataByteArray = (byte[])oData;
                }

                //입력값의 바이트 배열의 크기
                oDataLength = ((byte[])oData).Length;

                vLenth += addr.AddressByteArray.Length + addr.LengthByteArray.Length + 2 + oDataLength; //데이터 갯수 + 데이터 길이

                lstAddress.Add(addr);
            }

            if (XGT_DataType.Continue == emDatatype)
            {
                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);
            }



            foreach (XgtAddressData addr in lstAddress)
            {
                //데이터 쓰기일 경우
                byte[] count = BitConverter.GetBytes((short)addr.DataByteArray.Length);

                AddByte(count, ref idx, ref data);
                AddByte(addr.DataByteArray, ref idx, ref data);
            }


            return(data);
        }
Ejemplo n.º 2
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);
        }