Пример #1
0
 protected (string, DeviceStatus) Request(byte command, string?data = null)
 {
     lock (frameSyncLock)
     {
         if (DeadLine < DateTime.Now)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E999", "User timeout occured while sending the request");
             return(string.Empty, deviceStatus);
         }
         try
         {
             return(ParseResponse(RawRequest(command, data == null ? null : PrinterEncoding.GetBytes(data))));
         }
         catch (StandardizedStatusMessageException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError(e.Code, e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (InvalidResponseException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E107", e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (Exception e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E101", e.Message);
             return(string.Empty, deviceStatus);
         }
     }
 }
Пример #2
0
 protected (string, DeviceStatus) Request(string?data = null)
 {
     lock (frameSyncLock)
     {
         try
         {
             System.Diagnostics.Trace.WriteLine($"Request: '{data}'");
             var response = ParseResponse(RawRequest(data == null ? null : PrinterEncoding.GetBytes(data)));
             if (data == "00")
             {
                 return(response, new DeviceStatus());
             }
             var rawStatus = ParseRawResponse(RawRequest(data == null ? null : PrinterEncoding.GetBytes("F80C")));
             return(response, ParseStatus(rawStatus));
         }
         catch (InvalidResponseException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E107", e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (Exception e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E101", e.Message);
             return(string.Empty, deviceStatus);
         }
     }
 }
Пример #3
0
 protected (string, DeviceStatus) Request(byte command, string?data = null)
 {
     lock (frameSyncLock)
     {
         try
         {
             return(ParseResponse(RawRequest(command, data == null ? null : PrinterEncoding.GetBytes(data))));
         }
         catch (StandardizedStatusMessageException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError(e.Code, e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (InvalidResponseException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E107", e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (Exception e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E101", e.Message);
             return(string.Empty, deviceStatus);
         }
     }
 }
Пример #4
0
 protected (string, DeviceStatus) Request(string?data = null)
 {
     lock (frameSyncLock)
     {
         if (DeadLine < DateTime.Now)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E999", "User timeout occured while sending the request");
             return(string.Empty, deviceStatus);
         }
         try
         {
             var response = ParseResponse(RawRequest(data == null ? null : PrinterEncoding.GetBytes(data)));
             if (data == "00")
             {
                 return(response, new DeviceStatus());
             }
             var rawStatus = ParseRawResponse(RawRequest(data == null ? null : PrinterEncoding.GetBytes("F80C")));
             return(response, ParseStatus(rawStatus));
         }
         catch (InvalidResponseException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E107", e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (Exception e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E101", e.Message);
             return(string.Empty, deviceStatus);
         }
     }
 }
Пример #5
0
        public virtual (string, DeviceStatus) GetRawDeviceInfo()
        {
            var(response00, status00) = Request("00");
            if (!status00.Ok)
            {
                throw new InvalidDeviceInfoException();
            }

            var fields = response00.Split(new int[] { 4, 4 });

            if (fields.Length != 2)
            {
                throw new InvalidDeviceInfoException();
            }
            DeviceNo = PrinterEncoding.GetBytes(fields[1]);

            var(responseFP, statusFP) = Request("F807");
            if (!statusFP.Ok)
            {
                throw new InvalidDeviceInfoException();
            }

            var response = $"{response00}\t{responseFP}";

            return(response, statusFP);
        }
Пример #6
0
 protected virtual (string, DeviceStatus) ParseResponse(byte[]?rawResponse)
 {
     var(response, status) = ParseResponseAsByteArray(rawResponse);
     if (response == null)
     {
         return("", status);
     }
     return(PrinterEncoding.GetString(response), status);
 }
Пример #7
0
        protected virtual string ParseResponse(byte[]?rawResponse)
        {
            var data = ParseRawResponse(rawResponse);

            if (data == null)
            {
                return(string.Empty);
            }

            var response = PrinterEncoding.GetString(data);

            return(response);
        }
Пример #8
0
        protected virtual byte[] BuildHostFrame(byte[]?data)
        {
            // Frame header
            var frame = new List <byte>();

            frame.Add(MarkerSTX);
            frame.AddRange(DeviceNo == null ? PrinterEncoding.GetBytes("0000") : DeviceNo);
            frame.AddRange(data);
            frame.AddRange(ByteTo2Bytes((byte)(10 + (data != null ? data.Length : 0))));
            frame.AddRange(ComputeCS(frame.ToArray()));
            frame.Add(MarkerETX);

            return(frame.ToArray());
        }
        public override DeviceStatusWithRawResponse RawRequest(RequestFrame requestFrame)
        {
            if (requestFrame.RawRequest.Length == 0)
            {
                var deviceStatus = new DeviceStatus();
                deviceStatus.AddError("E401", "Request length must be at least 1 character");
                return(new DeviceStatusWithRawResponse(deviceStatus));
            }
            var cmd  = PrinterEncoding.GetBytes(requestFrame.RawRequest.Substring(0, 1))[0];
            var data = requestFrame.RawRequest.Substring(1);

            var(rawResponse, status) = Request(cmd, data);
            var deviceStatusWithRawResponse = new DeviceStatusWithRawResponse(status);

            deviceStatusWithRawResponse.RawResponse = rawResponse;
            return(deviceStatusWithRawResponse);
        }
Пример #10
0
        protected virtual (string, DeviceStatus) ParseResponse(byte[]?rawResponse)
        {
            if (rawResponse == null)
            {
                throw new InvalidResponseException("no response");
            }
            var(preamblePos, separatorPos, postamblePos, terminatorPos) = (0u, 0u, 0u, 0u);
            for (uint i = 0u; i < rawResponse.Length; i++)
            {
                var b = rawResponse[i];
                switch (b)
                {
                case MarkerPreamble:
                    preamblePos = i;
                    break;

                case MarkerSeparator:
                    separatorPos = i;
                    break;

                case MarkerPostamble:
                    postamblePos = i;
                    break;

                case MarkerTerminator:
                    terminatorPos = i;
                    break;
                }
            }
            if (preamblePos + 4 <= separatorPos && separatorPos + 6 < postamblePos && postamblePos + 4 < terminatorPos)
            {
                var data        = rawResponse.Slice(preamblePos + 4, separatorPos);
                var status      = rawResponse.Slice(separatorPos + 1, postamblePos);
                var bcc         = rawResponse.Slice(postamblePos + 1, terminatorPos);
                var computedBcc = ComputeBCC(rawResponse.Slice(preamblePos + 1, postamblePos + 1));
                if (bcc.SequenceEqual(computedBcc))
                {
                    var response = PrinterEncoding.GetString(data);

                    return(response, ParseStatus(status));
                }
            }
            throw new InvalidResponseException("The response is invalid. Checksum does not match.");
        }
 protected (string, DeviceStatus) Request(byte command, string?data = null)
 {
     lock (frameSyncLock)
     {
         try
         {
             System.Diagnostics.Trace.WriteLine($"Request({command:X}): '{data}'");
             return(ParseResponse(RawRequest(command, data == null ? null : PrinterEncoding.GetBytes(data))));
         }
         catch (InvalidResponseException e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E107", e.Message);
             return(string.Empty, deviceStatus);
         }
         catch (Exception e)
         {
             var deviceStatus = new DeviceStatus();
             deviceStatus.AddError("E101", e.Message);
             return(string.Empty, deviceStatus);
         }
     }
 }
Пример #12
0
 protected (string, DeviceStatus) Request(byte command, string data)
 {
     System.Diagnostics.Trace.WriteLine($"Request({command:X}): '{data}'");
     return ParseResponse(RawRequest(command, PrinterEncoding.GetBytes(data)));
 }