_bulkOut.write method for sending streams of data chunk by chunk

I am sending streams of bytes via USB to printer. I am able to send small image data directly at one gon and it is successfully printing the image. But when my image is large, printer does not print and it goes in Catch error().

So I want to send stream of bytes using _bulkOut.write() method in a loop.

try {
     _bulkOut.write(hexBlob, function(ep, state, data, len) {
     PRINTER.log("Raster Graphics Printed");
     }.bindenv(this));
} catch(err) {
     PRINTER.err("Catch Error()");
}

Above is my code written.

You can’t do that; you need to wait for the bulkout write to complete, and in the callback queue the next chunk to send.

The system works asynchronously.

Hi Hugo,

When I am trying to send the next chunk of data after the bulkout write complete, the data is printing in the next page over and over again. I am adding my code please have a look at it.

After writing first chunk, in the cb(), I am calling a recursive function until all the data gets written. But as I said, the problem is that it is writing in the next page. Please help me in it, I am very close to printing the bigger image.

function recursiveFun(n) {
    if(n==0) {
       return;
    }

    if(_bytes_written < starRaster.len()){
         starRaster.seek(_bytes_written, 'b');
    }

    local newBlob = starRaster.readblob(9);
    if(n>0) {
         try {
             _bulkOut.write(newBlob, function(ep, state, data, len) {
             PRINTER.log("New Chunk Endpoint : " + ep);
             PRINTER.log("New Chunk State : " + state);
             PRINTER.log("New Chunk Data : " + data);
             PRINTER.log("New Chunk Length : " + len);
             _bytes_written = _bytes_written + BUFFER_SIZE;
             recursiveFun(n-1);
             }.bindenv(this));
         } catch (err) {
             PRINTER.err("Error in New chunk writing");
         }
     }
}

Your code seems overly complex.

I’m also confused why you’re writing tiny amounts at once. You appear to be sending only 9 bytes per transfer; the USB stack will split much bigger transfers into the correct bulk packet size. I’ve no idea what “BUFFER_SIZE” is, but if its not 9 then you’re going to be having problems.

This doesn’t have error handling, but should work fine. It issues writes of max 4kB, which the imp will generally split up into 64 packets of 64 bytes on the wire, but the callback only fires when all 4kB has been sent.

dataToWrite <- blob(x); // this contains the data you want to write
const MAXWRITE = 4096;

function writeChunk(onComplete) {
  local remaining = dataToWrite.len() - dataToWrite.tell();
  local chunk = (remaining > MAXWRITE)?MAXWRITE:remaining;

  // Anything left to send?
  if (chunk > 0) {
    _bulkOut.write(dataToWrite.readblob(chunk), function(ep, state, data, len) {
      writeChunk(onComplete);
    });
  } else {
    // Fire complete callback
    onComplete();
  }
}

// Start the send; ensure pointer is at the start of the blob
dataToWrite.seek(0);
writeChunk(function() { server.log("transfer complete"); });