// Changes done By JK on 9th Apr, 2013 to enable this class to control multiple Vend Machines.
        private void Init(bool reconnect)
        {
            short counter = 0;
            if (simulateDevice)
            {
                isEnabled = true;
                this.stateCode = (int)DispenserCommState.ONLINE;
                this.statusCode = (int)DispenserStatus.NoError;
                if (simulationTimer == null)
                    simulationTimer = new Timer(new TimerCallback(SimulatePackVending), null, Timeout.Infinite, Timeout.Infinite);
            }
            else
            {
                multiVend = Convert.ToInt16(string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["MULTIVENDMACHINE"]) ? "1" : ConfigurationManager.AppSettings["MULTIVENDMACHINE"]);
                waitTime = Convert.ToInt16(string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["OPERATIONTIMEOUT"]) ? "1500" : ConfigurationManager.AppSettings["OPERATIONTIMEOUT"]);
                using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter writer = new StreamWriter(stream);
                    writer.WriteLine(string.Format(@"Datetime {0} -- Number of PackDispensers to be Initialized - {1}.", DateTime.Now, multiVend));
                    writer.Flush();
                    writer.Close();
                }
                for (counter = 0; counter < multiVend; counter++)
                {
                    // Instaniate new Pack Vendor object
                    PackVendor pv = new PackVendor();

                    pv.com = string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["PACKDISPENSERCOMPORT" + counter.ToString()]) ? counter.ToString() : ConfigurationManager.AppSettings["PACKDISPENSERCOMPORT" + counter.ToString()];
                    using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                    {
                        StreamWriter writer = new StreamWriter(stream);
                        writer.WriteLine(string.Format(@"Datetime {0} -- PackDispenser Init on COM{1}.", DateTime.Now, pv.com));
                        writer.Flush();
                        writer.Close();
                    }
                    // Set comm port
                    pv.packDispenser.CommPort = Convert.ToInt16(pv.com);
                    // Attempt to open port
                    pv.packDispenser.PortOpen = true;

                    try
                    {
                        // Check if the comm port is open to continue
                        if (pv.packDispenser.PortOpen)
                        {
                            // Wait for dispenser to complete processing now we have connected
                            System.Threading.Thread.Sleep(1000);
                            // Refresh all dispenser properties
                            pv.packDispenser.UpdateStatus(waitTime);
                            // Check that we did refresh successfully
                            if (pv.packDispenser.Success)
                            {
                                // Reset the unit
                                pv.packDispenser.Reset();
                                // Update status for the user
                                CheckStatus(pv);
                                using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                                {
                                    StreamWriter writer = new StreamWriter(stream);
                                    writer.WriteLine(string.Format(@"Datetime {0} -- Pack Dispenser Successfully Initialized on COM{1}.", DateTime.Now, pv.com));
                                    writer.Flush();
                                    writer.Close();
                                }
                            }
                            else
                            {
                                // Error last command was not a success
                                pv.stateCode = (int)DispenserCommState.OFFLINE;
                                pv.statusCode = (int)DispenserStatus.Error;
                                pv.statusDesc = "Pack Dispenser Fatal Error.";
                                using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                                {
                                    StreamWriter writer = new StreamWriter(stream);
                                    writer.WriteLine(string.Format(@"Datetime {0} -- Init- UpdateStatus operation failed.", DateTime.Now));
                                    writer.Flush();
                                    writer.Close();
                                }
                            }

                        }
                        else
                        {
                            // Error last command was not a success
                            pv.stateCode = (int)DispenserCommState.OFFLINE;
                            pv.statusCode = (int)DispenserStatus.Error;
                            pv.statusDesc = "Port Not Open.";
                            using (FileStream stream = new FileStream(@"Technik.log", FileMode.Open, FileAccess.Read))
                            {
                                StreamWriter writer = new StreamWriter(stream);
                                writer.WriteLine(string.Format(@"Datetime {0} -- COM{1} port not found/open.", DateTime.Now, pv.com));
                                writer.Flush();
                                writer.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // Error last command was not a success
                        pv.stateCode = (int)DispenserCommState.OFFLINE;
                        pv.statusCode = (int)DispenserStatus.Error;
                        pv.statusDesc = "Pack Dispenser Fatal Error.";
                        using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                        {
                            StreamWriter writer = new StreamWriter(stream);
                            writer.WriteLine(string.Format(@"Datetime {0} -- Init - Exception Caught - Message {1}.", DateTime.Now, ex.Message));
                            writer.Flush();
                            writer.Close();
                        }
                    }
                    pv.id = counter;
                    dispensers.Add(pv);
                }

            }
        }
        private void Vend(PackVendor pv, short number)
        {
            short counter = 0;
            try
            {
                pv.packDispenser.UpdateStatus();
                CheckStatus(pv);

                if ((number > 0) && (!simulateDevice))
                {
                    for (counter = 0; counter < number; counter++)
                    {
                        using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                        {
                            StreamWriter writer = new StreamWriter(stream);
                            writer.WriteLine(string.Format(@"Datetime {0} -- Attempting to Vend Card#{1}.", DateTime.Now, counter));
                            writer.Flush();
                            writer.Close();
                        }
                        if ((pv.packDispenser.PortOpen) && (pv.packDispenser.Success))
                        {
                            if ((statusCode == (int)DispenserStatus.NoError) && (stateCode == (int)DispenserCommState.ONLINE))
                            {
                                using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                                {
                                    StreamWriter writer = new StreamWriter(stream);
                                    writer.WriteLine(string.Format(@"Datetime {0} -- Issuing Vend call for Card#{1}.", DateTime.Now, counter));
                                    writer.Flush();
                                    writer.Close();
                                }

                                Action<short> handler = InitiateVendEvent;
                                if (handler != null)
                                {
                                    handler(pv.id);
                                    using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                                    {
                                        StreamWriter writer = new StreamWriter(stream);
                                        writer.WriteLine(string.Format(@"Datetime {0} -- Publishing Initiate Vend Event- com{1}", DateTime.Now, pv.com));
                                        writer.Flush();
                                        writer.Close();
                                    }
                                }
                                pv.packDispenser.Vend();

                                //if(packDispenser.Success)
                                // raise an event to notify the application that a card has been sucessfully dispensed.
                                Action<bool> handler1 = CardVendStatusEvent;
                                if (handler != null)
                                {
                                    handler1(pv.packDispenser.Success);
                                    using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                                    {
                                        StreamWriter writer = new StreamWriter(stream);
                                        writer.WriteLine(string.Format(@"Datetime {0} -- Vend - CardVend Status Handler Called. Vend Status : {1}", DateTime.Now, pv.packDispenser.Success));
                                        writer.Flush();
                                        writer.Close();
                                    }
                                    if (pv.packDispenser.Success)
                                      cardIssued++;
                                }
                                Thread.Sleep(3000);

                            }
                            else
                            {
                                break;
                            }

                            // Recheck to ensure that was not the last product or a jam has occured
                            pv.packDispenser.UpdateStatus();
                            CheckStatus(pv);
                            if (0 != pv.packDispenser.Status)
                              break;
                        }
                        else
                        {
                            break;
                        }

                    }

                }
                else
                {
                    simulationTimer.Change(5000, 5000);
                }
            }
            catch (Exception ex)
            {
                using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter writer = new StreamWriter(stream);
                    writer.WriteLine(string.Format(@"Datetime {0} -- Vend - Exception Caught Message {1}.", DateTime.Now, ex.Message));
                    writer.Flush();
                    writer.Close();
                }
                pv.stateCode = (int)DispenserCommState.ONLINE;
                pv.statusCode = (int)DispenserStatus.Error;
                pv.statusDesc = "Pack Dispenser Fatal Error.";
            }
        }
        private void CheckStatus(PackVendor packVendor)
        {
            try
            {
                // Check the dispenser status and update user interface
                if (packVendor.packDispenser.Success && packVendor.packDispenser.Error == 0)
                {
                    switch (packVendor.packDispenser.Status)
                    {
                        case 0:
                            packVendor.statusCode = (int)DispenserStatus.NoError;
                            packVendor.stateCode = (int)DispenserCommState.ONLINE;
                            packVendor.statusDesc = " OK.  Dispenser is ready.";
                            break;
                        case 1:
                            packVendor.statusCode = (int)DispenserStatus.Error;
                            packVendor.stateCode = (int)DispenserCommState.ONLINE;
                            packVendor.statusDesc = "Pack Dispenser Stock Sold Out. No Product is available in any column.";
                            break;
                        case 2:
                            packVendor.statusCode = (int)DispenserStatus.Error;
                            stateCode = (int)DispenserCommState.ONLINE;
                            statusDesc = "Pack Dispenser Down. No Column is able to Dispense.";
                            break;
                        case 13:
                            packVendor.statusCode = (int)DispenserStatus.Error;
                            packVendor.stateCode = (int)DispenserCommState.ONLINE;
                            packVendor.statusDesc = "Product Jam.";
                            break;

                        case 14:
                            packVendor.statusCode = (int)DispenserStatus.Error;
                            packVendor.stateCode = (int)DispenserCommState.ONLINE;
                            packVendor.statusDesc = "Product sensed in wrong column (for multi-column dispensers).  Can also be caused by inserting an object through the front of the dispenser in an attempt to steal product.";
                            break;

                        default:
                            packVendor.statusCode = (int)DispenserStatus.Error;
                            packVendor.stateCode = (int)DispenserCommState.ONLINE;
                            packVendor.statusDesc = "Pack Dispenser Fatal Error.";
                            break;
                    }
                }
                else
                {
                    packVendor.statusCode = (int)DispenserStatus.Error;
                    packVendor.stateCode = (int)DispenserCommState.ONLINE;
                    packVendor.statusDesc = "Pack Dispenser Fatal Error.";
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "The port is closed.")
                {
                    packVendor.stateCode = (int)DispenserCommState.OFFLINE;
                    packVendor.statusCode = (int)DispenserStatus.Error;
                    packVendor.statusDesc = ex.Message;
                }
                else
                {
                    packVendor.statusDesc = "There is some problem with PackDispenser.";
                    packVendor.statusCode = (int)DispenserStatus.Error;
                }
            }
            using (FileStream stream = new FileStream(@"Technik.log", FileMode.Append, FileAccess.Write))
            {
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(string.Format(@"Datetime {0} -- CheckStatus on COM{1} || state-{2} || status - {3} || statusDesc - {4} .", DateTime.Now, packVendor.com, packVendor.stateCode, packVendor.statusCode, packVendor.statusDesc));
                writer.Flush();
                writer.Close();
            }
        }