Printing with zebra MZ220 from android - android

What i´m trying to do is to print a label with zebra mz220 using the information I have in my SQLite database, but I don´t know how to fill in the label using strings. Here is my code so far
case R.id.bprint:
new Thread(new Runnable() {
public void run() {
try {
// Instantiate connection for given Bluetooth® MAC Address.
ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection("00:22:58:3C:9F:0B");
// Initialize
Looper.prepare();
// Open the connection - physical connection is established here.
thePrinterConn.open();
// here it should fill in the label but it doesn't =(
thePrinterConn.write("Input (ENCODING.LBL):! 0 200 200 200 1 ENCODING UTF-8 TEXT 0 20 30 r/n/ PRINTr/n/".getBytes());
//Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here
e.printStackTrace();
}
}
}).start();
break;
case R.id.spb:
startActivity(new Intent(this, Barcode.class));
break;
please help, thanks

The code looks appropriate, but unfortunately your comment "I don't know how to fill in the label using strings" isn't very clear. What is the outcome when you run your code? What are you trying to achieve? As an example, you can send the following data to the printer in your 'write' call. Assuming your printer is in CPCL mode:
thePrinterConn.write("! 0 200 200 210 1\r\nTEXT 4 0 30 30 Hello world\r\nFORM\r\nPRINT\r\n" .getBytes());
CPCL manual section 2 page 3: support.zebra.com/cpws/docs/comtec/PROMAN-CPCL_RevY.pdf

Related

Zebra ZQ620 ^FO command don't move origin

I have some issues printing labels on a Zebra ZQ620 bluetooth, with ZPL commands. Some of them don't work as expected. I use Android and Zebra's SDK with the next code:
new Thread(new Runnable() {
public void run() {
try {
// Instantiate insecure connection for given Bluetooth MAC Address.
Connection thePrinterConn = new BluetoothConnectionInsecure(getcBTMAC());
// Initialize
Looper.prepare();
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints a label.
String zplData = "^XA" +
"^FO530,10^ATR,36,20^FD Sheet: 9999/LA Lbl.: 1 S/R: 3333^FS" +
"^FO470,10^ADR,72,30^FD" + " 123456. THIS IS A BIG TEXT" + "^FS"+
"^FO420,10^ATR,36,20^FD" + " BarCode: 2139EJC 28/09/2021 Total: 6 u.^FS\n"+
"^FO300,300^B3R,N,100,N,N^FD2139EJC^FS" +
"^XZ";
// Send the data to printer as a byte array.
thePrinterConn.write(zplData.getBytes());
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
// Close the insecure connection to release resources.
thePrinterConn.close();
Looper.myLooper().quit();
} catch (Exception e) {
// Handle communications error here.
Log.e("PRINT", e.getMessage());
e.printStackTrace();
}
}
}).start();
The label is printed partly good (rotate as required and text displayed), but the next commands don't work as expected:
^FOx,y: I can move origin in "x" axis (width) but "y" axis value is ignored and all text is printed from bottom to top. It doesn't matter what value I put in "y" value. It always is ignored. Then, I can move origin of text y this axis.
^B3R,N,100,N,N: It ignores all parameters I put. I changed height to 100, 200, 300, include or not line text. It is always ignored. I have added a ^BY3 command previously, and It changes width but not height. Barcode is printed with only 2mm of heigth and I can't set it bigger.
^ADR,72,30: All fonts command ignore scales.
Printer configuration is:
Commands: ZPL (I have tried ZPL II, hibryd_xml_zpl, epl_zpl).
Labels: 76mm (width) x 101 mm (height) . Label separator: black mark line and space between labels.

IOIO locks up during SPI transaction with MCP-4131 any solutions?

I am attempting to using an IOIO-RTG board to control a MCP-4131 digital potentiometer via SPI. I'm new to SPI but I believe that I've followed the SPI example. I'm able to set a resistance apparently but IOIO remains stuck afterwards. The only way to continue is to disconnect and reconnect to the board. I note that the SPI example expects a MISO and MOSI pin whereas the pot has a combined SDI/SDO pin. Is this difference the source of my issue?
IOIO RTG
IOIOLIb 0326
Application Firmware 0506
Bootloader Firmware 0402
Hardware Sprk 0020
I've tried to implement asynchronous transactions to not wait for a response but the end result is the same. I've called the highgear function from within the Looper class and outside with no change.
class Looper extends BaseIOIOLooper
{
SpiMaster spi;
protected void setup() throws ConnectionLostException
{
int clkPin = 39;//left side = 36
int misoPin = 38;//left side = 33, not expecting output
int mosiPin = 38;//left side = 35
spi = ioio_.openSpiMaster(new DigitalInput.Spec(misoPin,
Mode.PULL_UP), new DigitalOutput.Spec(mosiPin),
new DigitalOutput.Spec(clkPin),
new DigitalOutput.Spec[] { new DigitalOutput.Spec(40), new DigitalOutput.Spec(37), },
new SpiMaster.Config(Rate.RATE_125k, true, true));
}
public void highgear()
{
byte[] request = new byte[] {0,0,0,0,0,5,5,5};
byte[] response = new byte[4];
try {
SpiMaster.Result result = spi.writeReadAsync(0, request, request.length, 7, response, 0);
} catch (ConnectionLostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The expected outcome is that the MCP with give the desired resistance and the IOIO will be available for further commanding. There are no errors as the board just freezes in it's set configuration.
The shared SDO/SDI pin of the MCP-4131 should not be the problem.
From the datasheet on page 31: "The 8-lead Single Potentiometer devices are pin limited so the SDO pin is multiplexed with the SDI pin (SDI/SDO pin). After the Address/Command (first 6-bits) are received, If a valid Read command has been requested, the SDO pin starts driving the requested read data onto the SDI/SDO pin."
As long as you only write to the digital potentiometer everything should be the same as with other SPI devices.
Have you tried your code with other SPI devices or even without connecting one?

How to read available text (not ending with new line) from TCP socket using BufferedReader

I'm communication with a server through a tcp socket connection, i'm able to read lines that ends with \n fine, however when the line is not terminated (ends in \n) i'm not able to read it. I tried the following but it didn't work and caused my app to freeze at startup:
private Socket socket;
private BufferedReader input;
public boolean isConnected;
#Override
public void onCreate()
{
try
{
socket = new Socket ("server.ip.add.ress", 23456);
input = new BufferedReader (new InputStreamReader (socket.getInputStream());
handshake();
isConnected = true;
}
catch // Handle IOException and UnknownHostException
}
// custom runnable to read availabe input from the server
private class MyRunnable implements Runnable
{
private volativle String value;
public String getValue()
{
return value;
}
#Override
public void run()
{
int count;
char[] buffer = new char[10]; // expected message 'username: '
try
{
count = input.read (buffer, 0, 10);
if (count > 0) value = new String (buffer);
}
catch // IOException
}
}
// when connection is established with server expect 'username: ' from
// the server and send the user name back to it
public void handshake()
{
MyRunnable runnable = new MyRunnable();
try
{
Thread thread = new Thread (runnable);
thread.start();
thread.join();
String greeting = runnable.getValue();
if (greeting.equals ("username: ")) // Send username back
}
catch // InterruptedException
}
why is it hanging? and how can i read a non terminated line?
Edit:
To clarify: The server sends the greeting message username: immediately after the connection is established with a client, the client wait for the greeting and send back it's username when received (that's what handshake() does), if no handshake the client disconnects otherwise it start listening for incoming messages. Because i need to know if handshake is complete before starting the listener i had to use Thread.join().
The problem: Thanks for the comments and answers below, it turned out that BufferedReader.read() blocks the thread and waits until something is sent from the server and if nothing is being sent it causes the app to hang, Therefor there's no way to find out if the line has ended.
The solution: In my specific situation i just wanted to know if a specific message is sent "username: " so i used read (buffer, 0, 10) to read exactly 10 characters (the length of "username: "), and because it blocks if nothing is sent i used Thread.join (1000) which waits only one second and then if nothing received i disconnect the client.
Why is it hanging?
This is what it is suppose to be. It will block the thread if no data is available to read. This is also why you want to put it in a background thread.
Can it not just return if nothing is available?
What you are looking for is ready(), which will tell you whether there is available data or not.
Indicates whether this reader is ready to be read without blocking.
Returns
true if this reader will not block when read is called, false if unknown or blocking will occur.
But you should be very careful when using this function. Because networking is a lot about timing. The fact that you don't have any data to read at this second doesn't necessary mean that it won't be any data in the next second.
So a better design of the server should be more or less as the following:
If the username is found, return the username
If the username is not found, return an error message to let the client side know that the username is not found
There's no need for the thread. Your goal is to wait until you've read what you've been waiting for. Why not just let read() perform the wait for you?
What you're struggling with is the classic problem of TCP communication: "when do I know that I've got everything the server sent?"
In your case, you're expecting to read bytes until the collection of bytes ends with "username: ". So, change your algorithm to perform 1 byte reads (filling a buffer as you go) until that buffer ends with "username: ".
You can make a more complicated algorithm -- which would be more efficient -- that would attempt to read multiple bytes at a time and append them to a buffer -- performing your check each time. But either strategy is logically equivalent.
I also recommend just using the InputStreamReader. It has various read() methods. I am a bit suspicious about the BufferedInputReader, especially when dealing with data that isn't newline terminated. I'm probably just paranoid. I've just never used it when writing TCP client/server programs, so I'm not sure.

MIT App Inventor and Arduino Ethernet LED Error 1109 URL not valid

I am beginning the basis for a home automation system using an arduino with ethernet and a phone with software that I programmed with MIT App inventor. I've been playing with code from a tutorial and got my LED to turn on and off fine from a local computer using the internet by using a browser and navigating the url 192.168.1.10/$1
/* thrown together by Randy Sarafan
Allows you to turn on and off an LED by entering different urls.
To turn it on:
http://192.168.1.10/$1
To turn it off:
http://192.168.1.10/$2
Based almost entirely upon Web Server by Tom Igoe and David Mellis
Edit history:
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
boolean incoming = 0;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
//IPAddress ip(191,168,1,15); //<<< ENTER YOUR IP ADDRESS HERE!!! i commented this out and did it on the router side
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup()
{
pinMode(8, OUTPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac);
server.begin();
Serial.begin(9600);
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
//reads URL string from $ to first blank space
if(incoming && c == ' '){
incoming = 0;
}
if(c == '$'){
incoming = 1;
}
//Checks for the URL string $1 or $2
if(incoming == 1){
Serial.println(c);
if(c == '1'){
Serial.println("ON");
digitalWrite(8, HIGH);
}
if(c == '2'){
Serial.println("OFF");
digitalWrite(8, LOW);
}
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
The problem I'm having is on the mobile side. I created an app with MIT app inventor that should toggle the IP, but instead gives me:
error 1109: The specified URL is not valid: 192.168.1.10/$1
Im quite perplexed. I know that this URL IS VALID because I've connected to it before. Is there a way to override this or otherwise fix it?
Here's the MIT app inventor AIA source: http://www.filedropper.com/internetled
You SHOULD specify the IP address in the code itself. And change "Ethernet.begin(mac);" to "Ethernet.begin(mac, ip);".
You should also try to open the web page in the browser to check if it works, before trying it out with App Inventor.
In AppInventor error "error 1109: The specified URL is not valid: 192.168.1.10/$1" you may have forgotten to put the "http: //" in your address.
Example, if you are saving the address in a global variable or tinyDB, you should put http://192.168.1.10/$1
This would be your address to connect the led.

USB bulkTransfer errors after sending nulls

I am using FTDI usb-serial dongle to send binary data from my android device,Asus transformer, to a industrial controller.
I am using code from http://android.serverbox.ch/?p=370&cpage=1 (thanks Manuel Di Cerbo )
It works fine as long as I do not send nulls, but after a null is sent the rest of the data is corrupted.
Questions.
Should it work? and/or
Can anyone confirm what I am seeing?
public byte[] dataframe = new byte[256];
......
......
for(;;){//this is the main loop for transferring
conn.bulkTransfer(epOUT, dataframe, 10, 0);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(mStop){
mStopped = true;
return;
}
l("sent "+dataframe[0]+","+dataframe[1]+","+dataframe[2]);
}
}
This thread resends the data periodically, currently 10 bytes but it will be more, while the main app updates some of the values. Normally most of the bytes would be zero, with some changing data. At the moment my experimental code runs OK if I use non zero values. The log shows the values are still correct at that point so I am assuming the problem is after the bulkTransfer.

Categories

Resources