4

ネットワーク Zebra プリンタで印刷する必要があります。いくつかの理由から、winspool 印刷 ( http://support.microsoft.com/kb/154078 ) を使用できません。IP とポートのソケットを介して直接印刷する必要があります。ここに私の印刷方法があります:

System.Net.Sockets.TcpClient zebraClient = new System.Net.Sockets.TcpClient(); 
        try 
        { 
            zebraClient.SendTimeout = 5000; 
            zebraClient.Connect(IP, port);
        } 
        catch (Exception ex) 
        { 
            Utils.ShowError(ex); 
        } 
        if (zebraClient.Connected) 
        { 
            NetworkStream nStream; 
            nStream = zebraClient.GetStream(); 
            StreamWriter wStream; 
            using (nStream) 
            { 
                wStream = new StreamWriter(nStream); 
                using (wStream) 
                { 
                    wStream.Write(content); 
                    wStream.Flush(); 
                } 
            } 
            zebraClient.Close(); 
        } 

問題は、時々「ターゲットコンピュータが積極的に拒否したため、接続を作成できませんでした」という例外が発生することです。なぜそれが起こっているのかわかりません(おそらくプリンターバッファーがいっぱいです-もしそうなら、どうすれば両方の言語でそれを確認できますか?)。それで、誰かがこの問題を抱えているかどうか、どうすれば修正できるかを尋ねます。

4

4 に答える 4

0

これが私のVBコードです。

    Private Sub sendData(ByVal zpl As String)
    Dim ns As System.Net.Sockets.NetworkStream = Nothing
    Dim socket As System.Net.Sockets.Socket = Nothing
    Dim printerIP As Net.IPEndPoint = Nothing
    Dim toSend As Byte()

    Try
        If printerIP Is Nothing Then
            'set the IP address
            printerIP = New Net.IPEndPoint(IPAddress.Parse(IP_ADDRESS), 9100)
        End If

        'Create a TCP socket
        socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        'Connect to the printer based on the IP address
        socket.Connect(printerIP)
        'create a new network stream based on the socket connection
        ns = New NetworkStream(socket)

        'convert the zpl command to a byte array
        toSend = System.Text.Encoding.ASCII.GetBytes(zpl)

        'send the zpl byte array over the networkstream to the connected printer
        ns.Write(toSend, 0, toSend.Length)

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Cable Printer", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
    Finally
        'close the networkstream and then the socket
        If Not ns Is Nothing Then
            ns.Close()
        End If

        If Not socket Is Nothing Then
            socket.Close()
        End If
    End Try
End Sub

    Private Function createString() As String
    Dim command As String

    command = "^XA"
    command += "^LH20,25"

    If rdoSmall.Checked = True Then
        command += "^FO1,30^A0,N,25,25^FD"
    ElseIf rdoNormal.Checked = True Then
        command += "^FO1,30^A0,N,35,35^FD"
    Else
        command += "^FO1,30^A0,N,50,50^FD"
    End If

    command += txtInput.Text
    command += "^FS"
    command += "^XZ"

    Return command

End Function

これは、テキストをs4mプリンターに印刷するためだけのものです。

于 2012-07-09T22:27:13.007 に答える
0

ポート9100を試してください。ネットワーク上でプリンタのIPが表示されていることを確認してください。

于 2012-06-12T19:19:42.207 に答える
0

これがあなたに当てはまるかどうかはわかりませんが、asp classic を使用して同様の問題に遭遇しました。デフォルトのプリンターを変更せずにゼブラプリンターに直接印刷する必要があったため、解決策として、ソケットを使用してゼブラプリンターに接続する Java 実行可能ファイルを作成しました。Java 実行可能ファイルが、開いているソケットのストリームを介して Zpl の文字列をゼブラ プリンタに送信できるようになったら、Java 実行可能ファイルを実行するためのバッチ ファイルを作成しました。実行可能ファイルには asp ページからの文字列が必要だったので、ユーザー入力変数をバッチ ファイルに追加しました。これら 2 つのファイル (Java jar ファイルと .bat ファイル) を共有ドライブに配置し、ASP ページで ActiveX を使用して、未加工のバイトを文字列の形式でゼブラ プリンターに直接送信することができました。ご不明な点がございましたら、お気軽にお問い合わせください。 https://km.zebra.com/kb/index?page=content&id=SO7149&actp=RSS

于 2016-06-21T04:53:55.620 に答える
0

これは私のために働いた:

    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    .
    .
    .
    private void btnPrint_Click(object sender, EventArgs e) {
      try {
    string ipAddress = txtIPAddr.Text.ToString(); ; //ie: 10.0.0.91
    int port = int.Parse(txtPort.Text.ToString()); //ie: 9100

    System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
    client.Connect(ipAddress, port);
    StreamReader reader = new StreamReader(txtFilename.Text.ToString()); //ie: C:\\Apps\\test.txt
    StreamWriter writer = new StreamWriter(client.GetStream());
    string testFile = reader.ReadToEnd();
    reader.Close();
    writer.Write(testFile);
    writer.WriteLine("Hello World!");
    writer.Flush();
    writer.Close();
    client.Close();
  }
  catch (Exception ex) {
    MessageBox.Show(ex.Message, "Error");
  }
}
于 2020-05-02T09:34:03.207 に答える