Android Question astreams.write to print

mario53

Member
Licensed User
Taking a code posted by MarcoRome:
https://www.b4x.com/android/forum/threads/usb-ticket-printer-error-when-open-9600-1.61745/

And modified for my needs as follows:

B4X:
If usb1.UsbPresent(1) = usb1.USB_NONE Then ' Ver_2.4
   Msgbox("No USB device or accessory detected!", "Error") 
   Return
End If
Log("Checking permission 1")
If (usb1.HasPermission(1)) Then ' Ver_2.4
   Msgbox(usb1.DeviceInfo(1),"Info")
   'This is very important - ( VendorId, ProductId )
   usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x1504, 0x6)

   Dim dev As Int
   dev = usb1.Open(19200, 1)                   ' 
   If dev <> usb1.USB_NONE Then
      Msgbox("Connected successfully! 1","Info")
      astreams1.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "astreams1")
      'This is important
      usb1.SetParameters(19200, usb1.DATABITS_8,usb1.STOPBITS_1, usb1.PARITY_NONE)
      'Here if you want codce that call print
      astreams1.Write("abcde".GetBytes("UTF8"))
      astreams1.Close
      usb1.Close
   Else
     Log("Error opening USB port 1")
   End If
 Else
   usb1.RequestPermission(1) ' Ver_2.4
 End If

It works perfectly up to usb1.SetParameters, when it does: astreams1.Write ( "abcde" .GetBytes ( "UTF8")) There is nothing in the printer
Do you have any idea?
thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Moved to the questions forum.

Don't close AsyncStreams immediately after you call Write.

Try it like this:
B4X:
astream1.Write(...)
Sleep(500)
astreams1.Close
usb1.Close

Maybe your printer expects an end of line character after each string?

Try to add:
B4X:
astream1.Write(...)
astream1.Write(Array As Byte(13, 10))
 
Upvote 0

mario53

Member
Licensed User
Ok thanks i could print in the thermal printer "abcde";
Now how can you decide size and other control fonts, new page ....
 
Upvote 0
Top