Can you advise us how to optimize the SendAndWaitForResponse(Byte[], Int32, Int32, String) method?

We are finding that setting the printer response time and initial timeout
is causing operations to be very slow. With the below code -

thePrinterConn.SendAndWaitForResponse(Encoding.UTF8.GetBytes(zplData),
960,650, "null")
^XA
^RFW,H^FD30361F8A7803428000000001^FS^FN1
^RFR,H^FS^HV1^FS
^XZ

We need to ensure that we first encode each tag & then read the data on
each tag, but with the send and wait for response command, and the values
960 and 650 - the process is very slow. However when we decrease the
values, then the printer ends up skipping tags & we do not have control on
the tags.
We are using the UsbConnection Class - not the driver printer class.

Can you advise us how to optimize the SendAndWaitForResponse(Byte[], Int32, Int32, String) method?

Or an alternative and faster way to send commands and get a response from the printer?

following is code for ref:

Connection myConnection = new Zebra.Sdk.Comm.DriverPrinterConnection("ZDesigner R110Xi4 300 dpi");

for (int i = 1; i <= 200; i++)
{
byte[] zpldata = Encoding.UTF8.GetBytes("^XA^RS8,,0,1,N,,,2^RR3^SZ2^JMA^MCY^PMN^PW590~JSN^JZY^LH0,0^LRN^XZ~DGR:SSGFX000.GRF,315,7,:Z64:eJw9zjsOgkAQBuB/s8ZgQkJLZ0Nia0lh8BqWVthaKVZwAi/jBSax8BqjXmDBhoKw7oyPbb7sv/NYAHaEnMSTsPYs1Jde6DHAnnNGER7cdpij9mx90tSeEn9rnPRdSbB30s6WlROHEFXF+8BQuXAz40qwAVb4wy/c/0sw5L92Hfal1dA8SWYaXQujnwj7Gy2ZQyngPhx7v3ApupiyHOgmVEbAI0YZCxvh1Y9lvMVhSdmE0KWURYwuonLGWpkGprRrWWcahm4nvAE3iW/6:8F12^XA^RS8,,0,1,N,,,2^FT375,113^CI0^A0N,25,17^FD42^FS^FT64,137^A0N,25,24^FD1002582661003^FS^FT64,177^A0N,25,16^FDBFCA-4209-M1836-CA-LU-PU-PH^FS^FO120,192^BY3^BEN,49,Y,N^FD8905011129868^FS^FT64,97^A0N,25,19^FDBUFFALO^FS^FT64,57^A0N,25,26^FDTAN^FS^FO457,26^XGR:SSGFX000.GRF,1,1^FS^RFW,H^FD30361F84CC0CAE8000000" + i + "^FS^FN1^RFR,H^FS^HV1^FS^XZ");
myConnection.Open();
byte[] myreturneddata = myConnection.SendAndWaitForResponse(zpldata, 710, 350, null);// 500, 200, null);
String str = System.Text.Encoding.Default.GetString(myreturneddata);
String[] row;
row = new String[] { i.ToString(), str };
dataGridView1.Rows.Add(row);
myConnection.Close();
}

also i have another query as
When we start printing, for some reason there is a random string that appears as the response that we get from the printer as so "JJL179464". The code we use is:

thePrinterConn.Open()
'---loop starts here
tid = thePrinterConn.SendAndWaitForResponse(Encoding.UTF8.GetBytes(zplcmd), 850, 500, "\r")
1.fetch reponse
2. if reponse is blank or -,1 then stop
3. add value in grid
'---loop ends here
zplcmd= ^RFW,H^FD30361FAF545CA6C0000003ED^FS^FN1
^RFR,H^FS^HV1^FS

Ideally we should get the value that is read by the printer, however, we do not. We get "JJL179464" first, and then in the next read we get the previous value that was read by the printer. As below:

1 30361FAF545CA6C0000003E9(value we want to encode) "JJL179464"(1st response)
2 30361FAF545CA6C0000003EA +,030361FAF545CA6C0000003E9(this response should have come 1st)
3 30361FAF545CA6C0000003EB +,030361FAF545CA6C0000003EA
4 30361FAF545CA6C0000003EC +,030361FAF545CA6C0000003EB
5 30361FAF545CA6C0000003ED +,030361FAF545CA6C0000003EC
================================================================================
- we have implemented the clear buffer, and our responses are coming properly in line
- however our speed is still quite slow in comparison to bartender, is there any other method for us to optimize this? we are only encoding 96 bits of data

- it takes about 1.4 seconds per tag, with bartender it takes less than a second per tag
is there any other method for us to optimize this?

Steven Si
To clear the read buffer,

To clear the read buffer, just simply make a call to thePrinterConn.Read(), which will read whatever content out of the read buffer. This way, you can ensure a clean start.

About the loop, how much time does it take to get the response back for each tag? Please be aware that the response time is also determined by the tags themselves, because the read/write operations take different amount of time depending on the type of tags. Some tags respond faster and some slower.

Vote: 
Vote up!
Vote down!

Points: 1

You voted ‘up’


priya upkare
Hi Steven ,

Hi Steven ,

Thank you for response, also can help me in below queries:

1.how do we clear read buffer ?is there any command?
2.how do we optimize in loop to get faster process?

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steven Si
Hi Priya,

Hi Priya,

This misalignment is probably because the so-called *random string* - "JJL179464". I am not sure what it is and why it is there. It might be there as part of the response from a prior operation. Because this *random string" is there in the read buffer before you call this SendAndWaitForResponse(Encoding.UTF8.GetBytes(zplcmd), 850, 500, "\r") API, it returns this *random string* because it is ended with "\r". The actual response that you are intended to read is still in the read buffer, which is read on the second call to SendAndWaitForResponse() API, and so on. We end up getting this misalignment. To avoid this situation, you can try to empty the read buffer by reading out all the data before calling the SendAndWaitForResponse() API. Hope this helps.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steven Si
There are a few things to

There are a few things to keep in mind. First of all, the RFID tag read/write plus label printing is generally slower than a regular ZPL label printing. Secondly, the USB connection generally is slower than TCP connection. The sum of the two timeouts in SendAndWaitForResponse() API will be the max time to wait before the API returns. If we can shorten either one of them, we will shorten the total time to wait. One thing noticed in your post is that "null" is passed to the API call for the string terminator parameter. If we know the response string from the printer is ended with a specific string, we can pass that specific string as the terminator to the SendAndWaitForResponse() API call, so that the API call will return immediately after receiving that specific string. If we don't know the specific ending string, we can always use "\r" as the terminator because all response string will have it at the end. This way we can shorten the second timeout.

Another way to improve this is to use myConnection.write() and myConnection.read() directly, which bypasses the inherent delays and timeouts in SendAndWaitForResponse() API. Hope this helps.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


priya upkare
Thank you for the reply

Thank you for the reply Steven,
By using : myConnection.write() and myConnection.read() directly, sometimes the response from the printer is skipped. To combat this, we used the Thread.Sleep(3000). However in this case the operation became too slow.

We will add "/r" and change the connection to TCP, thank you

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


priya upkare
also i have another query as

When we start printing, for some reason there is a random string that appears as the response that we get from the printer as so "JJL179464". The code we use is:

thePrinterConn.Open()
'---loop starts here
tid = thePrinterConn.SendAndWaitForResponse(Encoding.UTF8.GetBytes(zplcmd), 850, 500, "\r")
1.fetch reponse
2. if reponse is blank or -,1 then stop
3. add value in grid
'---loop ends here
zplcmd= ^RFW,H^FD30361FAF545CA6C0000003ED^FS^FN1
^RFR,H^FS^HV1^FS

Ideally we should get the value that is read by the printer, however, we do not. We get "JJL179464" first, and then in the next read we get the previous value that was read by the printer. As below:

1 30361FAF545CA6C0000003E9(value we want to encode) "JJL179464"(1st response)
2 30361FAF545CA6C0000003EA +,030361FAF545CA6C0000003E9(this response should have come 1st)
3 30361FAF545CA6C0000003EB +,030361FAF545CA6C0000003EA
4 30361FAF545CA6C0000003EC +,030361FAF545CA6C0000003EB
5 30361FAF545CA6C0000003ED +,030361FAF545CA6C0000003EC

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Topic locked