开发者

How do I make star TSP100LAN receipt printer work with Android?

开发者 https://www.devze.com 2023-03-18 14:49 出处:网络
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.

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.


Reading through the documentation for the iOS and Android StarIO SDKs I found that the STAR TSP100LAN requires it to be in 'raster mode'. Unfortunately the samples provided with both the iPhone and Android SDKs are for printing in 'line mode' only. While this isn't an answer, hopefully it will help point you in the right direction :)

I am going to try and contact Star themselves and see if I can get some sample code from them directly, wish me luck, I will report back here with any response I get!


Did you add your Activity on AndroidManifest? and add the permission that your app need to allow them?


Star added a new Android SDK package with more functionality than the old version mentioned in this thread (which I believe was V1.0). The new one is available here

As mentioned above, the TSP100LAN needs to receive Raster commands since it's natively a graphical printer. The latest SDK package has an updated sample app which lets you test a lot of different printer functionality, including some Raster commands.

The SDK package that's available for download at the time of writing this answer is V2.3. The manual (README_StarIO_POSPrinter_Android_SDK.pdf) states that the TSP100LAN can use the Open Cash Drawer, Get Status, Print Raster Graphical Text (decorate text/send it to the printer), and Image File Printing (coupons) functions.

Star also has a programming manual which contains Raster commands detailing other functionality. See section 3.4

0

精彩评论

暂无评论...
验证码 换一张
取 消