Example #1
0
        private bool initPort(UartSpeed speed)
        {
            try
            {
                // Let's initialize the port with highest possible bandwidth
                MifareResponse request = sendInitRequest(speed);
                if (request.Response != Response.OK)
                {
                    return(false);
                }

                // Let's find out what hardware we're dealing with
                MifareDeviceResponse deviceModeResponse = sendReadDeviceMode();
                if (deviceModeResponse.Response != Response.OK)
                {
                    return(false);
                }

                Console.WriteLine("Communicating with device name {0}", deviceModeResponse.DeviceName);

                // Turn LED off so we can actively use it to give visual feedback
                //Console.WriteLine (sendLedRequest (LEDColor.ALL_LED_OFF));
                //Console.WriteLine ();

                sendLedRequest(LEDColor.BLUE_ON_RED_OFF);
                Thread.Sleep(500);
                sendLedRequest(LEDColor.ALL_LED_OFF);

                return(true);
            }
            catch (System.TimeoutException exception)
            {
                return(false);
            }
        }
Example #2
0
        private MifareResponse sendInitRequest(UartSpeed speed)
        {
            byte[] readBuffer  = new byte[64];
            int    offset      = 0;
            int    bytesToRead = 0;

            int retryCount = 0;

            do
            {
                if (retryCount++ == INIT_REQUEST_RETRY_COUNT)
                {
                    throw new Exception("Failed at establishting connection!");
                }

                WriteCommand(Command.INIT_PORT, NODE_BROADCAST, (byte)speed);

                Thread.Sleep(50);

                bytesToRead = port.BytesToRead;
            } while (bytesToRead == 0);

            Console.WriteLine("Connection established after " + retryCount + " attempts!");

            // TODO: Use callback, make response parser able to read lazily from input stream
            int bytesRead = port.Read(readBuffer, offset, bytesToRead);


            // Evaluate data in buffer


            //Console.WriteLine ();
            //Console.WriteLine ("Received: {0} ( {1} bytes)", readBuffer.Subset (0, offset).ToHex (), offset);

            MifareResponse response = new MifareResponse();

            //ushort magic = readBuffer.Subset (0, 2).ToUInt16 ();
            //Console.WriteLine ("Magic: {0} ( {1})", magic, readBuffer.Subset (0, 2).ToHex ());

            ushort length = readBuffer.Subset(2, 2).ToUInt16();
            //Console.WriteLine ("Length: {0} ( {1})", length, readBuffer.Subset (2, 2).ToHex ());

            ushort nodeID = readBuffer.Subset(4, 2).ToUInt16();

            //Console.WriteLine ("NodeID: {0} ( {1})", nodeID, readBuffer.Subset (4, 2).ToHex ());
            response.NodeId = nodeID;

            ushort commandCode = readBuffer.Subset(6, 2).ToUInt16();

            //Console.WriteLine ("Command: {0} ( {1})", Enum.GetName (typeof(CommandCode), commandCode), readBuffer.Subset (6, 2).ToHex ());
            response.Command = (Command)commandCode;

            byte responseCode = readBuffer[8];

            //Console.WriteLine ("Response Code: {0} ( {1})", Enum.GetName (typeof(ResponseCode), responseCode), responseCode.ToHex ());
            response.Response = (Response)responseCode;

            //byte checksum = readBuffer [readBuffer.Length-1];
            byte checksum = readBuffer[4 + length];
            //Console.WriteLine ("XOR: {0} ( {1})", checksum, checksum.ToHex ());

            byte calculatedChecksum = CalcCheckSum(readBuffer, 4, 3 + length);

            //Console.WriteLine ("XOR match: {0} ( {1})", calculatedChecksum, calculatedChecksum.ToHex ());

            validateChecksum(checksum, calculatedChecksum);

            return(response);
        }