Pergunta

Estou tentando imprimir dados RAW ASCII em uma impressora térmica. Eu faço isso usando este exemplo de código: http://support.microsoft.com/kb/322091 Mas minha impressora impressa sempre apenas um caractere e isso não até que eu pressione o botão de alimentação do formulário. Se eu imprimir algo com o bloco de notas, a impressora fará um feed de formulário automaticamente, mas sem imprimir nenhum texto.

A impressora está conectada via USB em um adaptador LPT2USB e o Windows 7 usa o driver "genérico -> genérico / apenas texto".

Alguém sabe o que está dando errado? Como é possível imprimir algumas palavras e fazer alguns feeds de formulário? Existem alguns personagens de controle que tenho que enviar? E se sim: como faço para enviá -los?

Editar 14.04.2010 21:51

Meu código (c#) se parece com o seguinte:

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

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

Este código retornará um "T" depois que eu pressionei o botão Formulário (este botão Litte Black aqui: Swissmania.ch/images/935-151.jpg - Desculpe, não há reputação suficiente por dois hiperlinks)

Editar 15.04.2010 16:56

Estou usando agora o formulário de código aqui: c-sharpcorner.com/uploadfile/johnodonell/printingdirectlytotheprinter11222005001207am/printingdirectlytotheprinter.aspx

Modifiquei um pouco que posso usar o seguinte 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);

A execução deste código tem o mesmo Efekt, como pressionar o botão Formulário, ele funciona bem!

Mas código como esse ainda não 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);

Isso vai imprimir apenas um "b", mas eu espero "bc" e Depois de executar qualquer código, tenho que reconectar o cabo USB para fazê -lo funcionar Aian. Alguma ideia?

Foi útil?

Solução 2

Ok, a razão de tudo isso é apenas o fato de eu usar um adaptador porque meu computador não possui uma porta LPT antiga. Copiei meu aplicativo para um computador antigo executando o Windows XP e tudo funciona bem.

Agora tenho que esperar que alguns outros anúncios do LPT2USB que comprei fazem o trabalho deles corretamente.

Editar 20.04.2010

Com outro adaptador LPT2USB, tudo funciona bem agora. Se alguém estiver interessado em todo o código que estou usando agora, entre em contato comigo ou comente aqui.

Outras dicas

Solução rápida passo a passo

Como o código não foi fornecido, eu o faço trabalhar com a ajuda dos links fornecidos e aqui está o 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);

Fontes:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top