Exemple #1
0
        private void Data_Proc(byte[] data)
        {
            try
            {
                Console.WriteLine("[Data수신]" + Fnc.ByteArray2HexString(data, " "));
                //헤더 체크
                if (!Fnc.bytesEqual(data, 0, _bytHeader, 0, _bytHeader.Length))
                {
                    Log_Write("헤더 불일치", enCmdType.None, true);
                    return;
                }

                string log = string.Empty;

                enCmdType cmd = enCmdType.None;

                //20~21 명령 타입
                switch (data[20])
                {
                case 0x54:           //read command
                    cmd = enCmdType.Read;
                    break;

                case 0x58:           //write command
                    cmd = enCmdType.Write;
                    break;

                case 0xBD:
                    byte[] bytData = new byte[] { 0x01, 0xff };
                    Server.Send(bytData);
                    break;

                default:
                    break;
                }

                if (cmd == enCmdType.None)
                {
                    return;
                }

                //22-23데이터타입
                enDataType dType = enDataType.Block;

                if (data[22] != 0x14)
                {
                    dType = enDataType.Unit;
                }


                //24-25 예약

                //26-27 변수 개수
                int cnt = fnc.ByteToInt(data, 26, 2);

                int     idx  = 28;
                int     ridx = 30;
                DataRow dr;
                string  add = string.Empty;
                byte[]  send;
                int     value;
                int     vLen;

                //log = string.Format("[{0}]", cmd);

                if (dType == enDataType.Block)
                {
                    vLen = fnc.ByteToInt(data, idx, 2);
                    cnt  = fnc.ByteToInt(data, idx + 2 + vLen, 2);

                    cnt = Multiple > 1 ? cnt / Multiple : cnt;
                }

                if (cmd == enCmdType.Read)
                {
                    if (dType == enDataType.Unit)
                    {
                        send = new byte[32 + cnt * 4];
                    }
                    else
                    {
                        send = new byte[32 + cnt * 2];
                    }
                }
                else
                {
                    send = new byte[30];
                }


                for (int i = 0; i < cnt; i++)
                {
                    //처음이거나 처리단위가 유닛이면
                    if (i == 0 || dType == enDataType.Unit)
                    {
                        //28-29 변수명 길이
                        vLen = fnc.ByteToInt(data, idx, 2);
                        idx += 2;

                        //30~   변수명
                        add  = fnc.BytesToAscii(data, idx, vLen);
                        add  = fnc.Address_SetAddType(add, Multiple);
                        idx += vLen;
                    }
                    else
                    {   //블록단위(연속처리)
                        add = fnc.Address_NetGet(add);
                    }

                    if (_dtAdd.Select(string.Format("Address = '{0}'", add)).Length > 0)
                    {
                        dr = _dtAdd.Select(string.Format("Address = '{0}'", add))[0];
                    }
                    else
                    {
                        dr            = _dtAdd.NewRow();
                        dr["Address"] = add;
                        dr["Value"]   = 0;
                        _dtAdd.Rows.Add(dr);
                    }


                    if (cmd == enCmdType.Read)
                    {
                        if (dType == enDataType.Unit)
                        {
                            //data size
                            fnc.ByteSetIntValue(send, ridx, 2, 2);
                            ridx += 2;
                        }

                        //datavalue
                        fnc.ByteSetIntValue(send, ridx, 2, Fnc.obj2int(dr["Value"]));
                        ridx += 2;

                        //if (i == 0 && dType == enDataType.Block & cnt > 1)
                        //    log += string.Format("[{0}", add);
                        //else if (i == 0 || dType == enDataType.Unit)
                        //    log += string.Format("[{0}] ", add);

                        log += string.Format("[{0}]{1} ", add, dr["Value"]);
                    }
                    else if (cmd == enCmdType.Write)
                    {
                        //값 길이
                        vLen = fnc.ByteToInt(data, idx, 2);
                        idx += 2;

                        //값
                        value = fnc.ByteToInt(data, idx, vLen);
                        value = Multiple > 1 ? value / Multiple : value;

                        log += string.Format("[{0}]{1}=>{2} ", add, dr["Value"], value);

                        dr["Value"] = value;
                        idx        += 2;
                    }
                }

                //if (cmd == enCmdType.Read && dType == enDataType.Block && cnt > 1)
                //{
                //    log += string.Format("~{0}] ", add);
                //}


                _bytHeader.CopyTo(send, 0);

                //plc info 10~11
                send[11] = 1;

                //cpu info [A0]XGK [A4]XGI [A8]XGR
                send[12] = data[12];

                //source of frame
                send[13] = 0x11;

                //invoke id
                send[14] = data[14];
                send[15] = data[15];

                //length
                fnc.ByteSetIntValue(send, 16, 2, send.Length);

                //response type
                if (cmd == enCmdType.Read)
                {
                    send[20] = 0x55;
                    //data type
                    send[22] = data[22];

                    //data cnt
                    fnc.ByteSetIntValue(send, 28, 2, cnt);

                    //data size
                    //fnc.ByteSetIntValue(send, 30, 2, 2);
                }
                else if (cmd == enCmdType.Write)
                {
                    send[20] = (byte)(dType == enDataType.Block ? 0x59 : 0x58);
                    send[20] = 0x59;
                    //data type
                    send[22] = data[22];

                    //data cnt
                    fnc.ByteSetIntValue(send, 28, 2, cnt);
                }



                Log_Write(log, cmd, false);

                Server.Send(send);
            }
            catch (Exception ex)
            {
                ProcException(ex, "Data_Proc");
            }
        }