Ejemplo n.º 1
0
        static byte[] SendHeaderAndReceive(PrinterControl printer, byte[] header)
        {
            //Console.WriteLine("SendingHeader");
            var fooRcvd   = new byte[4];
            int bytesRead = 0;

            printer.WriteSerialToFirmware(header, 4);
            do
            {
                bytesRead = printer.ReadSerialFromFirmware(fooRcvd, 4);
            } while (bytesRead < 1);
            return(fooRcvd);
        }
        private void handleResponse(Command cmd, byte[] byteMessage, List <Object> param)
        {
            var finalResponse  = "";
            var headerResponse = new byte[1];

            printer.ReadSerialFromFirmware(headerResponse, 1);
            if (headerResponse[0] == byteMessage[0])
            {
                printer.WriteSerialToFirmware(new byte[1] {
                    0xA5
                }, 1);
                byte[] finalMessage = new byte[byteMessage.Length - 3];
                Array.Copy(byteMessage, 4, finalMessage, 0, byteMessage.Length - 3);
                printer.WriteSerialToFirmware(finalMessage, byteMessage.Length - 3);
                var ch = new byte[2] {
                    0xFF, 0xFF
                };
                while (ch != new byte[2] {
                    0x00, 0x00
                })
                {
                    printer.ReadSerialFromFirmware(ch, 2);
                    finalResponse += System.Text.Encoding.UTF8.GetString(ch);
                }
                if (finalResponse != "SUCCESS")
                {
                    execute(cmd, param);
                }
            }
            else
            {
                printer.WriteSerialToFirmware(new byte[1] {
                    0xFF
                }, 1);
                execute(cmd, param);
            }
        }
        private bool read(byte[] buffer, int expectedBytes)
        {
            int timeout = 100;

            while (printer.ReadSerialFromFirmware(buffer, expectedBytes) != expectedBytes && timeout > 0)
            {
                System.Threading.Thread.Sleep(20);
                timeout--;
                if (timeout == 0)
                {
                    Console.WriteLine("Host: Read timeout");
                    System.Threading.Thread.Sleep(5);
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
        static bool WaitForResponse(PrinterControl printer)
        {
            //Console.WriteLine("HostWaitResponse");
            var responseRcvd  = false;
            var responseArray = new byte[successBytes.Length];
            var readResponse  = 0;

            while (!responseRcvd /* readResponse != successBytes.Length*/)
            {
                readResponse = printer.ReadSerialFromFirmware(responseArray, successBytes.Length);
                if (readResponse == successBytes.Length)
                {
                    //Console.WriteLine("Host done waiting: " + responseArray[0]);
                    responseRcvd = true;
                }
            }

            return(ByteArraysEquals(successBytes, responseArray));
        }
        public static byte[] ReadPacket(PrinterControl printer, int expected)
        {
            byte[] data    = new byte[expected];
            byte[] failure = new byte[4];
            int    count   = 0;

            while (count < 1000000000)
            {
                int response = printer.ReadSerialFromFirmware(data, expected);
                if (response == expected)
                {
                    //Console.WriteLine("\nHost is reading: ");
                    //for (int i = 0; i < data.Length; i++)
                    //{
                    //    Console.Write(" ");
                    //    Console.Write(data[i]);
                    //}
                    return(data);
                }
                count++;
            }
            return(failure);
        }
Ejemplo n.º 6
0
        /*
         * HostToFirmware
         *
         * Returns: true on success, false on failure
         * Arguments:
         *   packet: Packet to be sent
         *   simCtl: PrinterControl variable that gets passed around to everything
         *   (optional) maxRetries: Maximum number of retries before faliure is returned
         *   (internal) currentRetry: Used internally to track the current retry count
         */

        // MAKE SURE TO CHANGE MAXRETI
        static bool HostToFirmware(byte[] packet, PrinterControl simCtl, int maxRetries = 10, int currentRetry = 0) // MAKE COPIES OF ALL THE THINGS
        {
            if (currentRetry >= maxRetries)
            {
                return(false);
            }

            //Console.Write("+++++++++++++++NEXT COMMAND++++++++++++++++ \n");
            //        Host-to-Firmware Communication Procedure
            const int header_size   = 4;    // 4 is header size
            int       response_size = 1;    // So it reads one byte at a time
            int       ACK_NAK_size  = 1;    // size of both ACK and NAK bytes
            byte      null_byte     = 0x30; // Null byte
            const int max_size      = 20;

            byte[] ACK     = { 0xA5 }; // ACK byte
            byte[] NAK     = { 0xFF }; // ACK byte
            string success = "SUCCESS";

            //      Send 4-byte header consisting of command byte, length, and 16-bit checksum
            byte[] checksummed_packet = Checksum(packet);
            byte[] header             = checksummed_packet.Skip(0).Take(header_size).ToArray(); // array substring from Skip and Take, 0 to 4
            var    header_copy        = header.ToArray();                                       // making a copy for header to go in so it doesn't change it

            printByteArray(header_copy, "Host sending header");
            int header_bytes_sent = simCtl.WriteSerialToFirmware(header_copy, header_size);

            //      Read header bytes back from firmware to verify correct receipt of command header
            byte[] possible_header = new byte[header_size];
            // int header_bytes_recieved = simCtl.ReadSerialFromFirmware(possible_header, header_size);

            while (simCtl.ReadSerialFromFirmware(possible_header, header_size) < header_size) // function inside returns header_bytes_recieved
            {
                ;                                                                             // wait for four bytes to be recieved // 4 bytes
            }
            int test = 0;

            printByteArray(possible_header, "Host received header response");

            //      If header is correct
            if (header.SequenceEqual(possible_header))  // header == possible_header
            {
                //      Send ACK(0xA5) to firmware
                byte[] ACK_to_send = ACK.ToArray();
                printByteArray(ACK_to_send, "Host sending ack");
                int ACK_send = simCtl.WriteSerialToFirmware(ACK_to_send, ACK_NAK_size);    // 1 is the size of the ACK and NAK bytes

                //      Send rest of packet not including the 4-byte header
                byte[] rest_bytes_send = checksummed_packet.Skip(header_size).Take(checksummed_packet.Length - header_size).ToArray(); // array substring
                printByteArray(rest_bytes_send, "Host sending remaining bytes");
                int rest_bytes_sent = simCtl.WriteSerialToFirmware(rest_bytes_send, packet.Length - header_size);                      // change last argument to parameter data length in the 4th byte

                //      Wait for first byte of response to be received
                byte[] response_byte  = new byte[response_size];
                byte[] response_bytes = new byte[max_size];
                int    num_received   = 0;
                int    i = 0;
                //int response_bytes_recieved = simCtl.ReadSerialFromFirmware(response_bytes, response_size);
                //      Continue reading rest of response until null byte (0) is received
                while (true)    // might get hung up here
                {
                    num_received = simCtl.ReadSerialFromFirmware(response_byte, 1);
                    if (num_received == 1)
                    {
                        i++;                                                         // ++'s the number of bytes received
                        response_bytes[i] = response_byte[response_byte.Length - 1]; // fills the byte[] with the bytes starting at 1 for some reason
                        num_received      = 0;                                       // resets num_received
                    }

                    if (response_bytes[i] == null_byte) // should I use .SequenceEqual()?
                    {
                        break;                          // exit the wait loop
                    }
                    else if (i >= max_size)
                    {
                        Console.Write("Broke when trying to read response \n");
                        return(false);
                    }
                }
                var    new_response    = response_bytes.Skip(1).Take(i - 1).ToArray();       // i - 1 to take off the null and skip 1 to get rid of the 0 in first
                string response_string = System.Text.Encoding.ASCII.GetString(new_response); // converts from byte[] to string
                printByteArray(response_bytes, "Host received response string " + response_string);



                //      Verify that response string equals “SUCCESS” or “VERSION n.n” (If not, re-send entire command)
                if (response_string == success)    //  || response_bytes == "VERSION n.n"
                {
                    return(true);
                }
                else
                {
                    Console.Write("retry NO SUCCESS  \n");
                    //      retry command
                    return(HostToFirmware(packet, simCtl, maxRetries, currentRetry + 1));  // this retries the command and returns the result of that command
                }
            }
            //      else if header is not received correctly
            else
            {
                //      Send NAK(0xFF)
                byte[] NAK_to_send = NAK.ToArray();
                printByteArray(NAK_to_send, "Host sending nak :(");
                int NAK_send = simCtl.WriteSerialToFirmware(NAK_to_send, ACK_NAK_size);
                Console.Write("retry NAK \n");
                //      Retry command
                return(HostToFirmware(packet, simCtl, maxRetries, currentRetry + 1));  // this retries the command and returns the result of that command
            }
        }
Ejemplo n.º 7
0
        public string SendCommand()
        {
            byte[] value = new byte[HEADER_SIZE];
            int    bytesRetrieved;
            string retString = "";

            // Calculate checksum
            short checkSum = 0;

            for (int i = 0; i < curCommand.Count(); i++)
            {
                checkSum += curCommand[i];
            }

            curCommand[2] = (byte)checkSum;    // Stuff checksum into buffer at reserved spot
            curCommand[3] = (byte)(checkSum >> 8);

            do
            {
                // Write the header
                printer.WriteSerialToFirmware(curCommand.ToArray(), HEADER_SIZE);
                // Get header back
                while ((bytesRetrieved = printer.ReadSerialFromFirmware(value, HEADER_SIZE)) == 0)
                {
                    ;
                }

                if (value[0] == curCommand[0] && value[1] == curCommand[1] && value[2] == curCommand[2] && value[3] == curCommand[3])
                {
                    // Send ACK
                    value[0] = 0xA5;
                    printer.WriteSerialToFirmware(value, 1);

                    // Send command payload
                    while (printer.WriteSerialToFirmware(curCommand.Skip(HEADER_SIZE).ToArray(), curCommand.Count() - HEADER_SIZE) == 0)
                    {
                        ;
                    }

                    retString = "";
                    // Wait for response byte
                    do
                    {
                        bytesRetrieved = printer.ReadSerialFromFirmware(value, 1);
                    } while (bytesRetrieved == 0);

                    retString += (char)value[0];  // Add returned value to return string

                    // Add rest of response string until null byte read
                    while (value[0] != 0)
                    {
                        bytesRetrieved = printer.ReadSerialFromFirmware(value, 1);
                        if (bytesRetrieved != 0 && value[0] != 0)
                        {
                            retString += (char)value[0];
                        }
                    }

                    if (curCommand[0] == FWCommands.CMD_VERSION)
                    {
                        break;                                          // Return version info
                    }
                    //                    if (retString != "SUCCESS") Console.WriteLine("Error {0}: " + retString, cmdNum);
                }
                else  // error - send NACK
                {
                    // Send NACK
                    value[0] = 0xFF;
                    printer.WriteSerialToFirmware(value, 1);
                    retString = "NAK";
                }
            } while (retString != "SUCCESS");

            Clear();
            cmdNum++;

            return(retString);
        }