Pregunta

Estoy tratando de imprimir los datos ASCII sin procesar en una impresora térmica. Lo hago a través de este ejemplo de código: http://support.microsoft.com/kb/322091 pero mi impresora imprime siempre un solo personaje y esto no hasta que pulse el botón de avance. Si imprimo algo con la libreta de la impresora hará un avance de forma automática, pero sin imprimir cualquier texto.

La impresora está conectada a través de USB a través de un adaptador lpt2usb y Windows 7 utiliza el. "Genérico -> genérico / sólo texto" controlador

Cualquier persona sabe lo que va mal? ¿Cómo es posible imprimir algunas palabras y hacer algunos datos cargados? ¿Hay algunos caracteres de control que tengo que enviar? Y en caso afirmativo: ¿Cómo les envío

?

Editar 14.04.2010 21:51

Mi código (C #) es similar al siguiente:

PrinterSettings s =  new PrinterSettings();
s.PrinterName = "Generic / Text Only";

RawPrinterHelper.SendStringToPrinter(s.PrinterName, "Test");

Este código volverá una "T" después de presionar el botón de avance (Este botón litte negro aquí: swissmania.ch/images/935-151.jpg - lo siento, no es suficiente reputación por dos hipervínculos)

Editar 15.04.2010 16:56

Ahora estoy usando el formato de código aquí: c-sharpcorner.com/UploadFile/johnodonell/PrintingDirectlytothePrinter11222005001207AM/PrintingDirectlytothePrinter.aspx

Me lo modificó un poco que puedo usar el siguiente código:

byte[] toSend;
// 10 = line feed
// 13 carriage return/form feed
toSend = new byte[1] { 13 };
PrintDirect.WritePrinter(lhPrinter, toSend, toSend.Length, ref pcWritten);

Al ejecutar este código tiene el mismo effekt como pulsar el botón de avance, que trabaja muy bien!

Sin embargo, un código como éste todavía no funciona:

byte[] toSend;
// 10 = line feed
// 13 carriage return/form feed
toSend = new byte[2] { 66, 67 };
PrintDirect.WritePrinter(lhPrinter, toSend, toSend.Length, ref pcWritten);

Esto imprimirá sólo una "B" pero espero que "BC" y después de ejecutar cualquier código que tengo que volver a conectar el cable USB para que funcione alquilásemos. Algunas ideas?

¿Fue útil?

Solución 2

Bueno, la razón de todo eso es sólo el hecho de que yo use un adaptador, porque la computadora no tiene un puerto LPT de edad. He copiado mi solicitud a un viejo ordenador con Windows XP y todo funciona bien.

Ahora tengo la esperanza de que algunos otros adaters lpt2usb que compraron hacer su trabajo correctamente.

Editar 20.04.2010

Con otro adaptador lpt2usb todo funciona bien ahora. Si alguien está en trayectos al todo el código que estoy utilizando ahora, por favor, póngase en contacto conmigo o comentario aquí.

Otros consejos

paso rápido a paso la solución

Debido a que no se proporcionó código, hacer que funcione con la ayuda de los enlaces proporcionados, y aquí está el código:

Código

using System;
using System.Runtime.InteropServices;
using System.Windows;

[StructLayout(LayoutKind.Sequential)]
public struct DOCINFO {
    [MarshalAs(UnmanagedType.LPWStr)]
    public string pDocName;
    [MarshalAs(UnmanagedType.LPWStr)] 
    public string pOutputFile;
    [MarshalAs(UnmanagedType.LPWStr)] 
    public string pDataType;
}

public class PrintDirect {
    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long WritePrinter(IntPtr hPrinter, string data, int buf, ref int pcWritten);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long ClosePrinter(IntPtr hPrinter);
}

private void Print(String printerAddress, String text, String documentName) {
    IntPtr printer = new IntPtr();

    // A pointer to a value that receives the number of bytes of data that were written to the printer.
    int pcWritten = 0;

    DOCINFO docInfo = new DOCINFO();
    docInfo.pDocName = documentName;
    docInfo.pDataType = "RAW";

    PrintDirect.OpenPrinter(printerAddress, ref printer, 0);
    PrintDirect.StartDocPrinter(printer, 1, ref docInfo);
    PrintDirect.StartPagePrinter(printer);

    try {
    PrintDirect.WritePrinter(printer, text, text.Length, ref pcWritten);
    } catch (Exception e) {
        Console.WriteLine(e.Message);
    }

    PrintDirect.EndPagePrinter(printer);
    PrintDirect.EndDocPrinter(printer);
    PrintDirect.ClosePrinter(printer);
}

Uso

String printerAddress = "\\\\ComputerName\\PrinterName";
String documentName = "My document";
String documentText = "This is an example of printing directly to a printer.";

this.Print (printerAddress, DocumentText, DocumentName);

Fuentes:

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top