B4J Question How can I get my b4j to communicate with an USB printer

davepamn

Active Member
Licensed User
Longtime User
I need to send a zebra file to an usb Zebra ZT230 printer
 
Last edited:

davepamn

Active Member
Licensed User
Longtime User
B4X:
Dim InputFile As InputStream=File.OpenInput(File.DirAssets, "1Researc.zpl")



    If InputFile.BytesAvailable>0 Then

        Dim buffer() As Byte

        buffer=Bit.InputStreamToBytes(InputFile)

        PrinterService.printer_AStream.Write(buffer)

          Log("Send Research zpl data to printer")

    EndIf

    InputFile.Close


I want to be able to create an asychronous stream to the ZT230 printer in b4j and send a file to it. I was able to do this in B4A using a bluetooth data stream to the printer

In b4a, I have a printer service running in the background. I create a printer serial connection with the printer. I then read and write to the serial connection using an asynchronous stream.

I need to do a similar process in b4j. How do I create the serial communication to the printer using a socket?
 
Last edited:
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
If the printer driver creates a virtual com port then you can use jSerial for this.

I connected an usb cable between my pc and the t230 printer. How can I test if it has a virtual com port

In B4A, there is a usb library that allows me to select a usb device. I need the same capability in B4j
 
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
B4X:
Sub btnPrint_Action

    sp.Open(cmbPort.Value)

sp.SetParams(9600,8,1,0)

  if stream.isInitialized=False then
    astream.Initialize(sp.getInputStream,sp.getOutputStream,"AStream")
  end if

    lblStatus.Text = "Status: Printing"

    SendTextToPrinter

lblStatus.text="Status: Printed"

End Sub


Sub SendTextToPrinter()

    Dim InputFile As InputStream=File.OpenInput(File.DirAssets, "230Res_M.zpl")



    If InputFile.BytesAvailable>0 Then

        Dim buffer() As Byte

        buffer=Bit.InputStreamToBytes(InputFile)

        astream.Write(buffer)

          Log("Send zpl data to printer")

    End If



    Dim sXML As String

    Dim sField1 As String ="999999"


       sXML="<?xml version=" & Chr(34) & "1.0" & Chr(34) & " standalone=" & Chr(34) & "no" & Chr(34) & "?>"& Chr(13) & Chr(10)
       sXML=sXML & "<!DOCTYPE labels SYSTEM " & Chr(34) & "Label.dtd" & Chr(34) & ">"& Chr(13) & Chr(10)
       sXML=sXML & "<labels _FORMAT=" & Chr(34) & "E:230RES_M.ZPL" & Chr(34) & " _QUANTITY=" & Chr(34) & "1" & Chr(34)
       sXML=sXML & " _PRINTERNAME=" & Chr(34) & "Printer 1" & Chr(34) & " _JOBNAME=" & Chr(34) & "LBL101" & Chr(34) & ">"& Chr(13) & Chr(10)
       sXML=sXML & "<label>"& Chr(13) & Chr(10)
       sXML=sXML & "<variable name=" & Chr(34) & "Field1" & Chr(34) & ">" & sField1 & "</variable>"& Chr(13) & Chr(10)
       sXML=sXML & "</label>"& Chr(13) & Chr(10)
       sXML=sXML & "</labels>"& Chr(13) & Chr(10)

        Dim buffer2() As Byte    =sXML.getbytes("UTF8")

                astream.Write(buffer2)

            Log("Send xml data to printer")

End Sub
Sub AStream_NewData (Buffer() As Byte)

    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

    LogMessage("You", s)

EndSub

Sub AStream_NewStream(Dir As String, FileName As String)

    Log("new stream" & Dir & " filename=" & FileName)

EndSub

Sub LogMessage(From As String, Msg As String)

    txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF

    txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length)

EndSub

Sub AStream_Error

    Log("Error: " & LastException)

    astream.Close

    AStream_Terminated

EndSub

Sub AStream_Terminated

    Log("Connection is broken.")

    lblStatus.Text = "Status: Close"

end sub

Nothing happens

How can I test that the serial port is connect to the Zebra ZT230 printer. I am getting no connection errors using com2

Com2
Bits Per Second=9600
Data Bits = 8
Parity=None
Stop Bits=1
Flow Control=None

What value is Parity=none? is that 0

The Communication Port has the c:\windows\system32\drivers\serial.sys
c:\windows\system32\drivers\serenum.sys

I am running the code on win 7 virtual server
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
ow can I test that the serial port is connect to the Zebra ZT230 printer. I am getting no connection errors using com2
You can only verify that it is connected if the printer is sending any data.

I recommend you to use a non-VM for serial ports. It will be surprising if it will work.
 
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
I used Bartender free trial and it connected too usb005 and printed out an label.

I want to do the same thing using b4j
 
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
I was able to install the b4j jar code on the same machine that bartender runs on. No com port displayed. This is a parallels win 7 box running on an iMac. Do I need a computer that is a physical box?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…