public void Prime()
        {
            if (rfidOn == true)
            {
                Debug.WriteLine("Prime Not Available During RFID Scanning");
            }
            else
            {
                primeOn = !primeOn;
                if (primeOn)
                {
                    setDeviceLight(LED_PURPLE_BLINK);
                }
                else
                {
                    setDeviceLight(LED_PURPLE_STOP);
                }

                TcpPacket tcp = new TcpPacket(IP, 0x04, 0x0E, 0, null, 100);
                connection.Send(tcp.getBuffer());
            }
        }
        private void processPacket(byte[] buffer, int len)
        {
            TcpPacket pck   = new TcpPacket(buffer, len);
            int       pckId = 0;

            pckId = pck.getId();
            switch (pckId)
            {
            case 0x0104:
            {
                Debug.WriteLine("GET password 0104", convertBytesToHex(pck.getData()));
                password = convertBytesToHex(pck.getData());
                break;
            }

            case 0x0102:    // get ssid
            {
                Debug.WriteLine("GET SSID 0102", convertBytesToHex(pck.getData()));
            }
            break;

            case 0x040D:     // adapter settings
            {
                Debug.WriteLine("ADAPTER SETT 040D", convertBytesToHex(pck.getData()));
                setDeviceLight(LED_YELLOW_ON);
            }
            break;

            case 0x0209:     // system info
            {
                byte[] res = pck.getData();
                Debug.WriteLine("SYS INFO 0209 (" + convertBytesToHex(res).Length.ToString() + ")", convertBytesToHex(res));
                fwVer = res[1] | res[0] << 8;
                if (convertBytesToHex(res).Length > 12)
                {
                    String rawMac = convertBytesToHex(res);
                    MAC = rawMac[0] + rawMac[1] + ":" + rawMac[2] + rawMac[3] + ":" + rawMac[4] + rawMac[5] + ":" + rawMac[6] + rawMac[7] + ":" + rawMac[8] + rawMac[9] + ":" + rawMac[10] + rawMac[11];
                }
                else
                {
                    int majorVersion = 1;
                    int minorVersion = 16;

                    /*MemoryStream version_stream = new MemoryStream();
                     * version_stream.Write(BitConverter.GetBytes(majorVersion), 0, 1);
                     * version_stream.Write(BitConverter.GetBytes(minorVersion), 0, 1);
                     * TcpPacket tcp = new TcpPacket(IP, 0x02, 0x09, version_stream.ToArray().Length, version_stream.ToArray(), readDelay);
                     * Stream stm = connection.GetStream();
                     * stm.Write(tcp.getBuffer(), 0, tcp.getBuffer().Length);*/
                }
            }
            break;

            case 0x020A:
            {
                byte[] res = pck.getData();
                Debug.WriteLine("MEM INFO 020A", convertBytesToHex(res));
                totalMemSize = convertByteArrayToInt(res, 0);
                usedMemSize  = convertByteArrayToInt(res, 4);
            }
            break;

            case 0x040C:
            {
                Debug.WriteLine("RECEIVE TREATMENT 040C", convertBytesToHex(pck.getData()));
            }
            break;

            case 0x0410:
            {
                Debug.WriteLine("MEDIC CONFIG REQ 0410", convertBytesToHex(pck.getData()));
            }
            break;

            default:
                break;
            }
        }
        private void SetupDevice()
        {
            Debug.WriteLine("Local(" + IP + "): " + Convert.ToString(state));
            switch (state)
            {
            case DevStates.STATE_SET_ID:
            {
                try
                {
                    MemoryStream id_stream = new MemoryStream();
                    id_stream.Write(BitConverter.GetBytes(ID), 0, 1);
                    TcpPacket tcp = new TcpPacket(IP, 0x02, 0x0B, id_stream.ToArray().Length, id_stream.ToArray(), readDelay);
                    connection.Send(tcp.getBuffer());
                }
                catch (Exception e)
                {
                    Debug.WriteLine("ID conf failed" + e.GetBaseException());
                    state = DevStates.STATE_DONE;
                }
            }
            break;

            case DevStates.STATE_SET_TIME:
            {
                try
                {
                    MemoryStream time_stream = new MemoryStream();
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Hour), 0, 1);
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Minute), 0, 1);
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Second), 0, 1);
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Day), 0, 1);
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Month), 0, 1);
                    time_stream.Write(BitConverter.GetBytes(DateTime.Now.Year % 100), 0, 1);
                    TcpPacket tcp = new TcpPacket(IP, 0x02, 0x08, time_stream.ToArray().Length, time_stream.ToArray(), readDelay);
                    connection.Send(tcp.getBuffer());
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Time conf failed: " + e.GetBaseException());
                }
            }
            break;

            case DevStates.STATE_GET_SYS_INFO:
            {
                try
                {
                    int          majorVersion   = 1;
                    int          minorVersion   = 16;
                    MemoryStream version_stream = new MemoryStream();
                    version_stream.Write(BitConverter.GetBytes(majorVersion), 0, 1);
                    version_stream.Write(BitConverter.GetBytes(minorVersion), 0, 1);
                    TcpPacket tcp = new TcpPacket(IP, 0x02, 0x09, version_stream.ToArray().Length, version_stream.ToArray(), readDelay);
                    connection.Send(tcp.getBuffer());

                    byte[] request = new byte[255];
                    connection.Receive(request);
                    Debug.WriteLine("--------New Msg-------------");
                    Debug.WriteLine(BitConverter.ToString(request));
                    processPacket(request, request.Length);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Sys conf failed: " + e.GetBaseException());
                }
            }

            break;

            case DevStates.STATE_GET_MEM_INFO:
            {
                try
                {
                    TcpPacket tcp = new TcpPacket(IP, 0x02, 0x0A, 0, null, readDelay);
                    connection.Send(tcp.getBuffer());

                    byte[] request = new byte[255];
                    connection.Receive(request);
                    Debug.WriteLine("--------New Msg-------------");
                    Debug.WriteLine(BitConverter.ToString(request));
                    processPacket(request, request.Length);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Sys conf failed: " + e.GetBaseException());
                }
            }
            break;

            case DevStates.STATE_GET_ADAP_SETT:
            {
                try
                {
                    TcpPacket tcp = new TcpPacket(IP, 0x04, 0x0D, 0, null, readDelay);
                    connection.Send(tcp.getBuffer());

                    byte[] request = new byte[255];
                    connection.Receive(request);
                    Debug.WriteLine("--------New Msg-------------");
                    Debug.WriteLine(BitConverter.ToString(request));
                    processPacket(request, request.Length);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Adap get failed: " + e.GetBaseException());
                }
            }
            break;

            case DevStates.STATE_SET_FARM_INFO:
                break;

            case DevStates.STATE_DONE:
            {
                Debug.WriteLine("Setup Device Finished");
                readMsg();
            }
            break;
            }
            if (state < DevStates.STATE_DONE)
            {
                Task.Delay(500).Wait();
                state++;
                SetupDevice();
            }
            else
            {
                state = DevStates.STATE_SET_ID;
            }
        }