Example #1
0
 public bool read(byte[] buffer, int expectedBytes)
 {
     try
     {
         if (expectedBytes == 0)
         {
             return(true);
         }
         int timeout    = 10000;
         int readResult = 0;
         while (timeout > 0 && readResult == 0)
         {
             readResult = printer.ReadSerialFromHost(buffer, expectedBytes);
             if (readResult != expectedBytes)
             {
                 timeout--;
                 Thread.Sleep(10);
             }
         }
         if (timeout == 0)
         {
             var message = Encoding.UTF8.GetBytes("TIMEOUT" + char.MinValue);
             printer.WriteSerialToHost(message, message.Length);
             return(false);
         }
         return(true);
     }
     catch (Exception e)
     {
         System.Console.WriteLine(e.StackTrace);
         return(false);
     }
 }
Example #2
0
        // Handle incoming commands from the serial link
        void Process()
        {
            // Todo - receive incoming commands from the serial link and act on those commands by calling the low-level hardwarwe APIs, etc.
            while (!fDone)
            {
                byte[] header = ReadPacket(printer, 4);
                byte   length = header[1];
                byte[] response;

                if (header.Any(b => b != 0))
                {
                    //Console.WriteLine("\nFirmware sending to host: ");
                    //for (int i = 0; i < header.Length; i++)
                    //{
                    //    Console.Write(" ");
                    //    Console.Write(header[i]);
                    //}
                    printer.WriteSerialToHost(header, 4);

                    byte[] ack = ReadPacket(printer, 1);
                    if (ack[0] == 0xA5)
                    {
                        byte[] data = ReadPacket(printer, length);
                        if (data.Length == 0)
                        {
                            response = Encoding.ASCII.GetBytes("Timeout");
                        }
                        else
                        {
                            ushort checksum = FindChecksum(header[0], data, data.Length);
                            //Console.WriteLine("Host Checksum: " + header[2] + header[3] + "\n");

                            //Console.WriteLine("Firmware checksum: " + BitConverter.GetBytes(checksum)[0] + BitConverter.GetBytes(checksum)[1]);

                            if ((header[2] == BitConverter.GetBytes(checksum)[0]) && (header[3] == BitConverter.GetBytes(checksum)[1]))
                            {
                                response = ProcessCmd(header[0], data);
                            }
                            else
                            {
                                response = Encoding.ASCII.GetBytes("CHECKSUM");
                            }
                        }
                        //Console.WriteLine("\nFirmware sending response to host: ");
                        byte[] nullByte = { (byte)0 };
                        response = response.Concat(nullByte).ToArray();

                        //for (int i = 0; i < response.Length; i++)
                        //{
                        //    Console.Write(" ");
                        //    Console.Write(response[i]);
                        //}
                        printer.WriteSerialToHost(response, response.Length);
                    }
                }
            }
        }
Example #3
0
        void ReceiveHeaderAndSend(byte[] headerReceived)
        {
            int bytesRead = 0;

            while (bytesRead < 1)
            {
                bytesRead = printer.ReadSerialFromHost(headerReceived, 4);
            }
            printer.WriteSerialToHost(headerReceived, 4);
        }
        void Process()
        {
            byte[] value     = new byte[HEADER_SIZE];
            byte[] cmdParams = new byte[64];
            int    bytesRead = 0;
            string retString = "";
            int    timeout;

            while (!fDone)
            {
                do                                                              // Wait for command byte
                {
                    bytesRead = printer.ReadSerialFromHost(value, HEADER_SIZE); // Read command, length, and checksum
                } while (!fDone && bytesRead == 0);

//                Console.WriteLine("Process: {0} {1}", value[0], value[1]);

                if (!fDone && bytesRead == HEADER_SIZE)   // Successfully read command byte, length byte, and checksum bytes
                {
                    // Write header back to host
                    printer.WriteSerialToHost(value, HEADER_SIZE);

                    byte[] ACKNAK = new byte[2];
                    ACKNAK[0] = 0;
                    do                                                     // Wait for ACK/NACK byte
                    {
                        bytesRead = printer.ReadSerialFromHost(ACKNAK, 1); // Read ACK/NAK
                    } while (!fDone && bytesRead == 0);

                    if (ACKNAK[0] == ACK_BYTE)
                    {
                        timeout = 0;
                        if (value[1] > 0)
                        {
                            do
                            {
                                timeout++;
                            } while ((bytesRead = printer.ReadSerialFromHost(cmdParams, value[1])) == 0 && timeout < TIMEOUT_THRESHOLD);                    // Read in the parameters
                        }
                        if (timeout == TIMEOUT_THRESHOLD)
                        {
                            retString = "TIMEOUT";
                        }
                        else
                        {
                            // Validate checksum
                            short checkSum       = (byte)(value[0] + value[1]);
                            short targetCheckSum = (short)(((short)value[3] << 8) + value[2]);

                            for (int i = 0; i < value[1]; i++)
                            {
                                checkSum += cmdParams[i];
                            }

                            if (checkSum == targetCheckSum)  // Valid checksum, proceed
                            {
                                if (value[0] != FWCommands.CMD_BUFFER)
                                {
                                    retString = DoCommand(value[0], cmdParams, 0);
                                }
                                else  // Process command buffer
                                {
                                    while (cmdParams[0] != FWCommands.CMD_INVALID)
                                    {
                                        byte Ndx       = 0;
                                        byte bufferLen = value[1];
                                        byte cmd       = cmdParams[Ndx++];
                                        byte len       = cmdParams[Ndx++];
                                        Ndx += len;

                                        retString = DoCommand(cmd, cmdParams, 2);

                                        // Delete used command
                                        for (byte i = 0; i < bufferLen - (len + 2); i++)
                                        {
                                            cmdParams[i] = cmdParams[len + 2 + i];
                                        }

                                        for (byte i = (byte)(bufferLen - (len + 2)); i < bufferLen; i++)
                                        {
                                            cmdParams[i] = FWCommands.CMD_INVALID;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                retString = "CHECKSUM";
                            }
                        } // If no TIMEOUT

                        printer.WriteSerialToHost(retString.Select(c => (byte)c).ToArray(), retString.Length);
                        value[0] = 0;
                        printer.WriteSerialToHost(value, 1);
                    }  // if ACK'd
                }
            }
        }
Example #5
0
        string FirmwareToHost(PrinterControl printer) // took out byte[] packet, // need to change the return type to the same as the host side
        {
            //Firmware-to-Host Communication Procedure
            int header_size   = 4;      // 4 is header size
            int response_size = 1;      // UNSURE OF SIZE OF RESPONSE
            int ACK_NAK_size  = 1;      // size of both ACK and NAK bytes

            byte[] ACK      = { 0xA5 }; // ACK byte
            byte[] NAK      = { 0xFF }; // ACK byte
            string timeout  = "TIMEOUT";
            string success  = "SUCCESS";
            string checksum = "CHECKSUM";
            string nak      = "NAK";

            //      Read 4-byte header from host
            byte[] header_recieved = new byte[header_size];

            // wait for bytes to be recieved
            while (printer.ReadSerialFromHost(header_recieved, header_size) < header_size) // the function returns header_bytes_recieved
            {
                ;                                                                          // wait
            }
            ClearBuffer(printer);                                                          // This should clear the buffer
            printByteArray(header_recieved, "Firmware received header");

            //      Write 4-byte header back to host
            byte[] header_sent = header_recieved.ToArray();
            printByteArray(header_sent, "Firmware sent header");
            int header_bytes_sent = printer.WriteSerialToHost(header_sent, header_size);

            //      Read ACK/NAK byte
            byte[] ACK_NAK = { 0x00 };                                    // change this to a one element array with nothing in it
            while (printer.ReadSerialFromHost(ACK_NAK, ACK_NAK_size) < 1) // function inside returns ACK_NAK_recieved (int)
            {
                ;                                                         // wait
            }
            //ClearBuffer(printer);  // This should clear the buffer
            printByteArray(ACK_NAK, "Firmware received ACK or NAK");
            //      If ACK received
            if (ACK_NAK.SequenceEqual(ACK))
            {
                //      Attempt to read number of parameter bytes indicated in command header
                int    num_bytes_rest      = (int)(header_recieved[1]); // header_recieved[1] is the number of bytes in the rest of the packet
                byte[] rest_bytes          = new byte[num_bytes_rest];
                int    rest_bytes_recieved = 0;
                // header_recieved[1] should be rest of packet size
                int  test_count   = 0;
                bool timeout_test = false;
                while ((rest_bytes_recieved = printer.ReadSerialFromHost(rest_bytes, num_bytes_rest)) < num_bytes_rest)    // function inside returns rest_bytes_recieved
                {
                    // wait
                    if (rest_bytes_recieved < num_bytes_rest && test_count >= 100)
                    {
                        timeout_test = true;
                        break;
                    }
                }
                ClearBuffer(printer);  // This should clear the buffer
                printByteArray(rest_bytes, "Firmware received data");
                //      If insufficient bytes are received
                if (timeout_test)   // if number of bytes recieved is less than number of bytes sent
                {
                    //      return “TIMEOUT”
                    byte[] response_bytes = ResponseMaker(timeout);
                    printByteArray(response_bytes, "Firmware sent response");
                    int response_bytes_sent = printer.WriteSerialToHost(response_bytes, response_size);
                    return(timeout);
                }
                else
                {
                    //      Validate checksum(Be sure NOT to include checksum values themselves)
                    byte[] combined = new byte[header_recieved.Length + rest_bytes.Length]; // change header_revcieved.,length to header_size
                    System.Buffer.BlockCopy(header_recieved, 0, combined, 0, header_recieved.Length);
                    System.Buffer.BlockCopy(rest_bytes, 0, combined, 4, rest_bytes.Length);

                    byte[] combined_checksum       = Checksum(combined); // compare bytes 2 and 3 from both the header and what is recieved by adding up both parts: header and rest
                    var    combined_checksum_bytes = combined_checksum.Skip(2).Take(2).ToArray();
                    var    header_checksum_bytes   = header_recieved.Skip(2).Take(2).ToArray();

                    //      If checksum correct
                    if (combined_checksum_bytes.SequenceEqual(header_checksum_bytes))    // .SequenceEqual checks the actual value
                    {
                        // Combined should be the packet without the checksum. make sure
                        //      Process      // TYLER's Section
                        ProcessCommand(combined, printer);     // change from packet to actual packet that goes into the function // how can this work without returning????

                        //      Return “SUCCESS” or “VERSION n.n”
                        byte[] response_bytes = ResponseMaker(success);
                        printByteArray(response_bytes, "Firmware sent Success or Version response");
                        int response_bytes_sent = printer.WriteSerialToHost(response_bytes, response_bytes.Length);
                        //ClearBuffer(printer);  // This should clear the buffer
                        return(success); // SUCCESS
                    }
                    //      Else
                    else
                    {
                        //      Return “CHECKSUM”
                        byte[] response_bytes = ResponseMaker(checksum);
                        printByteArray(response_bytes, "Firmware sent Checksum response");
                        int response_bytes_sent = printer.WriteSerialToHost(response_bytes, response_bytes.Length);
                        return(checksum);    // CHECKSUM
                    }
                }
            }
            //      Else if NAK received
            else if (ACK_NAK.SequenceEqual(NAK))
            {
                //ClearBuffer(printer);
                //      Ignore command – it will be resent
                return(nak);
            }
            else
            {   // BANDAID
                int    max_size     = 20;
                byte[] nak_byte     = new byte[response_size];
                byte[] nak_bytes    = new byte[max_size];
                int    num_received = 0;
                int    i            = 0;
                while (true)    // might get hung up here
                {
                    num_received = printer.ReadSerialFromHost(nak_byte, 1);
                    if (num_received == 1)
                    {
                        i++;                                          // ++'s the number of bytes received
                        nak_bytes[i] = nak_byte[nak_byte.Length - 1]; // fills the byte[] with the bytes starting at 1 for some reason
                        num_received = 0;                             // resets num_received
                    }
                    // NAK[0] because one byte long
                    if (nak_bytes[i] == NAK[0]) // should I use .SequenceEqual()?
                    {
                        break;                  // exit the wait loop
                    }
                }
                ClearBuffer(printer);           // This should clear the buffer
                return("Found the bloody NAK"); // should never get to this but it does
            }
        }