/// <summary>
        /// 解码固定顺序数据段
        /// </summary>
        /// <param name="package"></param>
        /// <param name="container"></param>
        private void DecodeOrderedData(IProtocolPackage <byte[]> package, byte[] container)
        {
            var currentIndex = 0;

            for (var i = 0; i < package.Command.CommandDatas.Count; i++)
            {
                var data = package.Command.CommandDatas.First(obj => obj.DataIndex == i);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = data.DataName,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ComponentContent = container.SubBytes(currentIndex, currentIndex + data.DataLength)
                };

                currentIndex += data.DataLength;

                package.AppendData(component);
            }
        }
示例#2
0
        public void DecodeCommand(IProtocolPackage <byte[]> package)
        {
            var currentIndex = 0;

            var container = package[StructureNames.Data].ComponentContent;

            for (var i = 0; i < package.Command.CommandDatas.Count; i++)
            {
                var data = package.Command.CommandDatas.First(obj => obj.DataIndex == i);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = data.DataName,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ComponentContent = container.SubBytes(currentIndex, currentIndex + data.DataLength)
                };

                currentIndex += data.DataLength;

                package.AppendData(component);
            }

            package.DeviceNodeId = (string)DataConverter.DecodeComponentData(package[StructureNames.NodeId]);

            package.Finalization();
        }
示例#3
0
        public StringProtocolPackage(IProtocolCommand command)
        {
            Protocol = command.Protocol;

            Command = command;

            foreach (var structure in Protocol.ProtocolStructures)
            {
                var component = new PackageComponent <string>()
                {
                    ComponentName    = structure.StructureName,
                    DataType         = structure.DataType,
                    ComponentIndex   = structure.StructureIndex,
                    ComponentContent = Encoding.ASCII.GetString(structure.DefaultBytes)
                };

                this[structure.StructureName] = component;
            }

            foreach (var commandData in command.CommandDatas)
            {
                var component = new PackageComponent <string>()
                {
                    ComponentName  = commandData.DataName,
                    DataType       = commandData.DataConvertType,
                    ComponentIndex = commandData.DataIndex
                };

                AppendData(component);
            }
        }
示例#4
0
        public IProtocolPackage DecodeProtocol(byte[] bufferBytes, Protocol matchedProtocol)
        {
            var protocolString = Encoding.ASCII.GetString(bufferBytes);

            var package = new StringProtocolPackage()
            {
                Protocol = matchedProtocol, ReceiveDateTime = DateTime.Now
            };

            var structures = matchedProtocol.ProtocolStructures.ToList();

            var currentIndex = 0;

            var knownStructureLength = structures.Sum(protocolStructure => protocolStructure.StructureDataLength);

            for (var i = 0; i < structures.Count; i++)
            {
                var structure = structures.First(obj => obj.StructureIndex == i);

                var componentDataLength = structure.StructureName == StructureNames.Data && structure.StructureDataLength == 0
                    ? int.Parse(package["ContentLength"].ComponentContent) - knownStructureLength + 12
                    : structure.StructureDataLength;

                if (currentIndex + componentDataLength > protocolString.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return(package);
                }

                if (structure.StructureName == StructureNames.Data)
                {
                    DetectCommand(package, matchedProtocol);
                    if (package.Command == null)
                    {
                        package.Status = PackageStatus.InvalidHead;
                        return(package);
                    }
                    componentDataLength = package.Command.ReceiveBytesLength == 0 ? componentDataLength : package.Command.ReceiveBytesLength;
                }

                var component = new PackageComponent <string>
                {
                    ComponentName    = structure.StructureName,
                    DataType         = structure.DataType,
                    ComponentIndex   = structure.StructureIndex,
                    ComponentContent = protocolString.Substring(currentIndex, componentDataLength)
                };

                component.ComponentValue = ParseStructureValue(component.ComponentContent);

                currentIndex += componentDataLength;

                package[structure.StructureName] = component;
            }

            DecodeCommand(package);

            return(package);
        }
 public void Print(PackageComponent PrintComponent)
 {
     if (!SaveMe() || (IsClass2 && !Validate()))
     {
         return;
     }
     TaxForm.Print(PrintComponent, GUID);
 }
示例#6
0
 public void Print(PackageComponent printComponent)
 {
     if (!SaveMe() || (IsClass2 && !Validate()))
     {
         return;
     }
     Print(printComponent, GUID);
     OnPropertyChanged("Fields"); //to twiddle the InitPrtAbw field that is presumably bound to an "Un-Printed" type visual alert
 }
 private static JObject ToJObject(PackageComponent component)
 {
     return(new JObject
     {
         { "DisplayName", component.DisplayName },
         { "URI", component.Uri },
         { "Version", component.Version },
     });
 }
示例#8
0
        static private bool Print(PackageComponent printComponents, string guid)
        {
            if (!IsPrintOK())
            {
                return(false);
            }

            //nugget: sending raw ESCape codes to dot matrix printer (Epson's "ESC/P" standard)
            //nugget: ESC/P reference manual: http://files.support.epson.com/pdf/general/escp2ref.pdf
            //ESC @   = reset
            //ESC 3 n = n/216" line spacing
            //ESC M   = 12 cpi
            //ESC x n = quality: 0 = Draft, 1 = NLQ
            //ESC k n = font: 0 = Roman, 1 = Sans serif font
            //ESC p 0 = turn off proportional
            //ESC 2   = 1/6" line spacing = 0.166666666666666

// ReSharper disable InconsistentNaming
            using (var TaxForm_print = new Proc("TaxForm_print"))
// ReSharper restore InconsistentNaming
            {
                TaxForm_print["@TaxFormGUID"]    = guid; //for testprint, pass Guid.Empty i.e. '00000000-0000-0000-0000-000000000000'
                TaxForm_print["@PrintComponent"] = (int)printComponents;

                TaxForm_print.ExecuteDataSet();

                var poPrinter = new RawPrinterJob(
                    SettingsModel.Global["FormPrinterInitCodes"],
                    Convert.ToInt16(SettingsModel.Global["FormPrinterWidth"]),
                    Convert.ToInt16(SettingsModel.Global["FormPrinterHeight"]));

                var abwPrinter = poPrinter.Clone(); //saves the wasted effort of repopulating another blank page

                if (PrintFields(poPrinter, TaxForm_print.DataSet.Tables[0].Rows))
                {
                    if (guid == Guid.Empty.ToString())
                    {
                        poPrinter.PrintTestRulers();
                    }
                    poPrinter.SendToPrinter(SettingsModel.Local["POPrinter"]);
                }

                if (PrintFields(abwPrinter, TaxForm_print.DataSet.Tables[1].Rows))
                {
                    if (guid == Guid.Empty.ToString())
                    {
                        abwPrinter.PrintTestRulers();
                    }
                    abwPrinter.SendToPrinter(SettingsModel.Local["AbwPrinter"]);
                }

                CacheTables(TaxForm_print, "TaxForm"); //sync the updated PrintDate columns in the TableCache
            }
            return(true);
        }
示例#9
0
        public IProtocolPackage DecodeProtocol(byte[] bufferBytes, Protocol matchedProtocol)
        {
            var package = new BytesProtocolPackage()
            {
                Protocol = matchedProtocol, ReceiveDateTime = DateTime.Now
            };

            var structures = matchedProtocol.ProtocolStructures.ToList();

            var currentIndex = 0;

            for (var i = 0; i < structures.Count; i++)
            {
                var structure = structures.First(obj => obj.StructureIndex == i);

                //协议中,数据段如果是自由组织的形式,那么数据库中设置数据段长度为零。解码时,按照协议中的DataLength段的值解码数据段。
                var componentDataLength = structure.StructureName == StructureNames.Data && structure.StructureDataLength == 0
                    ? Globals.BytesToInt16(package["DataLength"].ComponentContent, 0, true)
                    : structure.StructureDataLength;

                if (currentIndex + componentDataLength > bufferBytes.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return(package);
                }

                if (structure.StructureName == StructureNames.Data)
                {
                    DetectCommand(package, matchedProtocol);
                    componentDataLength = package.Command.ReceiveBytesLength == 0 ? componentDataLength : package.Command.ReceiveBytesLength;
                }

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = structure.StructureName,
                    DataType         = structure.DataType,
                    ComponentIndex   = structure.StructureIndex,
                    ComponentContent = bufferBytes.SubBytes(currentIndex, currentIndex + componentDataLength)
                };

                currentIndex += componentDataLength;

                package[structure.StructureName] = component;
            }

            DecodeCommand(package);

            return(package);
        }
示例#10
0
        static public void PrintTest(PackageComponent printComponent)
        {
            Print(printComponent, Guid.Empty.ToString());

            /*this is a good place to jump in and do some test logic...
             * CharacterPrinter POPrinter = new CharacterPrinter(
             * Setting.Global["FormPrinterInitCodes"],
             * Convert.ToInt16(Setting.Global["FormPrinterWidth"]),
             * Convert.ToInt16(Setting.Global["FormPrinterHeight"]));
             *
             * //for (int i = 128; i < 178; i++) test1 += new string((char)i, 1);
             * //POPrinter.PrintStringAtPos(test2, 10, 11);
             *
             * POPrinter.PrintStringAtPos("A: Ää E: Ëë I: Ïï O: Öö U: Üü Y: Ÿÿ SS: ß Euro: €", 10, 10);
             *
             * POPrinter.SendToPrinter(Setting.Local["POPrinter"]);
             */
        }
示例#11
0
        /// <summary>
        /// 解码自由组合数据段
        /// </summary>
        /// <param name="package"></param>
        /// <param name="container"></param>
        private void DecodeRandomData(IProtocolPackage <byte[]> package, byte[] container)
        {
            var currentIndex = 0;

            while (currentIndex < container.Length)
            {
                var dataIndex = currentIndex + 2;

                var flagIndex = currentIndex + 1;

                var dataFlag = container[flagIndex] & 0x7F; //0x7F = 0111 1111

                var data = package.Command.CommandDatas.First(obj => obj.DataFlag == dataFlag);

                if (currentIndex + data.DataLength > container.Length)
                {
                    package.Status = PackageStatus.NoEnoughBuffer;
                    return;
                }

                var channel = container[currentIndex];

                var component = new PackageComponent <byte[]>
                {
                    ComponentName    = $"{data.DataName}-{channel}",
                    ComponentChannel = channel,
                    CommandData      = data,
                    DataValueType    = data.DataValueType,
                    DataType         = data.DataConvertType,
                    ComponentIndex   = data.DataIndex,
                    ValidFlag        = container[flagIndex],
                    ComponentContent = container.SubBytes(dataIndex, dataIndex + data.DataLength)
                };

                currentIndex = data.DataLength + dataIndex;

                package.AppendData(component);
            }
        }
示例#12
0
        static public void ResetPrinterSettings(PackageComponent printComponent)
        {
            //this routine sends a binary payload that resets the Epson FX-890 printer back to desirable defaults (listed below)

            //nugget: embedding a binary file in the assembly for runtime retrieval
            //http://www.dotnetscraps.com/dotnetscraps/post/Insert-any-binary-file-in-C-assembly-and-extract-it-at-runtime.aspx
            //the other main thing to do is set the file's properties to "Embedded Resource"

            //reset_epson.bin is generated via "Epson Remote Configuration Manager"
            //download here: http://www.epson.com/cgi-bin/Store/support/supDetail.jsp?BV_UseBVCookie=yes&oid=23730&infoType=Downloads
            //(make sure to run it from a CMD.EXE with %TEMP% set to an accessible 8.3 path, long path names screw up the setup.exe since it's from the Win9x days, but it does still work on Win7!)
            //it's a Windows app that bundles all the special settings and fires them at the printer
            //i could find no other way to select "Normal Draft" out of USD, HSD, Draft
            //this binary file also sets the top-of-form position to the smallest value (0.167)
            //and defaults the pitch to 12cpi

            using (var resetEpson = AssemblyHelper.GetEmbeddedResource("reset_epson.bin"))
            {
                RawPrinterHelper.SendStreamToPrinter(
                    SettingsModel.Local[printComponent == PackageComponent.OrderForm ? "POPrinter" : "AbwPrinter"],
                    resetEpson);
            }
        }
示例#13
0
        protected override void DecodeCommand(IProtocolPackage <string> package)
        {
            var container = package[StructureNames.Data].ComponentContent;

            container = container.Replace("CP=&&", string.Empty).Replace("&&", string.Empty);

            var dataGroups = container.Split(';');

            var commandDataDicts = (from dataGroup in dataGroups
                                    where dataGroup.Contains(",")
                                    from data in dataGroup.Split(',')
                                    select data.Split('='))
                                   .ToDictionary(dataKeyValuePair => dataKeyValuePair[0], dataKeyValuePair => dataKeyValuePair[1]);

            foreach (var commandDataDic in commandDataDicts)
            {
                var commandData = package.Command.CommandDatas.FirstOrDefault(obj => obj.DataName == commandDataDic.Key);
                if (commandData == null)
                {
                    continue;
                }
                var component = new PackageComponent <string>
                {
                    ComponentName    = commandDataDic.Key,
                    DataType         = commandData.DataConvertType,
                    ComponentIndex   = commandData.DataIndex,
                    ComponentContent = commandDataDic.Value
                };
                component.ComponentValue = component.ComponentContent;

                package.AppendData(component);
            }

            package.DeviceNodeId = package[StructureNames.NodeId].ComponentValue;

            package.Finalization();
        }
 /// <summary>
 /// Creates this instance.
 /// </summary>
 public PackageComponent Create()
 {
     DatabaseConnection db = new DatabaseConnection();
     System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(this.GetInsertSQL("PackageComponents"));
     db.RunScalarCommand(com);
     PackageComponent p = new PackageComponent(db.GetIdentity());
     db.Dispose();
     return p;
 }
示例#15
0
    static public void ResetPrinterSettings(PackageComponent printComponent)
    {
      //this routine sends a binary payload that resets the Epson FX-890 printer back to desirable defaults (listed below)

      //nugget: embedding a binary file in the assembly for runtime retrieval
      //http://www.dotnetscraps.com/dotnetscraps/post/Insert-any-binary-file-in-C-assembly-and-extract-it-at-runtime.aspx
      //the other main thing to do is set the file's properties to "Embedded Resource"

      //reset_epson.bin is generated via "Epson Remote Configuration Manager" 
      //download here: http://www.epson.com/cgi-bin/Store/support/supDetail.jsp?BV_UseBVCookie=yes&oid=23730&infoType=Downloads
      //(make sure to run it from a CMD.EXE with %TEMP% set to an accessible 8.3 path, long path names screw up the setup.exe since it's from the Win9x days, but it does still work on Win7!)
      //it's a Windows app that bundles all the special settings and fires them at the printer
      //i could find no other way to select "Normal Draft" out of USD, HSD, Draft
      //this binary file also sets the top-of-form position to the smallest value (0.167)
      //and defaults the pitch to 12cpi

      using(var resetEpson = AssemblyHelper.GetEmbeddedResource("reset_epson.bin"))
      {
        RawPrinterHelper.SendStreamToPrinter(
          SettingsModel.Local[printComponent == PackageComponent.OrderForm ? "POPrinter" : "AbwPrinter"],
          resetEpson);
      }
    }
示例#16
0
    static public void PrintTest(PackageComponent printComponent)
    {
      Print(printComponent, Guid.Empty.ToString());

      /*this is a good place to jump in and do some test logic...
      CharacterPrinter POPrinter = new CharacterPrinter(
        Setting.Global["FormPrinterInitCodes"],
        Convert.ToInt16(Setting.Global["FormPrinterWidth"]),
        Convert.ToInt16(Setting.Global["FormPrinterHeight"]));

      //for (int i = 128; i < 178; i++) test1 += new string((char)i, 1);
      //POPrinter.PrintStringAtPos(test2, 10, 11);

      POPrinter.PrintStringAtPos("A: Ää E: Ëë I: Ïï O: Öö U: Üü Y: Ÿÿ SS: ß Euro: €", 10, 10);

      POPrinter.SendToPrinter(Setting.Local["POPrinter"]);
      */
    }
        static private void Print(PackageComponent PrintComponent, string guid)
        {
            //nugget: sending raw ESCape codes to dot matrix printer (Epson's "ESC/P" standard)
            //nugget: ESC/P reference manual: http://files.support.epson.com/pdf/general/escp2ref.pdf
            //ESC @   = reset
            //ESC x n = quality: 0 = Draft, 1 = NLQ
            //ESC k n = font: 0 = Roman, 1 = Sans serif font
            //ESC M   = 12 cpi

            //reinitialize the page every time by filling the array with spaces plus a line feed at the end of every line...
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(pageinit), 0, pagebytes, 0, pageinit.Length);
            // "flattening a 2D array": http://www.dotnetperls.com/flatten-array
            // logicalrow = row * pagewidth
            byte[] rowbytes = Encoding.ASCII.GetBytes(new string(' ', pagewidth));
            rowbytes[pagewidth - 1]         = 0x0A; // linefeed (LF)
            pagebytes[pagebytes.Length - 1] = 0x0C; // form feed (FF)

            //apparently simple for-loop is the recommended best practice for this drudgery
            //http://stackoverflow.com/questions/1897555/what-is-the-equivalent-of-memset-in-c
            //http://stackoverflow.com/questions/6150097/initialize-a-byte-array-to-a-certain-value-other-than-the-default-null
            for (int row = 0; row < pageheight; row++)
            {
                PrintBytesAtPos(rowbytes, 0, row);
            }

            PrintStringAtPos(String.Format("{0}{0}{0}{0}{0}{0}{0}{0}{0}123456", "1234567890"), 0, 0);
            PrintStringAtPos(String.Format("2        1{0}2{0}3{0}4{0}5{0}6{0}7{0}8{0}9", "         "), 0, 1);
            for (int row = 2; row < pageheight; row++)
            {
                //we need to leave FormField data definition 0-based to be consistent with v1...
                //but want to view in 1-based on the hardcopy
                PrintStringAtPos((row + 1).ToString(), 0, row);
            }

            RawPrinterHelper.SendManagedBytesToPrinter(Setting.Local["POPrinter"], pagebytes);

            return;

            using (Proc TaxForm_print = new Proc("TaxForm_print"))
            {
                TaxForm_print["@TaxFormGUID"]    = guid; //for testprint, pass Guid.Empty i.e. '00000000-0000-0000-0000-000000000000'
                TaxForm_print["@PrintComponent"] = (int)PrintComponent;

                TaxForm_print.ExecuteDataSet();

                Printer POPrinter  = InitDotMatrix(Setting.Local["POPrinter"]);                                                                               //coding defensively, reset printer settings with every print... so agents can readily power cycle the printer with no ramifications, etc.
                Printer AbwPrinter = ((Setting.Local["POPrinter"] == Setting.Local["AbwPrinter"])) ?  POPrinter : InitDotMatrix(Setting.Local["AbwPrinter"]); //don't re-init the same printer

                if (guid == Guid.Empty.ToString())                                                                                                            //test mode...
                {
                    SetCurrentXY(POPrinter, 0, 0);
                    POPrinter.Write(String.Format("{0}{0}{0}{0}{0}{0}{0}{0}{0}123456", "1234567890"));

                    SetCurrentXY(POPrinter, 0, 1);
                    POPrinter.Write(String.Format("2        1{0}2{0}3{0}4{0}5{0}6{0}7{0}8{0}9", "         "));

                    for (int i = 2; i <= 63; i++)
                    {
                        //we need to leave FormField data definition 0-based to be consistent with v1...
                        SetCurrentXY(POPrinter, 0, i);
                        //but want to view in 1-based on the hardcopy
                        POPrinter.Write((i + 1).ToString());
                    }
                }

                PrintFields(POPrinter, TaxForm_print.dataSet.Tables[0].Rows);
                PrintFields(AbwPrinter, TaxForm_print.dataSet.Tables[1].Rows);
            }
        }
 static public void PrintTest(PackageComponent PrintComponent)
 {
     Print(PrintComponent, Guid.Empty.ToString());
 }
示例#19
0
    static private bool Print(PackageComponent printComponents, string guid)
    {
      if (!IsPrintOK()) return(false);

      //nugget: sending raw ESCape codes to dot matrix printer (Epson's "ESC/P" standard)
      //nugget: ESC/P reference manual: http://files.support.epson.com/pdf/general/escp2ref.pdf
      //ESC @   = reset
      //ESC 3 n = n/216" line spacing
      //ESC M   = 12 cpi
      //ESC x n = quality: 0 = Draft, 1 = NLQ
      //ESC k n = font: 0 = Roman, 1 = Sans serif font
      //ESC p 0 = turn off proportional
      //ESC 2   = 1/6" line spacing = 0.166666666666666

// ReSharper disable InconsistentNaming
      using (var TaxForm_print = new Proc("TaxForm_print"))
// ReSharper restore InconsistentNaming
      {
        TaxForm_print["@TaxFormGUID"] = guid; //for testprint, pass Guid.Empty i.e. '00000000-0000-0000-0000-000000000000'
        TaxForm_print["@PrintComponent"] = (int)printComponents;

        TaxForm_print.ExecuteDataSet();

        var poPrinter = new RawPrinterJob(
          SettingsModel.Global["FormPrinterInitCodes"],
          Convert.ToInt16(SettingsModel.Global["FormPrinterWidth"]),
          Convert.ToInt16(SettingsModel.Global["FormPrinterHeight"]));

        var abwPrinter = poPrinter.Clone(); //saves the wasted effort of repopulating another blank page

        if (PrintFields(poPrinter, TaxForm_print.DataSet.Tables[0].Rows))
        {
          if (guid == Guid.Empty.ToString()) poPrinter.PrintTestRulers();
          poPrinter.SendToPrinter(SettingsModel.Local["POPrinter"]);
        }

        if (PrintFields(abwPrinter, TaxForm_print.DataSet.Tables[1].Rows))
        {
          if (guid == Guid.Empty.ToString()) abwPrinter.PrintTestRulers();
          abwPrinter.SendToPrinter(SettingsModel.Local["AbwPrinter"]);
        }

        CacheTables(TaxForm_print, "TaxForm"); //sync the updated PrintDate columns in the TableCache
      }
      return (true);
    }
 public void SavePackageComponents(int OwningPackageId, PackageComponent[] Components)
 {
     Package package = new Package(OwningPackageId);
     package.ClearPackageComponents();
     foreach (PackageComponent c in Components)
     {
         c.PackageId = OwningPackageId;
         PackageComponent newComponent = c.Create();
         // Attach the products
         foreach (string productId in c.ProductsString.Split(','))
             if (!String.IsNullOrEmpty(productId))
                 newComponent.AttachProduct(Convert.ToInt32(productId));
     }
 }
示例#21
0
 public void Print(PackageComponent printComponent)
 {
   if (!SaveMe() || (IsClass2 && !Validate())) return;
   Print(printComponent, GUID);
   OnPropertyChanged("Fields"); //to twiddle the InitPrtAbw field that is presumably bound to an "Un-Printed" type visual alert
 }