IOS socket connection small ticket printer

At the bottom of the demo link
connect the printer to print a small ticket recently in the development of the function of iOS, to find a lot of information, relevant information itself is small, but also all is c#, Java. The project is written in swift (OC thought almost), is now summarized as follows.

Ready to work: Xcode7.1, swift2.0, GP80106 WiFi (according to the printer printer Gpilot is different, some of the instructions may be different, but basically the same, are used in the ESC/POS directive)

Premise: in the same LAN, set up the printer, set up host, port

1. Socket connect printer

Use CocoaAsyncSocket, WiFi to connect the printer. The specific connection method is not the main content of this article, can refer to the use of CocoaAsyncSocket


This is the OC library, so you include the AsyncSocket.h in the bridge file, PrijectName-Bridging-Header.h

//ViewController.swift import AsyncSocke var asyncSocket:AsyncSocket? / /... / / assumption has been successfully connected

Two. Before writing, let’s see how socket sends instructions:

AsyncSocket.writeData (data:, NSData, withTimeout:, NSTimeInterval, tag:, Int)

It writes all of the instruction data into a data object and then sends it to the connected device (printer). First, a NSData object is created to store instructions

Let, sendData = NSMutableData (capacity: 0), asyncSocket.writeData (sendData, withTimeout:, -1, tag:, 0)

Finally, the instructions are sent, so here, the key is to build the sendData object

Start by creating a generic method that writes each instruction to sendData (if you don’t know what’s going on now)

Func addBytesCommand (command:UnsafePointer, length:Int) {self.sendData.appendBytes (command, length:, length)}

Three. Give a few examples of how to send instructions

According to the programming manual, “horizontal and vertical units of movement” are used in a number of spacing settings, and then the solution is first addressed

1., GS, P, x, y, horizontal and vertical movement units


Sixteen hexadecimal code 1D Fifty X Y
Decimal code Twenty-nine Eighty X Y

[range] 0 = x = 2550 = y = 255,
[description] – horizontal moving units respectively approximate set to 25.4/ x mm (1/ x inch) vertical moving unit is set to 25.4/ y mm (1/ y.). When x and y are 0, X and y are set as defaults, with the default value of x=200, y=200.

Let’s first explain, GS, P is ASCII code, the following is the corresponding sixteen decimal and decimal, here use sixteen decimal

Func printDotDistance (x:UInt8, y:UInt8) {var char:[UInt8] = [0x1D 0x50] char.append (x) char.append (y) / / general data created in step 2 write method addBytesCommand (char, length: char.count)}

Sometimes the direct use of parameters in an instruction will make you talking, when we will provide the interface to others when used, they may not know what is the meaning of X, y. So it is necessary to set the parameter into everyone understand the meaning of (also see other methods), changes are as follows

Set the horizontal and vertical moving unit / * * - parameter horizontal: - parameter vertical: mobile unit transverse longitudinal mobile unit printDotDistance (w w:Float / func, h:Float) {var char = [0x1D 0x50] char.append (Int (25.4/w) char.append (Int) (25.4/h) (addBytesCommand) char, length: char.count)}

W, I hope h is set around or move up and down unit distance, according to the mm. 0 = x = 255,0 = y = 255, and the mobile unit is set to 25.4/ x mm approximation “, the method of W, h is a range, not out of bounds, or instructions will become invalid, the default settings

Here’s another one

2.GS L nL nH sets the left distance


Sixteen hexadecimal code 1D 4C NL NH
Decimal code Twenty-nine Seventy-six NL NH

[range] 0 = nL = 255,0 = nH = 255,
[description] – nL and nH –
set the left; the left margin is set to [(nL + nH * * 256) horizontal moving units “.
[default] nL = 0, nH = 0

I used the horizontal moving unit I just set up. I will call the following method to set the horizontal and vertical moving units to 0.1mm

PrintDotDistance (w: 0.1, h: 0.1)

And instructions, if the nL and nH are directly set to parameters, the user is not readable, so the arguments need to be converted. It is easy to see that nL is the modulus (remainder), and nH is the quotient

Set the left - / * * margin: parameter mm * / func printLeftMargin unit margins (margin:CGFloat) {var char:[UInt8] = [0x1D 0x4C] char.append (UInt8 (margin%256) char.append (UInt8) (margin/256) (addBytesCommand) char, length: char.count)}

3. text print

Add text string text: parameter / * * - * / func printAddText (text:String) {/ / text encoding to GB_18030_2000 let gbkeEncoding = CFStringConvertEncodingToNSStringEncoding (CFStringEncoding (CFStringEncodings.GB_18030_2000.rawValue) let) data = text.dataUsingEncoding (gbkeEncoding) let size data.length let! = textData = malloc (size) data.getBytes (textData, length:size) addBytesCommand (textData, length: size (free) textData})

4. print pictures

The way to print an image is to find the item address on the GitHub


I used the method of converting pictures, using OC to write, because the project is urgent, direct use. When there is time, converted to swift, and then posted up

Select bitmap mode: ESC *, m, nL, nH, D1… DK

Sixteen hexadecimal code 1B 2A M NL NH D1… DK
Decimal code Twenty-seven Forty-two M NL NH D1… DK

[range] m = 0, 1, 32, 33,0 = nL = 255,0 = nH = 3,0 = D = 255

IOS socket connection small ticket printer
Select the bitmap mode - parameter / * * * func printBitmapModel bitmap: bitmap (bitmap:UIImage) {let data = IGThermalSupport.imageToThermalData (bitmap) //ThermalPrinterKit in this library method let = data.length picData = size let malloc (size) data.getBytes (picData, length:size) addBytesCommand (picData, length:, size) free (picData)}

Print two-dimensional code, you can become a two-dimensional code image, and then use the above method to write data

5. print small ticket

Finally, don’t forget to use it

AsyncSocket.writeData (sendData, withTimeout:, -1, tag:, 0)

Send data to the printer

Four. Pay attention

  1. In order to facilitate the use of socket connections, printer management methods are encapsulated, and then packaged a small ticket management class, direct use
  2. All parameters in the ESC/POS directive are of type UInt8. Sometimes it is a parameter type error, and the type is strongly converted to UInt8 (para), and it may be successful
  3. For the first time to develop the printer, if there is a mistake, please correct the exchange


1 recently compiled a demo, is written in OC, written several main methods of
2.swift demo version download

Six. Communication group

Just built a group, interested can add group discussion under