2012-08-08 1 views
2

J'ai quelques problèmes pour imprimer sur notre imprimante de codes à barres. Les codes fonctionnent bien, mais je ne peux pas en imprimer un autre immédiatement mais je dois attendre quelques secondes. Y at-il quelque chose qui ne va pas avec mes codes?C# Socket Imprimer parfois pas de réponse?

merci à tout le monde à l'avance.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 


namespace PrintToZebra 
{ 
class Program 
{ 
    public static void Main(string[] args) 
    { 
     Console.WriteLine("start printing to: 10.160.2.254:9100"); 
     string text = "^XA" + 
      "^FO335,22,^CI0^A0,14,14^FR^FDConta^FS" + 
      "^FO368,22,^CI0^A0,14,14^FR^FDins^FS" + 
      "^PQ1" + 
      "^XZ"; 
     printToIP("10.160.2.254", 9100, text); 


    } 


    public static void printToIP(string ipAddress, int printerPort, string content) 
    { 
     try 
     { 
      EndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), printerPort); 
      //EndPoint ep = new IPEndPoint(parseIP(ipAddress),printerPort); 
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      sock.Connect(ep); 
      NetworkStream ns = new NetworkStream(sock); 
      byte[] toSend = Encoding.ASCII.GetBytes(content); 
      ns.Write(toSend, 0, toSend.Length); 
      sock.Close(); 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex); 
     } 

    } 

} 

}

+3

Avez-vous besoin de fermer la connexion entre chaque étiquette imprimée? Certaines imprimantes (incertaines sur les zèbres du haut de ma tête) effectuent une procédure de réinitialisation entre chaque connexion, ce qui peut retarder la reconnexion. –

Répondre

0

Merci Joachim. J'ai modifié mon code en utilisant BeginWrite au lieu de Write et le problème a été résolu.

public class PrintToZebra 
    { 
     public string printerIP { get; set; } 
     public int printerPort { get; set; } 
     public string myZPL { get; set; } 
     private EndPoint ep { get; set; } 
     private Socket sock { get; set; } 
     private NetworkStream ns { get; set; } 
     //private AsyncCallback callbackWrite; 

    public PrintToZebra() 
    { 
     printerIP = ""; 
     printerPort = 0; 
     myZPL = ""; 
    } 


    public PrintToZebra(string anIP, int aPort, string aZPL) 
    { 
     printerIP = anIP; 
     printerPort = aPort; 
     myZPL = aZPL; 
    } 


    public void printToIP() 
    { 
     ep = new IPEndPoint(IPAddress.Parse(printerIP), printerPort); 
     sock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 

     try 
     { 
      sock.Connect(ep); 
      ns = new NetworkStream(sock); 
      byte[] toSend = Encoding.ASCII.GetBytes(myZPL); 
      ns.BeginWrite(toSend, 0, toSend.Length, OnWriteComplete, null); 
      ns.Flush(); 
     } 
     catch (Exception ex) 
     { 
      //Console.WriteLine(ex.ToString()); 
      sock.Shutdown(SocketShutdown.Both); 
      sock.Close(); 
     } 

    } 

    private void OnWriteComplete(IAsyncResult ar) 
    { 
     NetworkStream thisNS = ns; 
     thisNS.EndWrite(ar); 
     sock.Shutdown(SocketShutdown.Both); 
     sock.Close(); 
    } 

} 
Questions connexes