Что нового

Печать текста на термопринтере

dennsp

Новичок
Сообщения
1
Репутация
0
Версия AutoIt:
3.3.8.1

Описание:
Нужно распечатать на термопринтере (Star TSP100) какой-нибудь текст, используя ESC-коды принтера.

Имеется исходник на C#, печатающий чек, нужно вывести что-то похожее, используя autoit.

Примечания:
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using StarMicronics.StarIO;

namespace SendingRawData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        public class RawPrinterHelper
        {
            // Structure and API declarions:
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
            public class DOCINFOA
            {
                [MarshalAs(UnmanagedType.LPStr)]
                public string pDocName;
                [MarshalAs(UnmanagedType.LPStr)]
                public string pOutputFile;
                [MarshalAs(UnmanagedType.LPStr)]
                public string pDataType;
            }
            [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

            [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool ClosePrinter(IntPtr hPrinter);

            [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

            [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool EndDocPrinter(IntPtr hPrinter);

            [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool StartPagePrinter(IntPtr hPrinter);

            [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool EndPagePrinter(IntPtr hPrinter);

            [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

            // SendBytesToPrinter()
            // When the function is given a printer name and an unmanaged array
            // of bytes, the function sends those bytes to the print queue.
            // Returns true on success, false on failure.
            public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
            {
                Int32 dwError = 0, dwWritten = 0;
                IntPtr hPrinter = new IntPtr(0);
                DOCINFOA di = new DOCINFOA();
                bool bSuccess = false; // Assume failure unless you specifically succeed.

                di.pDocName = "My C#.NET RAW Document";
                di.pDataType = "RAW";

                // Open the printer.
                if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
                {
                    // Start a document.
                    if (StartDocPrinter(hPrinter, 1, di))
                    {
                        // Start a page.
                        if (StartPagePrinter(hPrinter))
                        {
                            // Write your bytes.
                            bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                            EndPagePrinter(hPrinter);
                        }
                        EndDocPrinter(hPrinter);
                    }
                    ClosePrinter(hPrinter);
                }
                // If you did not succeed, GetLastError may give more information
                // about why not.
                if (bSuccess == false)
                {
                    dwError = Marshal.GetLastWin32Error();
                }
                return bSuccess;
            }

         
            public static bool SendStringToPrinter(string szPrinterName, string szString)
            {
                IntPtr pBytes;
                Int32 dwCount;
                // How many characters are in the string?
                dwCount = szString.Length;
                // Assume that the printer is expecting ANSI text, and then convert
                // the string to ANSI text.
                pBytes = Marshal.StringToCoTaskMemAnsi(szString);
                // Send the converted ANSI string to the printer.
                SendBytesToPrinter(szPrinterName, pBytes, dwCount);
                Marshal.FreeCoTaskMem(pBytes);
                return true;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            string buffer = "\x1b\x1d\x61\x1";             //Center Alignment - Refer to Pg. 3-29
            buffer = buffer + "\x5B" + "If loaded.. Logo1 goes here" + "\x5D\n";
            buffer = buffer + "\x1B\x1C\x70\x1\x0";          //Stored Logo Printing - Refer to Pg. 3-38
            buffer = buffer + "Star Clothing Boutique\n";
            buffer = buffer + "1150 King Georges Post Rd.\n";
            buffer = buffer + "Edison, NJ 08837\n\n";
            buffer = buffer + "\x1b\x1d\x61\x0";             //Left Alignment - Refer to Pg. 3-29
            buffer = buffer + "\x1b\x44\x2\x10\x22\x0";      //Setting Horizontal Tab - Pg. 3-27
            buffer = buffer + "Date: 12/31/2008 " + "\x9" + " Time: 9:10 PM\n";      //Moving Horizontal Tab - Pg. 3-26
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "\x1b\x45";                    //Select Emphasized Printing - Pg. 3-14
            buffer = buffer + "SALE\n";
            buffer = buffer + "\x1b\x46";                    //Cancel Emphasized Printing - Pg. 3-14
            buffer = buffer + "300678566 " + "\x9" + "  PLAN T-SHIRT" + "\x9" + "         10.99\n";
            buffer = buffer + "300692003 " + "\x9" + "  BLACK DENIM" + "\x9" + "         29.99\n";
            buffer = buffer + "300651148 " + "\x9" + "  BLUE DENIM" + "\x9" + "         29.99\n";
            buffer = buffer + "300642980 " + "\x9" + "  STRIPE DRESS" + "\x9" + "         49.99\n";
            buffer = buffer + "300638471 " + "\x9" + "  BLACK BOOT" + "\x9" + "         35.99\n\n";
            buffer = buffer + "Subtotal " + "\x9" + "" + "\x9" + "        156.95";
            buffer = buffer + "Tax " + "\x9" + "" + "\x9" + "" + "         00.00";
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "Total" + "\x6" + "" + "\x9" + "\x1b\x69\x1\x1" + "         $156.95\n";    //Character Expansion - Pg. 3-10
            buffer = buffer + "\x1b\x69\x0\x0";                                                          //Cancel Expansion - Pg. 3-10
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "Charge\n" + "$159.95\n";
            buffer = buffer + "Visa XXXX-XXXX-XXXX-0123\n\n";
            buffer = buffer + "\x1b\x34" + "Refunds and Exchanges" + "\x1b\x35\n";                       //Specify/Cencel White/Black Invert - Pg. 3-16
            buffer = buffer + "Within " + "\x1b\x2d\x1" + "30 days" + "\x1b\x2d\x0" + " with receipt\n"; //Specify/Cancel Underline Printing - Pg. 3-15
            buffer = buffer + "And tags attached\n\n";
            buffer = buffer + "\x1b\x1d\x61\x1";             //Center Alignment - Refer to Pg. 3-29
            buffer = buffer + "\x1b\x62\x6\x2\x2" + " 12ab34cd56" + "\x1e\n";             //Barcode - Pg. 3-39 - 3-40
            buffer = buffer + "\x1b\x64\x02";                                            //Cut  - Pg. 3-41
            buffer = buffer + "\x7";                                                    //Open Cash Drawer


            RawPrinterHelper.SendStringToPrinter("TSP100", buffer);
        }
        

    }
}
 
Верх