Commit 641ec581 authored by pengguangpu's avatar pengguangpu

英泰打印驱动尘埃落定

parent ed9f6ba9
...@@ -137,7 +137,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -137,7 +137,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
break; break;
case R.id.btnRelease: case R.id.btnRelease:
if (PrinterManager.getInstance().isConnected()) { if (PrinterManager.getInstance().isConnected()) {
if (PrinterManager.getInstance().getPrinter().release() == true) { if (PrinterManager.getInstance().release() == true) {
tvResult.setText("销毁成功"); tvResult.setText("销毁成功");
} else { } else {
tvResult.setText("没初始化过"); tvResult.setText("没初始化过");
......
...@@ -8,6 +8,11 @@ import android.graphics.Bitmap; ...@@ -8,6 +8,11 @@ import android.graphics.Bitmap;
*/ */
public class BasePrinter implements IPrinter { public class BasePrinter implements IPrinter {
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
final static int MAX_IMG_WIDTH_80MM = 570;
/** /**
* 初始化调用者的引用 * 初始化调用者的引用
*/ */
......
...@@ -41,11 +41,6 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -41,11 +41,6 @@ public class ChuangjiePrinter extends BasePrinter {
*/ */
int textSize = -1; int textSize = -1;
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
final static int MAX_IMG_WIDTH_80MM = 570;
@Override @Override
public boolean init(Context context) { public boolean init(Context context) {
this.context = context; this.context = context;
...@@ -71,9 +66,7 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -71,9 +66,7 @@ public class ChuangjiePrinter extends BasePrinter {
} }
ret = setAlign(align); ret = setAlign(align);
ret = printString(content); ret = printString(content);
if (isFeed) { //创捷机器打印完后会强制走纸一行,这里不再判断是否走纸标志
ret = LF();
}
return ret; return ret;
} }
...@@ -89,12 +82,17 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -89,12 +82,17 @@ public class ChuangjiePrinter extends BasePrinter {
@Override @Override
public boolean release() { public boolean release() {
super.release(); if (mUsbDriver != null && mUsbDev != null) {
if (null == mUsbDev) {
if (mUsbDriver.isConnected()) { if (mUsbDriver.isConnected()) {
mUsbDriver.disconnet(); if (null != mUsbDev) {
mUsbDriver.disconnet(mUsbDev);
}
} }
mUsbDev = null;
mUsbDriver = null;
context = null;
} }
D.i(TAG, "创捷打印资源已关闭");
return true; return true;
} }
...@@ -319,9 +317,6 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -319,9 +317,6 @@ public class ChuangjiePrinter extends BasePrinter {
int ret = setAlign(align); int ret = setAlign(align);
//创捷机器打印条码只能居左、居中打印,并且内部会强制走纸 //创捷机器打印条码只能居左、居中打印,并且内部会强制走纸
ret = printBarCode(content); ret = printBarCode(content);
if (isFeed) {
ret = feedPaper();
}
return ret; return ret;
} }
......
...@@ -17,10 +17,11 @@ public class PrinterManager { ...@@ -17,10 +17,11 @@ public class PrinterManager {
enum Type { enum Type {
//这里添加扩展的驱动 //这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()), // TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
// TYPE_YINGTAI("yingtai", ChuangjiePrinter.class.getName()), // TYPE_SUNMI("shangmi", ChuangjiePrinter.class.getName()),
// TYPE_SUNMI("shangmi", ChuangjiePrinter.class.getName()), TYPE_YINGTAI("yingtai", YingtaiPrinter.class.getName()),
TYPE_SANGDA("sangda", SangdaPrinter.class.getName()), TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()),
TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()); TYPE_SANGDA("sangda", SangdaPrinter.class.getName());
/** /**
* 设备类型名称 * 设备类型名称
...@@ -96,6 +97,23 @@ public class PrinterManager { ...@@ -96,6 +97,23 @@ public class PrinterManager {
return false; return false;
} }
/**
* 销毁相关资源
*
* @return
*/
public boolean release() {
if (isConnected()) {
boolean result = printer.release();
if (result == false) {
return false;
}
printer = null;
return true;
}
return true;
}
/** /**
* 获取当前打印接口 * 获取当前打印接口
* *
......
...@@ -25,17 +25,13 @@ public class SangdaPrinter extends BasePrinter { ...@@ -25,17 +25,13 @@ public class SangdaPrinter extends BasePrinter {
final static String TAG = SangdaPrinter.class.getSimpleName(); final static String TAG = SangdaPrinter.class.getSimpleName();
public PrinterAPI mPrinter = PrinterAPI.getInstance(); public PrinterAPI mPrinter;
InterfaceAPI io = null; InterfaceAPI io = null;
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
final static int MAX_IMG_WIDTH_80MM = 570;
@Override @Override
public boolean init(Context context) { public boolean init(Context context) {
super.init(context); super.init(context);
mPrinter = PrinterAPI.getInstance();
if (isConnect()) { if (isConnect()) {
mPrinter.disconnect(); mPrinter.disconnect();
} }
......
package com.miya.print;
import android.content.Context;
import android.graphics.Bitmap;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BitmapUtils;
import com.miya.print.utils.D;
import com.miya.print.utils.PrintFormatUtils;
import com.szsicod.print.escpos.PrinterAPI;
import com.szsicod.print.io.InterfaceAPI;
import com.szsicod.print.io.USBAPI;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* 英泰打印机驱动
*/
public class YingtaiPrinter extends BasePrinter {
final static String TAG = YingtaiPrinter.class.getSimpleName();
private PrinterAPI mPrinter;
private InterfaceAPI io;
@Override
public boolean init(Context context) {
this.context = context;
mPrinter = PrinterAPI.getInstance();
io = new USBAPI(context);
if (!io.isOpen().booleanValue()) {
if (PrinterAPI.SUCCESS == mPrinter.connect(io)) {
D.i(TAG, "英泰打印机连接成功");
return true;
} else {
D.e(TAG, "英泰打印机连接失败");
return false;
}
}
return false;
}
@Override
public boolean release() {
if (mPrinter != null) {
mPrinter.disconnect();
io = null;
mPrinter = null;
context = null;
}
//为空也返回true
return true;
}
@Override
public int printText(int align, int size, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
try {
ret = mPrinter.printString(content, "GBK", isFeed);
return ret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int printBarcode(int align, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
try {
//通过printString传入打印指令实现条码的打印
ret = mPrinter.printString(PrintFormatUtils.getBarcodeCmd(content), "GBK", isFeed);
// ret = mPrinter.printBarCode(73, content.length(), content);
return ret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int printQrcode(int align, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
ret = mPrinter.printQRCode(content, 6, false);
if (isFeed) {
ret = feedPaper();
}
return changeStatus2Custom(ret);
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int printImage(int align, Bitmap bm, boolean isFeed) {
if (mPrinter != null && bm != null) {
int ret = setAlign(align);
try {
Bitmap actualBm = BitmapUtils.scaleBitmap(bm, MAX_IMG_WIDTH_80MM, 0);
if (bm.getWidth() > MAX_IMG_WIDTH_80MM) {
//如果超过阈值,则缩放至目标大小
ret = mPrinter.printRasterBitmap(actualBm);
} else {
ret = mPrinter.printRasterBitmap(bm);
}
if (actualBm != null) {
actualBm.recycle();
}
changeStatus2Custom(ret);
} catch (IOException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
//桑达打印图片时,本身就会强制走纸一行,这里不再判断是否再走纸
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int setAlign(int align) {
if (mPrinter != null) {
if (this.align != align &&
(align == 0 || align == 1 || align == 2)) {
int ret = mPrinter.setAlignMode(align);
changeStatus2Custom(ret);
this.align = align;
}
return PrinterStatusEnum.CODE_SUCCESS.getStatus();
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int feedPaper() {
if (mPrinter != null) {
int ret = mPrinter.printFeed();
return changeStatus2Custom(ret);
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int cutPaper() {
if (mPrinter != null) {
int ret = mPrinter.cutPaper(66, 0);
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int getPrinterStatus() {
if (mPrinter != null) {
//这个方法参数中的n只能取1或49
int ret = mPrinter.getPrinterStatus(1);
//做一个状态对应转换,转换为自定义标准的状态码类型
switch (ret) {
case 0:
//正常状态
return PrinterStatusEnum.CODE_SUCCESS.status;
case 12:
//缺纸
return PrinterStatusEnum.CODE_NO_PAPER.status;
case 3:
//纸将尽
return PrinterStatusEnum.CODE_FEW_PAPER.status;
}
return PrinterStatusEnum.CODE_UNKOWN.status;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
/**
* 将桑达的状态码翻译成PrinterStatusEnum中的标准状态码
* 注:内部会自动抛出不允许的异常状态
*
* @param status 桑达返回的状态码
* @return PrinterStatusEnum中的标准状态码
*/
private int changeStatus2Custom(int status) {
if (status == 0) {
//正常状态
return PrinterStatusEnum.CODE_SUCCESS.status;
}
if (status == -1) {
//数据传输错误,请检查连接并重新发送
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "数据传输错误,请检查连接并重新发送");
}
if ((status & 0x200) > 0) {
return PrinterStatusEnum.CODE_FEW_PAPER.status;
}
if ((status & 0x400) > 0 || (status & 0x08) > 0) {
//缺纸
throw new PrinterException(PrinterStatusEnum.CODE_NO_PAPER.status, PrinterStatusEnum.CODE_NO_PAPER.desc);
}
if ((status & 0x4) > 0) {
//脱机状态
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.status, "发生错误");
}
if ((status & 0x20) > 0) {
//盖板打开
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.getStatus(), "盖板打开");
}
if ((status & 0x1) > 0) {
//脱机
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.status, "脱机");
}
if ((status & 0x100) > 0) {
//机械错误
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "机械错误");
}
if ((status & 0x80) > 0) {
//不可恢复错误
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "不可恢复错误");
}
return PrinterStatusEnum.CODE_SUCCESS.status;
}
public PrinterAPI getPrinter() {
return mPrinter;
}
public boolean isConnect() {
return mPrinter.isConnect();
}
public static String getStatusDescribe(int getStatus) {
int status = getStatus;
if (status == -1) {
return "数据传输错误,请重新连接或者重新发送";
}
String strStatus = Integer.toBinaryString(getStatus);
while (strStatus.length() < 32) {
strStatus = "0" + strStatus;
}
StringBuilder builder = new StringBuilder();
StringBuffer descriptBuffer = new StringBuffer();
StringBuffer troubleBuffer = new StringBuffer();
//传感应状态
if ((strStatus.substring(4, 6).equals("11"))) {
descriptBuffer.append("少纸, ");//[1]
troubleBuffer.append("PaperFew|");
}
if ((strStatus.substring(1, 3).equals("11"))) {
descriptBuffer.append("缺纸, ");//[2]
troubleBuffer.append("OutOfPaper|");
}
//脱机状态
if ((strStatus.substring(17, 18).equals("1"))) {
descriptBuffer.append("发生错误, ");//[9]
troubleBuffer.append("happen error|");
}
if ((strStatus.substring(21, 22).equals("1"))) {
descriptBuffer.append("盖板打开, ");//[9]
troubleBuffer.append("box open|");
}
//打印机状态
if ((strStatus.substring(28, 29).equals("1"))) {
descriptBuffer.append("脱机, ");//[8]
troubleBuffer.append("Offline|");
}
if ((strStatus.substring(25, 26).equals("1"))) {
descriptBuffer.append("正在feed, ");//[8]
troubleBuffer.append("feeding|");
}
//错误状态
if ((strStatus.substring(13, 14).equals("1"))) {
descriptBuffer.append("机械错误, ");//[10]
troubleBuffer.append("MachineError|");
}
if ((strStatus.substring(9, 10).equals("1"))) {
descriptBuffer.append("可自动恢复错误, ");//[11]
troubleBuffer.append("CorrectingError|");
}
if ((strStatus.substring(10, 11).equals("1"))) {
descriptBuffer.append("不可恢复错误, ");//[12]
troubleBuffer.append("NotCorrectError|");
}
String descript = descriptBuffer.toString().trim();
if (!descript.isEmpty()) {
descript = descript.substring(0, descript.length() - 1);
} else {
descript = "正常";
}
return descript;
}
}
\ No newline at end of file
package com.miya.weighing; package com.miya.weighing;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.util.Log; import android.util.Log;
...@@ -30,6 +31,10 @@ public class ChuangjieWeighing extends BaseWeighing { ...@@ -30,6 +31,10 @@ public class ChuangjieWeighing extends BaseWeighing {
@Override @Override
public boolean init(Context context) { public boolean init(Context context) {
//根据序列号判断是否是创捷机器
if (!Build.SERIAL.startsWith("CCL")) {
return false;
}
serialPortFunction = SerialPortFunction.getInstance(); serialPortFunction = SerialPortFunction.getInstance();
serialPortFunction.setContext(context); serialPortFunction.setContext(context);
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment