コード例 #1
0
        public static dynamic CreateSerializerFromType(DeviceCommand device)
        {
            switch (device)
            {
            case DeviceCommand.PrintReceipt:
                return(new EsponPrintReceiptSerializer());

            default:
                throw new System.NotSupportedException($"Device hub didnot support serilizer for command {device.ToString()}");
            }
        }
コード例 #2
0
        private void CommPortThreadProc()
        {
            App app = (App)Application.Current;

            byte[] commandBytes = new byte[COMMAND_BUFFER_SIZE];

            int errorCount = 0;

            for (;;)
            {
                try
                {
                    if (app.nextCommands.Count > 0)
                    {
                        DeviceCommand deviceCommand = app.nextCommands.Dequeue();
                        int           length        = deviceCommand.getBytes(commandBytes);
                        app.p.Write(commandBytes, 0, length);
                        //Console.WriteLine("Command: " + deviceCommand.ToString());
                        int attempts = 0;
                        int offset   = 0;
                        length = deviceCommand.answerBytes;
                        for (;;)
                        {
                            int count = app.p.Read(commandBytes, offset, length);
                            length -= count;
                            offset += count;
                            if (length == 0)
                            {
                                break;
                            }
                            attempts++;
                            if (attempts == 10)
                            {
                                MessageBox.Show("Command = " + deviceCommand.ToString() + ", answer length: expected = " + deviceCommand.answerBytes +
                                                "received = " + offset + (offset == 0 ? "" : ", answer: " + BitConverter.ToString(commandBytes, 0, offset)),
                                                "Wrong device answer length", MessageBoxButton.OK, MessageBoxImage.Error);
                                break;
                            }
                        }
                        if (length == 0)
                        {
                            //Console.WriteLine("Answer: " + BitConverter.ToString(commandBytes, 0, count)));
                            if (!deviceCommand.CheckAnswer(commandBytes))
                            {
                                MessageBox.Show("Command = " + deviceCommand.ToString() + ", answer = " + BitConverter.ToString(commandBytes, 0, offset),
                                                "Wrong device answer", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }
                    errorCount = 0;
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Comm port error", MessageBoxButton.OK, MessageBoxImage.Error);
                    errorCount++;
                }
                if (errorCount > 3)
                {
                    MessageBox.Show("Maximum error count reached. Giving up.", "Comm port error", MessageBoxButton.OK, MessageBoxImage.Error);
                    break;
                }
            }
        }
コード例 #3
0
        public static dynamic CreateDeviceFromType(DeviceCommand device)
        {
            switch (device)
            {
            case DeviceCommand.PrintReceipt:
                return(new EpsonPrinterDevice());

            default:
                throw new System.NotSupportedException($"Device hub didnot support device for command {device.ToString()}");
            }
        }