TSC TX600 从 android 打印?
TSC TX600 print from android?
我想使用 TSC -TX600 型号(通过 USB)从 android 设备打印 Text/barcode 标签。
从 TSC 网站上,我得到了以下源代码。因此,我创建了一个新的 android 项目并添加了 libs/*.jar 文件作为依赖项。然后我在 mainActivity.java.
复制了下面的内容
Link: https://www.tscprinters.com/EN/support/support_download/TX200_Series
当我执行此操作时,Android(代码)正在与打印机通信。 (显示屏显示正在打印)但打印机没有输出任何内容。你能告诉我我在哪里搞砸了吗?我还需要发送命令详细信息(在互联网上找不到)。
PS:如果我点击打印机中的诊断选项,状态打印正确。因此,纸张或打印机没有问题。
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import java.util.HashMap;
import java.util.Iterator;
import com.example.tscdll.TSCUSBActivity;
public class MainActivity extends Activity {
TSCUSBActivity TscUSB = new TSCUSBActivity();
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static UsbManager mUsbManager;
private static PendingIntent mPermissionIntent;
private static boolean hasPermissionToCommunicate = false;
private Button test;
private TextView tv1;
private static UsbDevice device;
IntentFilter filterAttached_and_Detached = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
// Catches intent indicating if the user grants permission to use the USB device
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
hasPermissionToCommunicate = true;
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
UsbAccessory[] accessoryList = mUsbManager.getAccessoryList();
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Log.d("Detect ", deviceList.size()+" USB device(s) found");
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
{
device = deviceIterator.next();
if(device.getVendorId() == 4611)
{
//Toast.makeText(MainActivity.this, device.toString(), 0).show();
break;
}
}
//-----------start-----------
PendingIntent mPermissionIntent;
mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT);
mUsbManager.requestPermission(device, mPermissionIntent);
tv1 = (TextView) findViewById(R.id.textView1);
test = (Button) findViewById(R.id.button1);
test.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(mUsbManager.hasPermission(device))
{
TscUSB.openport(mUsbManager,device);
//String status = TscUSB.printerstatus(300);
TscUSB.sendcommand("SIZE 3,1\r\n");
TscUSB.sendcommand("GAP 0,0\r\n");
TscUSB.sendcommand("CLS\r\n");
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"TscUSB.sendcommand("PRINT 1\r\n");
TscUSB.closeport(3000);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
您在此处发布的代码中似乎存在语法错误。
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"TscUSB.sendcommand("PRINT 1\r\n");
应该是这样的:
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"); // <-- previous command ends here
TscUSB.sendcommand("PRINT 1, 1\r\n"); // <-- Add missing argument here
更好的是,您可以简单地使用这个命令:
TscUSB.printlabel(1, 1); // <-- Ready made method on TSCUSBActivity class
此外,根据 TSPL2 编程手册,打印命令采用 2 个强制参数。 (Link 这里:TSPL2 Programming Manual)
PRINT Command Documentation Screenshot
我想使用 TSC -TX600 型号(通过 USB)从 android 设备打印 Text/barcode 标签。
从 TSC 网站上,我得到了以下源代码。因此,我创建了一个新的 android 项目并添加了 libs/*.jar 文件作为依赖项。然后我在 mainActivity.java.
复制了下面的内容Link: https://www.tscprinters.com/EN/support/support_download/TX200_Series
当我执行此操作时,Android(代码)正在与打印机通信。 (显示屏显示正在打印)但打印机没有输出任何内容。你能告诉我我在哪里搞砸了吗?我还需要发送命令详细信息(在互联网上找不到)。
PS:如果我点击打印机中的诊断选项,状态打印正确。因此,纸张或打印机没有问题。
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import java.util.HashMap;
import java.util.Iterator;
import com.example.tscdll.TSCUSBActivity;
public class MainActivity extends Activity {
TSCUSBActivity TscUSB = new TSCUSBActivity();
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static UsbManager mUsbManager;
private static PendingIntent mPermissionIntent;
private static boolean hasPermissionToCommunicate = false;
private Button test;
private TextView tv1;
private static UsbDevice device;
IntentFilter filterAttached_and_Detached = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
// Catches intent indicating if the user grants permission to use the USB device
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
hasPermissionToCommunicate = true;
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
UsbAccessory[] accessoryList = mUsbManager.getAccessoryList();
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Log.d("Detect ", deviceList.size()+" USB device(s) found");
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
{
device = deviceIterator.next();
if(device.getVendorId() == 4611)
{
//Toast.makeText(MainActivity.this, device.toString(), 0).show();
break;
}
}
//-----------start-----------
PendingIntent mPermissionIntent;
mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT);
mUsbManager.requestPermission(device, mPermissionIntent);
tv1 = (TextView) findViewById(R.id.textView1);
test = (Button) findViewById(R.id.button1);
test.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(mUsbManager.hasPermission(device))
{
TscUSB.openport(mUsbManager,device);
//String status = TscUSB.printerstatus(300);
TscUSB.sendcommand("SIZE 3,1\r\n");
TscUSB.sendcommand("GAP 0,0\r\n");
TscUSB.sendcommand("CLS\r\n");
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"TscUSB.sendcommand("PRINT 1\r\n");
TscUSB.closeport(3000);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
您在此处发布的代码中似乎存在语法错误。
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"TscUSB.sendcommand("PRINT 1\r\n");
应该是这样的:
TscUSB.sendcommand("TEXT 100,100,\"3\",0,1,1,\"123456\"\r\n"); // <-- previous command ends here
TscUSB.sendcommand("PRINT 1, 1\r\n"); // <-- Add missing argument here
更好的是,您可以简单地使用这个命令:
TscUSB.printlabel(1, 1); // <-- Ready made method on TSCUSBActivity class
此外,根据 TSPL2 编程手册,打印命令采用 2 个强制参数。 (Link 这里:TSPL2 Programming Manual)
PRINT Command Documentation Screenshot