public EltradeFiscalDeviceResponsePackage Send(EltradeFiscalDeviceRequestPackage package)
        {
            if (ReferenceEquals(null, package))
            {
                throw new ArgumentNullException(nameof(package));
            }

            response = EltradeFiscalDeviceResponsePackage.Empty;
            try
            {
                serialPort.DataReceived += Read;

                var bytes = package.Build(true);
                reading = true;
                log.Debug($"0x{package.Command.ToString("x2").ToUpper()} => {package.GetType().Name} {package.DataString}");
                serialPort.Write(bytes, 0, bytes.Length);

                var wait = 0;
                while (reading && wait++ < 500) // mynkow
                {
                    Thread.Sleep(10);           // KnowHow: https://social.msdn.microsoft.com/Forums/en-US/ce8ce1a3-64ed-4f26-b9ad-e2ff1d3be0a5/serial-port-hangs-whilst-closing?forum=Vsexpressvcs
                }
                serialPort.DataReceived -= Read;

                if (response?.Data?.Length != 0)
                {
                    log.Debug($"0x{response.Command.ToString("x2").ToUpper()} Response => {response.GetHumanReadableData()}");
                }
            }
            catch (IOException ex)
            {
                log.Warn("Repopening port", ex);
                response = null;//mynkow
                Dispose();
                FindFiscalDevicePort();
            }
            catch (Exception ex)
            {
                log.Error("PAFA!", ex);
                response = null;//mynkow
                throw;
            }

            return(response);
        }
Exemple #2
0
        public void Commit()
        {
            while (requests.Count != 0)
            {
                var request = requests.Dequeue();
                EltradeFiscalDeviceResponsePackage response = null;
                var success = false;
                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        response = driver.Send(request.Request);
                        if (response is null)
                        {
                            continue;
                        }

                        success = true;
                        break;
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(10);
                    }
                }

                if (success == false)
                {
                    requests.Clear();
                    return;
                }

                if (request.Then is null)
                {
                    continue;
                }

                var @continue = request.Then(response);
                if (@continue == false)
                {
                    requests.Clear();
                }
            }
        }
        private void Read(object sender, SerialDataReceivedEventArgs e)
        {
            var serialPortSender = sender as SerialPort;

            var buffer = new byte[serialPortSender.ReadBufferSize];

            while (reading)
            {
                try
                {
                    buffer = new byte[serialPortSender.ReadBufferSize];
                    var readBytes = serialPortSender.Read(buffer, 0, serialPortSender.ReadBufferSize);
                    response = new EltradeFiscalDeviceResponsePackage(buffer.Take(readBytes).ToArray());
                    if (response.Printing)
                    {
                        Thread.Sleep(SynWaitMilliseconds);
                        continue;
                    }
                    else
                    {
                        reading = false;
                    }
                }
                catch (TimeoutException ex)
                {
                    log.Error(ex.Message, ex);
                    response = null;//mynkow
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message, ex);
                    response = null;//mynkow
                    reading  = false;
                }
            }
        }
        private bool CheckPortConnectivity(string portName, byte[] bytes)
        {
            log.Debug("Start CheckPortConnectivity");
            if (string.IsNullOrEmpty(portName))
            {
                return(false);
            }

            try
            {
                if (ReferenceEquals(null, serialPort) || serialPort.PortName != portName)
                {
                    if (serialPort?.PortName != portName)
                    {
                        log.Debug($"Disposing old port {serialPort?.PortName}");
                        serialPort?.Dispose();
                    }

                    log.Debug($"Creating serial port {portName}");
                    serialPort = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One);

                    serialPort.ReadTimeout    = 500;
                    serialPort.WriteTimeout   = 500;
                    serialPort.Encoding       = Encoding.ASCII;
                    serialPort.ErrorReceived += SerialPort_ErrorReceived;
                }

                var attempts = 0;
                while (attempts < 5)
                {
                    try
                    {
                        OpenPort();

                        serialPort.Write(bytes, 0, bytes.Length);
                        Thread.Sleep(SynWaitMilliseconds);

                        var buffer    = new byte[serialPort.ReadBufferSize];
                        var readBytes = serialPort.Read(buffer, 0, serialPort.ReadBufferSize);

                        var response = new EltradeFiscalDeviceResponsePackage(buffer.Take(readBytes).ToArray()).GetHumanReadableData();
                        if (string.IsNullOrEmpty(response) == false)
                        {
                            lock (mutex)
                            {
                                lastWorkingPort = portName;
                                var responseElements = response.Split(',');
                                SerialNumber = responseElements[5];
                                FiscalNumber = responseElements[6];
                                log.Info($"---------- Fiscal device port {lastWorkingPort}, serial number {SerialNumber}, fiscal number {FiscalNumber} ----------");
                            }

                            log.Debug("End CheckPortConnectivity");
                            return(true);
                        }

                        attempts++;
                    }
                    catch (TimeoutException)
                    {
                        lock (mutex)
                        {
                            lastWorkingPort = string.Empty;
                            SerialNumber    = string.Empty;
                            FiscalNumber    = string.Empty;
                        }

                        log.Debug("End CheckPortConnectivity. Timeout.");
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message, ex);
                        if (attempts >= 5)
                        {
                            log.Debug("End CheckPortConnectivity. Error.");
                            throw;
                        }

                        attempts++;
                        Thread.Sleep(200);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
            }

            log.Error($"Unable to find fiscal device on port {portName}. Check cable connection!");
            return(false);
        }