Android Question Print on Epson TM-T20II

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I wanted to ask if anyone could print on an Epson TM-T20II POS printer.

Is it easy to send printing from a mobile application in B4A?

Is there an example code?

I need to know if I can implement this solution to a client.

Thank you very much for your reply.
 

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Thank you f0raster0

I have already been able to print from a POS printer started with the smarthpone via Bluetooth

The Epson TM-T20II is a POS printer connected by USB to a PC with Windows Operating System.

It would be something like a LAN network printing from the Smartphone
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Does the T20II support a Network-connection? I guess no.

I would like to suggest to use Epson SDK for this Printer (i remember there is one)

Edit to add: As the Printer is connected to the PC you need to PRINT on the PC. Run a B4J app on the pc which is able to get commands from a android device and print from the b4j app.
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
@DonManfred

The Epson TM-T20II printer does not support direct connection to the Smartphone.

I have seen in the forum here: https://www.b4x.com/android/forum/threads/print-via-the-local-network.54432/

B4X:
Sub Process_Globals
    Dim TMT20II As Socket 'Network library
    Dim AStreams As AsyncStreams 'RandomAccessFile library
End Sub

Sub Activity_Create(FirstTime As Boolean)
    TMT20II.Initialize("TMT20II")
    TMT20II.Connect("192.168.0.100", 9100, 0) '100=Printer address, 9100=Port number (please look up port number)
End Sub

Sub TMT20II_Connected (Successful As Boolean)
    Log(Successful)
    If Successful Then
        AStreams.Initialize(TMT20II.InputStream, TMT20II.OutputStream, "AStreams")
        CallSub(Null, SendToPrinter)
    End If
End Sub

Sub SendToPrinter
    Dim SendMsg As String

    SendMsg = Chr(27) & Chr(61) & Chr(1) 'Select EPSON printer
    AStreams.Write(SendMsg.GetBytes("UTF8"))

    SendMsg = "Hello, this is a test print" 'Send this line to the EPSON printer
    AStreams.Write(SendMsg.GetBytes("UTF8"))

    SendMsg = Chr(27) & Chr(100) & Chr(2) 'Line feed
    AStreams.Write(SendMsg.GetBytes("UTF8"))

    SendMsg = Chr(27) & Chr(109) 'Partial cut receipt paper
    AStreams.Write(SendMsg.GetBytes("UTF8"))
End Sub

According to the code I see that it is possible to connect via network from the mobile device to the network printer, I want to know if that is possible.

I thank you very much for your help, since I don't want to promise my client something I can't do.
 
Upvote 0

sakissoft

Member
Licensed User
Longtime User
On android ver:4,5,6,7 and 8 it worked normally but in android smartphone with android 10 has a problem: "An error has occurred in sub: main_socket1_connected (java line: 20239) android.os.NetworkOnMainThreadException"

i have b4a v10.7 and compiled with android 29 and jdk1.8.0_92 with network library v1.52 (without errors)

Βelow is my code for print network epson TM-T20iii:

B4X:
Sub Process_Globals
   Dim Socket1 As Socket 'for connect printer 1
End sub

Sub Socket1_Connected (Successful As Boolean)
 
    If Successful = False Then
        Socket1.close
        Dim res As Int = DialogResponse.CANCEL
           Do While res <> DialogResponse.POSITIVE
             res = Msgbox2("problem printer Νο1", "Warning", "ΟΚ", "", "",LoadBitmap(File.DirAssets,"iconfinder_sad_emoticon_emoticons_emoji_emote_1_2993623.png"))
          Loop
     
        Return
    End If
 
    Dim tr As TextWriter
    tr.Initialize2(Socket1.OutputStream,"ISO8859-7")
    tr.Write(Chr(27) & Chr(116) & Chr(15))    '15 = greek code page "ISO8859-7"
    tr.Write(textForDirectPrint & CRLF & CRLF & CRLF)
    tr.write(Chr(27) & Chr(109))   'cut paper
    tr.Flush
    tr.Close
    Socket1.Close
End sub



Sub testPrint1_Click

    deviceIP1.text="192.168.1.150"
    Dim val1,val2 As Int
    Dim str As String

    val1=9100 'port 9100
    val2=1000 'internal timeout
    str=deviceIP1.text 'IP
     textForDirectPrint="Printer_1: Καλημέρα " & CRLF
     Socket1.Initialize("Socket1")
     Socket1.Connect(str, val1, val2)
 
Εnd sub

Please HELP :(
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
It's a recent(ish) Android limitation. As the error says you can no longer do networky thingies on the main thread. You need to use AsyncStreams. A forum search should turn up some examples

You should not use Msgbox2 any longer, it has been deprecated for quite a long time now. use MsgboxAsync or MsgboxAsync2.

Google. Goalposts. Moved. 😠
 
Last edited:
Upvote 0
Top