我如何与And​​roid明星TSP100LAN票据打印机的工作? [英] How do I make star TSP100LAN receipt printer work with Android?

查看:360
本文介绍了我如何与And​​roid明星TSP100LAN票据打印机的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了一个API 这里

然而,当我运行AndroidSample测试程序我得到的错误。

当I $上的获取打印机状态按钮,我得到的打印机处于联机状态P $ PSS。此按钮看起来。

不过:<​​/ P>

  • pressing读取从打印机数据产量失败,无法读入固件的名称。

  • pressing打印收据会导致应用程序挂起3秒。然后什么都没有。

  • pressing打印经过座收据产量不是打印成功或大挂(有时是强制关闭)。在任何情况下,没有打印。

解决方案

我花了永远弄清楚如何得到它的工作。我给你什么,我可以让你开始。我是相当新的机器人,可以随意地指出的事情我做不正确。它偶尔错误地移动所述内容进行打印,从而切断顶部和底部增加了大量的空间。如果任何人都可以明白这一点我会更AP preciated。

在这里你去:

需要从APK这些文件:不要相信我修改他们无论如何: 1. RasterDocument.java 2. StarBitmap.java

主要印刷方式:

 公共静态无效PrintReceipt(上下文的背景下,RelativeLayout的布局){

        字符串PORTNAME =TCP:10.1.250.20;您的打印机// IP地址
        字符串portSettings =;

//必须测量布局为​​它正确打印,否则尺寸为零
        layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams(),宽度,View.MeasureSpec.EXACTLY)
                View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams()高度,View.MeasureSpec.EXACTLY));
        layout.layout(0,0,layout.getMeasuredWidth(),layout.getMeasuredHeight());

        点阵位图= Bitmap.createBitmap(layout.getWidth(),layout.getHeight(),Bitmap.Config.RGB_565);
        帆布油画=新的Canvas(位);
        canvas.drawColor(Color.WHITE);
        layout.draw(画布);


        INT =了maxWidth 576; //默认TSP100收到宽度
        RasterDocument rasterDoc =新RasterDocument(RasterDocument.RasSpeed​​.Full,RasterDocument.RasPageEndMode.FeedAndFullCut,RasterDocument.RasPageEndMode.FeedAndFullCut,RasterDocument.RasTopMargin.Standard,0,0,0);
        StarBitmap starbitmap =新StarBitmap(位图,假了maxWidth);

        StarIOPort端口= NULL;
        尝试
        {
            / *
                   使用StarIOPort3.1.jar(支持USB端口)
                   Android操作系统版本:上2.2
               * /
            端口= StarIOPort.getPort(PORTNAME,portSettings,10000,背景);
            / *
                   使用StarIOPort.jar
                   Android操作系统版本:2.1下
                   端口= StarIOPort.getPort(PORTNAME,portSettings,10000);
               * /

            尝试
            {
                视频下载(500);
            }
            赶上(InterruptedException异常E){}

            byte []的命令= rasterDoc.BeginDocumentCommandData();
            port.writePort(命令,0,command.length);
            命令= starbitmap.getImageRasterDataForPrinting();
            port.writePort(命令,0,command.length);
            命令= rasterDoc.EndDocumentCommandData();
            port.writePort(命令,0,command.length);

            尝试
            {
                视频下载(1000);
            }
            赶上(InterruptedException异常E){}
        }
        赶上(StarIOPortException E)
        {
            ShowAlertMessage(背景下,失败,无法连接到打印机。+ e.getMessage());
        }
        最后
        {
            如果(端口!= NULL)
            {
                尝试 {
                    StarIOPort.releasePort(端口);
                }赶上(StarIOPortException E){}
            }
        }

    }

    私有静态无效ShowAlertMessage(最终上下文的背景下,最终的字符串alertTitle,最终字符串消息){
        尝试 {
            ((活动)范围内).runOnUiThread(新的Runnable(){

                @覆盖
                公共无效的run(){
                    AlertDialog.Builder对话框=新AlertDialog.Builder(上下文);
                    dialog.setNegativeButton(OK,NULL);
                    AlertDialog警报= dialog.create();
                    alert.setTitle(alertTitle);
                    alert.setMessage(消息);
                    alert.show();
                }});
        }赶上(最终例外五){
            Log.e(PrinterFunctions.class.getName(),e.​​getMessage());
        }
    }
 

THEN: 在另一种方法,发送PrintReceipt一个RelativeLayout的。坐落在类的构造函数当前上下文。

 上下文currentContext;
    RelativeLayout的RelativeLayout的;
    私人最终诠释receiptWidth = 576;
    私人字样字体= Typeface.DEFAULT;
    私人诠释normalFontSize = 23;
    私人诠释largeFontSize = 28;

公共SETU preceiptClass(上下文的背景下){
    currentContext =背景;
}
公共无效SETU preceipt(字符串客户名称){
       //创建收据布局
        RelativeLayout的=新RelativeLayout的(currentContext);
        RelativeLayout.LayoutParams PARAMS;
        PARAMS =新RelativeLayout.LayoutParams(receiptWidth,ViewGroup.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(PARAMS);
        relativeLayout.setId(R.id.ReceiptLayout);

//创建任何意见,你想在这里和它们添加到RelativeLayout的,使您的收据
relativeLayout.addView(whateverViewsYouCreate);

//最后,打印收据。
        新主题(新的Runnable(){

            @覆盖
            公共无效的run(){
                PrintReceipt(currentContext,RelativeLayout的);
            }
        })。开始();

}
 

此外,我是新来的android的东西,这可能不是最好的方式,但它是打印。什么酷你发现我很想听到他们的声音。

I have found an API here

However, when I run the AndroidSample test app I get errors.

When I press on the "Get Printer Status" button I get "Printer is online". This button works it seems.

However:

  • Pressing "Read data from printer" yields "Failure. could not read in the firmware name."

  • Pressing "Print Receipt" cause the application to hang for 3 seconds. Then nothing.

  • Pressing "Print Checked Block Receipt" yields either "printing succeeded" or a big hang (sometimes force close). In any case, nothing is printed.

解决方案

It took me forever to figure out how to get it to work. I'll give you what I can to get you started. I'm fairly new to android, so feel free to point out things I am be doing incorrectly. It does occasionally incorrectly print by moving the content up, thus cutting off the top and adds a lot space at the bottom. If anyone can figure that out I would be much appreciated.

Here you go:

Requires these files from the APK: Don't believe i modified them anyway: 1. RasterDocument.java 2. StarBitmap.java

Main printing method:

    public static void PrintReceipt(Context context, RelativeLayout layout){

        String portName = "tcp:10.1.250.20"; //ip address of your printer
        String portSettings = "";

//have to measure the layout for it to print correctly, otherwise sizes are zero
        layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().height, View.MeasureSpec.EXACTLY));
        layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

        Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        layout.draw(canvas);


        int maxWidth = 576; //default width of tsp100 receipt
        RasterDocument rasterDoc = new RasterDocument(RasterDocument.RasSpeed.Full, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasTopMargin.Standard, 0, 0, 0);
        StarBitmap starbitmap = new StarBitmap(bitmap, false, maxWidth);

        StarIOPort port = null;
        try
        {
            /*
                   using StarIOPort3.1.jar (support USB Port)
                   Android OS Version: upper 2.2
               */
            port = StarIOPort.getPort(portName, portSettings, 10000, context);
            /*
                   using StarIOPort.jar
                   Android OS Version: under 2.1
                   port = StarIOPort.getPort(portName, portSettings, 10000);
               */

            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e) {}

            byte[] command = rasterDoc.BeginDocumentCommandData();
            port.writePort(command, 0, command.length);
            command = starbitmap.getImageRasterDataForPrinting();
            port.writePort(command, 0, command.length);
            command = rasterDoc.EndDocumentCommandData();
            port.writePort(command, 0, command.length);

            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e) {}
        }
        catch (StarIOPortException e)
        {
            ShowAlertMessage(context, "Failure", "Failed to connect to printer. " + e.getMessage());
        }
        finally
        {
            if(port != null)
            {
                try {
                    StarIOPort.releasePort(port);
                } catch (StarIOPortException e) {}
            }
        }

    }

    private static void ShowAlertMessage(final Context context, final String alertTitle, final String message){
        try {
            ((Activity)context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                    dialog.setNegativeButton("Ok", null);
                    AlertDialog alert = dialog.create();
                    alert.setTitle(alertTitle);
                    alert.setMessage(message);
                    alert.show();
                }});
        } catch (final Exception e) {
            Log.e(PrinterFunctions.class.getName(), e.getMessage());
        }
    }

THEN: In another method, send PrintReceipt a relativelayout. Set the current context in the constructor of the class.

  Context currentContext;
    RelativeLayout relativeLayout;
    private final int receiptWidth = 576;
    private Typeface typeFace = Typeface.DEFAULT;
    private int normalFontSize = 23;
    private int largeFontSize = 28;

public SetupReceiptClass(Context context){
    currentContext = context;
}
public void SetupReceipt(String customerName){
       //Create layout for receipt
        relativeLayout = new RelativeLayout(currentContext);
        RelativeLayout.LayoutParams params;
        params = new RelativeLayout.LayoutParams(receiptWidth,     ViewGroup.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(params);
        relativeLayout.setId(R.id.ReceiptLayout);

//Create whatever views you want here and add them to the RelativeLayout to make up your receipt
relativeLayout.addView(whateverViewsYouCreate);

//Finally, Print the receipt.
        new Thread(new Runnable() {

            @Override
            public void run() {
                PrintReceipt(currentContext, relativeLayout);
            }
        }).start();

}

Again, I'm new to the android thing, and this may not be the best way, but it is printing. Anything cool you find out I'd love to hear them.

这篇关于我如何与And​​roid明星TSP100LAN票据打印机的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆