I'm developing a class that writes data to a Star TSP100iiiBi Bluetooth printer. This particular printer only supports raster graphics, so I have to send everything to it as a raster image, no plain ASCII text is supported.
The problem I'm having is that the last data I send to the printer never seems to actually make it to the printer. I create a 24k byte array (23,258 bytes using B4XBytesBuilder .ToArray function, actually) that I write directly to the Asyncstream via Stream.Write(EntireByteArray) and it throws no errors. After writing, I fire an event in the class that the Activity responds to by issuing a command that causes the class to close the connection (stream.SendAllAndClose followed by Serial.Disconnect). The last few bytes of what I'm sending never seem to get to the printer.
I verified the byte values I'm sending with Star Micronics developer integration to be correct, and Star's PassPRNT Android app produces output correctly when started from an intent. If I send multiple copies of the bitmap to the printer, all images print correctly except for the last one, which never completes.
As a work-around I wrote a routine that outputs the full array in 2k chunks (i.e. send 2K bytes, followed by a sleep for 100 milliseconds, then send the next 2k, etc. for the size of the output array) followed by a 250 millisecond pause before firing the event that leads to disconnecting. At no time during this have I observed the stream's OutputQueueSize change from zero.
This workaround seems to produce the desired output fairly reliably. Although, if I increase the buffer to 3K I start seeing the same behavior I got from just writing the entire byte array at once. A long way to go to get to my question, I know, but I wanted to give a framework for why I'm asking the question.
Is there some type of setting for the output buffer size that I can work with, or is there a reason I never see the OutputQueueSize value change from zero or am I failing at some other fundamental level?
-- Edit -- Rubber ducking this as I typed this post lead me to trying adding a sleep(5000) to my Stream.Write(EntireByteArray) method. This seems to solve the problem as well. I guess at this point it seems to indicate perhaps the .SendAllAndClose is not actually sending all before closing the stream? Or, perhaps it's still going when I close the serial connection? Either way, I would have to have some means of knowing if all the bytes have been sent as it seems I cannot use the OutputQueueSize value.
The problem I'm having is that the last data I send to the printer never seems to actually make it to the printer. I create a 24k byte array (23,258 bytes using B4XBytesBuilder .ToArray function, actually) that I write directly to the Asyncstream via Stream.Write(EntireByteArray) and it throws no errors. After writing, I fire an event in the class that the Activity responds to by issuing a command that causes the class to close the connection (stream.SendAllAndClose followed by Serial.Disconnect). The last few bytes of what I'm sending never seem to get to the printer.
I verified the byte values I'm sending with Star Micronics developer integration to be correct, and Star's PassPRNT Android app produces output correctly when started from an intent. If I send multiple copies of the bitmap to the printer, all images print correctly except for the last one, which never completes.
As a work-around I wrote a routine that outputs the full array in 2k chunks (i.e. send 2K bytes, followed by a sleep for 100 milliseconds, then send the next 2k, etc. for the size of the output array) followed by a 250 millisecond pause before firing the event that leads to disconnecting. At no time during this have I observed the stream's OutputQueueSize change from zero.
This workaround seems to produce the desired output fairly reliably. Although, if I increase the buffer to 3K I start seeing the same behavior I got from just writing the entire byte array at once. A long way to go to get to my question, I know, but I wanted to give a framework for why I'm asking the question.
Is there some type of setting for the output buffer size that I can work with, or is there a reason I never see the OutputQueueSize value change from zero or am I failing at some other fundamental level?
-- Edit -- Rubber ducking this as I typed this post lead me to trying adding a sleep(5000) to my Stream.Write(EntireByteArray) method. This seems to solve the problem as well. I guess at this point it seems to indicate perhaps the .SendAllAndClose is not actually sending all before closing the stream? Or, perhaps it's still going when I close the serial connection? Either way, I would have to have some means of knowing if all the bytes have been sent as it seems I cannot use the OutputQueueSize value.
Last edited: