Question

Je suis en train d'imprimer des données brutes ascii à une imprimante thermique. Je le fais en utilisant cet exemple de code: http://support.microsoft.com/kb/322091 mais mon imprimante imprime toujours un seul caractère et ce jusqu'à ce que je n'appuyez sur le bouton d'alimentation de forme. Si j'imprime quelque chose avec bloc-notes de l'imprimante fera un saut de automatiquement mais sans imprimer le texte.

L'imprimante est connectée via USB sur un adaptateur lpt2usb et Windows 7 utilise le. "Générique -> générique / texte" pilote

Tout le monde sait ce qui se passe mal? Comment est-il possible d'imprimer quelques mots et faire des aliments de forme? Y a-t-il des caractères de contrôle que je dois envoyer? Et si oui: Comment puis-je les envoyer

?

Modifier 14.04.2010 21:51

Mon code (C #) ressemble à ceci:

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

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

Ce code renvoie un « T » après avoir appuyé sur le bouton d'alimentation de forme (Ce litte bouton noir ici: swissmania.ch/images/935-151.jpg - désolé, pas assez réputation pour deux hyperliens)

Modifier 15.04.2010 16:56

J'utilise maintenant la forme de code ici: c-sharpcorner.com/UploadFile/johnodonell/PrintingDirectlytothePrinter11222005001207AM/PrintingDirectlytothePrinter.aspx

Je l'ai modifié un peu que je peux utiliser le code suivant:

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

L'exécution de ce code a la même Effekt comme appuyer sur le bouton d'alimentation de forme, il fonctionne très bien!

Mais code comme cela ne fonctionne toujours pas:

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

affichera seulement un « B », mais je me attendre « BC » et après l'exécution de tout code je dois reconnecter le câble USB pour le faire fonctionner planifions. Toutes les idées?

Était-ce utile?

La solution 2

D'accord, la raison de tout cela est juste le fait que j'utiliser un adaptateur parce que mon ordinateur ne dispose d'un vieux port LPT. Je copiais ma demande à un ancien ordinateur exécutant Windows XP et tout fonctionne très bien.

Maintenant, je dois espérer que d'autres adaters de lpt2usb j'ai acheté faire correctement leur travail.

Modifier 20.04.2010

Avec un autre adaptateur lpt2usb tout fonctionne bien maintenant. Si quelqu'un est interessé dans tout le code que je me sers maintenant, s'il vous plaît me contacter ou commentaire ici.

Autres conseils

étape rapide par étape solution

Parce que le code n'a pas été fourni, je le faire fonctionner avec l'aide des liens fournis, et le code ici:

code

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

Utilisation

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);

Sources:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top