简体   繁体   English

Xamarin:如何从Android设备打印到点矩阵打印机?

[英]Xamarin: How to print from Android device to dot matrix printer?

I have the following hardware: 我有以下硬件:

  • Dot matrix printer: LX 300-II. 点矩阵打印机:LX 300-II。
  • Android device with Android version 4.2.2. Android版本为4.2.2的Android设备。
  • Connection type: USB, Wi-FI, Bluetooth. 连接类型:USB,Wi-FI,蓝牙。

I have an Android Activity that create an invoice, so I want to print some text on printer. 我有一个可创建发票的Android活动,因此我想在打印机上打印一些文本。 But I can't make it work. 但是我不能使它工作。

I tried using USBManager with no success. 我尝试使用USBManager失败。 Is there any way to achieve this? 有什么办法可以做到这一点?

I found a solution. 我找到了解决方案。 It's the only way to make it works. 这是使其运作的唯一方法。 USBManager cannot access to printer because driver is missing. USBManager无法访问打印机,因为缺少驱动程序。 So I tried with a WI-FI option. 因此,我尝试了WI-FI选项。

You need some extra hardware here: 您在这里需要一些额外的硬件:

  • Print Server (In this case I tried TL-WPS510-U). 打印服务器(在这种情况下,我尝试使用TL-WPS510-U)。
  • Android Device 4.0.3 or superior Android设备4.0.3或更高版本
  • Epson LX-300+II 爱普生LX-300 + II

First of all you must configure your Print Server. 首先,您必须配置打印服务器。 I did it following this tutorial . 我按照本教程进行操作

Now I have the Print Server pointing to my device (acting as Server) and waiting for any printer job. 现在,我将打印服务器指向我的设备(充当服务器)并等待任何打印机作业。

What I did on my Xamarin.Android project? 我在Xamarin.Android项目中做了什么?
Created an instance of a Socket class pointing to the Printer Server ;) 创建一个指向打印机服务器的Socket类的实例;)

No Epson command needed (Ok some of them). 无需爱普生命令(可以使用其中的一些命令)。 The code is the following: 代码如下:

private async Task SendCommand()
{
    await Task.Run (() => 
    {
        try
        {
            Socket sock = new Socket("199.1.1.50", 9100);
            PrintWriter oStream = new PrintWriter (sock.OutputStream, true);;
            OutputStreamWriter outWriter;

            oStream.Write(0x1B); //T1
            oStream.Write(0x40); //T2 Start Printer
            oStream.Print("This is great!!! á é í ó ú ü Ü Ñ ñ");

            oStream.Write(0x0C); //release paper
            oStream.Write(0x1B); //t1
            oStream.Write(0x40); //t2 Finish Printer

           oStream.Flush ();
           oStream.Close ();
           oStream.Dispose();
           sock.Close ();
           sock.Dispose();

        }
        catch(SocketException ex)
        {
            string m = ex.Message;
            RunOnUiThread(() => Toast.MakeText(this, m, ToastLength.Long).Show());
        }
        catch(Exception ex)
        {
            Toast.MakeText(this, ex.ToString(), ToastLength.Long).Show();
        }

    });
}

Now is working very fine. 现在工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM