Отправка необработанных данных на матричный USB-принтер с использованием C # - PullRequest
0 голосов
/ 06 апреля 2019

Я работаю над простым приложением, которое будет читать текстовый файл, который каждые 5 минут будет перезаписываться некоторыми данными телеметрии.

Я пытался использовать Microsoft PrintDocument, но в этом случае это не сработало, потому что мне нужно было контролировать спулер принтера, чтобы печатать новую строку каждые пять, но на той же странице, что и предыдущая (непрерывно). Это невозможно с классом PrintDocument, по крайней мере, для моего понимания, потому что вы не можете управлять спулером с помощью этого класса, и каждая команда печати выполняет задание, и когда оно завершает задание, принтер прокручивает страницу, которую должен принять пользователь. Если кто-то знает, есть ли способ продолжить печать на одной и той же странице каждые 5 минут, поделитесь информацией или примером.

Я использую USB-матричный принтер Okidata Microline 186, и после нескольких попыток с другими сторонними библиотеками мне не повезло, я нашел класс RawPrintHelper.cs, и я пытаюсь это сделать, и я смог отправить данные RAW на принтер с этим классом, но у меня есть некоторые проблемы. Класс здесь:

Это оригинальный код класса:

открытый класс RawPrinterHelper { // Struct

ure 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 SendFileToPrinter( string szPrinterName, string szFileName )
  {
    // Open the file.
    FileStream fs = new FileStream(szFileName, FileMode.Open);
    // Create a BinaryReader on the file.
    BinaryReader br = new BinaryReader(fs);
    // Dim an array of bytes big enough to hold the file's contents.
    Byte []bytes = new Byte[fs.Length];
    bool bSuccess = false;
    // Your unmanaged pointer.
    IntPtr pUnmanagedBytes = new IntPtr(0);
    int nLength;

    nLength = Convert.ToInt32(fs.Length);
    // Read the contents of the file into the array.
    bytes = br.ReadBytes( nLength );
    // Allocate some unmanaged memory for those bytes.
    pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
    // Copy the managed byte array into the unmanaged array.
    Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
    // Send the unmanaged bytes to the printer.
    bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
    // Free the unmanaged memory that you allocated earlier.
    Marshal.FreeCoTaskMem(pUnmanagedBytes);
    return bSuccess;
  }

  public static bool SendStringToPrinter( string szPrinterName, string szString )
  {
    IntPtr pBytes;
    Int32 dwCount;

    // How many characters are in the string?
    // Fix from Nicholas Piasecki:
    // dwCount = szString.Length;
    dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

    // 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;
  }
}

 public partial class USB : Form
{
    public USB()
    {
        InitializeComponent();
    }


    // 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 SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;

        // How many characters are in the string?
        // Fix from Nicholas Piasecki:
        // dwCount = szString.Length;
        dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

        // 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 buttonSEND_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
        string dados = "";
        if (open.ShowDialog().Equals(DialogResult.OK))
        {
            StreamReader sr = new StreamReader(open.FileName);
            dados = sr.ReadToEnd();
            sr.Close();
        }
        string printer = "PRINTER NAME";

        for (int i = 0; i < 30; i++)
        {
            SendStringToPrinter(printer, dados);    
        }

    }
}

Issues:
First problem was that it prints only using a file with this method "SendFileToPrinter":

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);


   // Free the unmanaged memory that you allocated earlier.
    Marshal.FreeCoTaskMem(pUnmanagedBytes);
    return bSuccess;
}

Однако это не работает идеально, головка принтера остается на правой стороне страницы, и при повторной попытке печати она пишет начало текста в правой части страницы, а затем переходит к остальной части текста. , Второй вариант, который использует этот метод, который получает строку для печати:

public static bool SendStringToPrinter(string szPrinterName, string szString)
{
    IntPtr pBytes;
    Int32 dwCount;

    // How many characters are in the string?
    // Fix from Nicholas Piasecki:
    // dwCount = szString.Length;
    dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

    // 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;
} 

Но этот метод не отправляет что-либо на принтер, принтер не начинает печать. Я сделал гибрид из обеих функций, взглянув на оба метода, и я наконец смог распечатать этот код, но с теми же проблемами выравнивания, что и с SendFileToPrinter, не важно, если я отправляю текст всегда в том же формате, разделенном вкладками в каждой строке то, что я посылаю на принтер, плохо выровнено.

Это мой кусок кода:

        public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;
        bool bSuccess = false;
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength = Encoding.ASCII.GetBytes(szString).Length;
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[nLength];
        bytes = Encoding.ASCII.GetBytes(szString);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }

Это пример того, что мне нужно напечатать:

Ожидаемый формат

Первые строки дерева для заголовка таблицы. и следующие строки будут печататься одна из них каждые 5 минут с показаниями значений.

Как бы то ни было, это не соответствует даже после форматирования строки и разделения на табуляцию.

Это то, что я получаю, когда печатаю по методу Франкенштейна.

НАЗВАНИЕ СТРАНИЦЫ ........ ЛИНИЯ 1 VAL3 VAL4 VAL1 VAL2
Время / дата DESC DESC DESC DESC ........ ЛИНИЯ 3 04.03.2009 11:30 100 300 500 434 ........ ЛИНИЯ 4 3/4/2019 11:35 утра 99 290 340 452 ........ ЛИНИЯ 5 3/4/2019 11:40 AM 120 310 100 532 ........ ЛИНИЯ 6

Не соответствует, и только первая строка печатается нормально и выровнено. Я новичок в том, что отправляю необработанные данные на принтер, и я не знаю, что происходит, когда я вызываю метод SendStringToPrinter (myString) каждые пять минут. Я не знаю, нужно ли мне указывать координаты или где начать печатать, как это сделать.

Буду признателен за любую помощь, чтобы решить эту проблему.

1 Ответ

0 голосов
/ 06 апреля 2019

Для первой части вопроса, да, WritePrinter - это путь, который позволяет отправлять необработанные байты.Я не буду вдаваться в подробности всего вашего кода, потому что это слишком много кода для этого вопроса.

Для части макета этого вопроса этот принтер выглядит так, как будто он ожидает ESC / POS.Вот командный лист .Обновите ваш текстовый файл, чтобы он соответствовал этим командам, а именно разделу формата страницы.Например,

ESC HT 12 Hello 08 World

выровняет «Мир» относительно позиции 8-го символа.Вам нужно будет поэкспериментировать с командами, чтобы найти то, что работает.Если вы новичок в ESC / POS, я бы порекомендовал использовать notepad ++ для создания тестовых файлов.Если вы активируете панель символов (Edit-> Character Panel), вы можете вставить произвольные коды ASCII в файл.Строка ESC / POS, описанная выше, будет выглядеть так:

1b090cHello09World
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...