ESC/POS sent via TCPSocket

Hello, I’m just wondering if anyone’s had any luck sending data to a receipt printer via a TCPSocket?

I’m woking in IOS but I guess the same question would stand in a desktop app.

I’ve opened a connection with a Epson Receipt printer (TM-M30ii) from simulator to printer
across wifi, this looks OK but now I’m trying to send ESC/POS commands to it via the
TCPSocket.Write method like so…

TCPSocket1.Address = “”
TCPSocket1.Port = 443

// connect the socket

// while the socket isn’t connected
While Not TCPSocket1.IsConnected
// poll the socket to let it do its thing
// if an error occurs, the Error event will fire

// if we broke the loop because we’re connected
If TCPSocket1.IsConnected Then

Dim S as string
S = “Hello world”

//TCPSocket1.Write(“ESC “”@”" ““Hello world”” LF")

End If

// close the socket

I’m stepping through the code and it does the .write bit OK but the printer
is not responding in any way. My first guess is that I’m not sending the correct codes
in the correct way, but after much Googling I’m no closer to a result.

Many thanks.

Escape is CHR(27), LineFeed is CHR(10). Try

TCPSocket1.Write(chr(27) + "@Hello World" + chr(10))
1 Like

Thanks very much Tim but no still nothing. Did you put the @ symbol in there to initialise the printer ?

I just copied what you had in your commented out code, replacing ESC and LF with the appropiate values.

If your’e going to post code, please do this to make it more legible:

  1. add the code to your post
  2. select the code with your mouse
  3. click on </>

Please start writing async code.
Don’t do a loop with poll.

Have a screen open to track status between connecting, sending, waiting for ack and closing.

1 Like

Thanks everyone for your reply. I’ve managed to get it going now. Here’s some working code in case someone else tries to do the same thing. This code is working on an Epson TM-M30ii receipt printer on a local network over wifi.

Put a TCPSocket in your ‘Desktop’ or ‘IOS’ project, add button and add this code to the button.

TCPSocket1.Address = "" // your device IP
TCPSocket1.Port = 9100 

You can use the TCPSocket events to monitor progress.

Use another button to send the ESC/POS commands to the printer.

Dim s As String
Dim D as New Dictionary

D.Value("Esc") = String.ChrByte(&h1B)
D.Value("@") = String.ChrByte(&h1B) + String.ChrByte(&h40) // ESC @
D.Value("LF") = String.ChrByte(&h0A) // LF line feed
D.Value("Cut") = String.ChrByte(&h1B) + String.ChrByte(&h69) // cut paper
D.Value("bold") = String.ChrByte(&h1B) + String.ChrByte(&h45) + String.ChrByte(&h1) // n = 1
D.Value("Height2") = String.ChrByte(&h1D) + String.ChrByte(&h21) + String.ChrByte(&h1) //n = 1
D.Value("Width2") = String.ChrByte(&h1D) + String.ChrByte(&h21) + String.ChrByte(&hA) //n=10
D.Value("Print&Feed") = String.ChrByte(&h1B) + String.ChrByte(&h64) + String.ChrByte(&hA) // 5 lines

S = D.Value("@") + D.Value("Bold") + D.Value("Height2")  + D.Value("Width2") + "Hello World" + D.Value("Print&Feed")

// I'm finding I have to make two writes to get the printer to correctly feed the paper and then cut.
S = D.Value("@") + D.Value("Cut")
1 Like

Hello Barney,

Thanks a lot for the code, apparently i have a weird printer that talks same protocol, just on my side it says 80 Command Set but i guess it is same ESC/POS

Could you please clarify for me how do you set the 5 lines please i see in the code you mention &hA which in decimal is 10 if i’m not mistaking .

So far while printing your test code i got this

Height: 44mm
Top to writing: 20mm
Bottom to writing: 20mm

Now, how do you adjust the font size and the lines in this case ?

Can this print images ? i did managed to use the printer with their driver and i use it using the printerSetup way but so far your way is much better if i manage to print all properly

on the other code i did something like that .

If g <> Nil Then
  g.PenSize = 10
  g.DrawOval(50, 2, 100, 60)
  g.FontSize = 25
  g.Bold = True
  g.DrawText("SSTC", 67, 42)
  g.Bold = False
  g.FontSize = 11
  g.DrawText("Receipt", 5, 80)
  g.DrawText("3569", 140, 80)
  g.PenSize = 1
  g.DrawLine(5, 100, 190, 100)
  g.DrawText("Translation", 5, 120)
  g.DrawText("500", 140, 120)
  g.DrawText("Attestation", 5, 140)
  g.DrawText("200", 140, 140)
  g.DrawLine(5, 180, 190, 180)
  g.Bold = True
  g.DrawText("Total", 5, 220)
  g.DrawText("700", 140, 220)
  g.Bold = False
  g.DrawText("Signature : ", 5, 260)
  'g.DrawText(".  ", 5, 300)
  g.DrawText("Printed on  : " + d.SQLDateTime, 5, 320)
  g.DrawingColor = Color.Gray
  g.DrawText(".  ", 5, 340)
End If

Is there a way to get same result using your code ?

In my case the translation and attestation might change and the price as well and that has to fit in the receipt

Thanks a lot

Hi Aurelian,

I’m no expert at this I’m afraid as it was my first attempt to send commands to a receipt printer. I do plan to go ahead and send receipts just like you’re doing so a learning curve ahead.

I’m using Epson’s command set to put together data to send:

If you have a study of these, you’ll soon piece together what I’ve done and yes &hA
is 10 sorry, my bad.

Now, how do you adjust the font size and the lines in this case ?

ESC M looks like it will control font size.

ESC d worked for me:

D.Value("Print&Feed") = String.ChrByte(&h1B) + String.ChrByte(&h64) + String.ChrByte(&hA) // 10 ESC d (Epson 'Print Commands')

as did ESC J

D.Value("Feed") = String.ChrByte(&h1B) + String.ChrByte(&h4A) + String.ChrByte(&hC8) //  200 lines

You will be able to print nice receipts with images as I’ve seen many examples of this using the ESC/POS command set. It’s going to be a matter of figuring the codes out along the way and writing a module/class etc to make life easy for everyday use.


Apparently this no name printer it seems that it uses same protocol so in the end i guess that is the reason code worked. I will need to find a way to test those and then it should be ok