最近做了利用socket連接打印機的小功能,記錄一下,demo在最下面.
-
SocketManager
連接管理類:使用的是GCDAsyncSocket,使用詳情自己看demo就可以,其實挺簡單的.
@interface SocketManager : NSObject
/** 單利 */
+ (instancetype)shareInstance;
/** 連接socket */
- (void)socketConnectToHost:(NSString *)host port:(uint16_t)port timeout:(NSTimeInterval)timeout;
/** 發(fā)送數(shù)據(jù) */
- (void)socketWriteData:(NSData *)data;
/** 斷開連接 */
- (void)socketDisConnect;
- LCPrinterManager打印機指令管理類(可能不同的打印機指令有點不同,這部分指令參考打印機的開發(fā)文檔)
@interface LCPrinterManager : NSObject
// 打印數(shù)據(jù)(文字圖片信息)
@property (nonatomic, strong) NSMutableData *sendData;
// 錄入文字
-(void)printAddText:(NSString *)text;
// 打印并換行
-(void)printAndGotoNextLine;
// 設(shè)置絕對打印位置
-(void)printAbsolutePosition:(NSInteger)location;
// 選擇位圖模式
- (void)printBitmapModel:(UIImage *)bitmap;
// 設(shè)置默認(rèn)行間距(約3.75mm)
- (void)printDefaultLineSpace;
// 初始化打印機
- (void)printInitialize;
// 打印并走紙
- (void)printPrintAndFeedPaper:(CGFloat)space;
// 設(shè)置字號
- (void)printSelectFont:(kCharFont)size;
// 設(shè)置成標(biāo)準(zhǔn)模式
- (void)printSetStanderModel;
// 設(shè)置對齊方式
-(void)printAlignmentType:(kAlignmentType)type;
// 產(chǎn)生錢箱控制脈沖
-(void)printOpenCashDrawer;
// 選擇字符大小
-(void)printCharSize:(kCharScale)scale;
// 設(shè)置左邊距
- (void)printLeftMargin:(CGFloat)left;
// 設(shè)置橫向和縱向移動單位
- (void)printDotDistanceW:(CGFloat)w h:(CGFloat)h;
// 選擇切紙模式并切紙
-(void)printCutPaper:(kCutPaperModel)model Num:(UInt8)n;
// 設(shè)置每行打印寬度
- (void)printAreaWidth:(CGFloat)width;
- 打印機使用的是ESC/POS指令 ,舉幾個例子演示指令的發(fā)送.詳情參閱demo.
打印機初始化
image
/** 初始化打印機 */
- (void)printInitialize {
unsigned char data[] = {0x1B, 0x40};
[self.sendData appendBytes:data length:2];
}
-
設(shè)置橫向和縱向移動單位
image
[范圍] 0≤x≤255 0≤y≤255
[描述] 分別將橫向移動單位近似設(shè)置成25.4/ x mm(1/ x英寸)縱向移動單位設(shè)置成25.4/ y mm(1/ y英寸)锥惋。
/**
注意:文檔中表示:橫向移動單位 = 25.4 / x, 縱向移動單位 = 25.4 / y
*/
- (void)printDotDistanceX:(int)x y:(int)y {
unsigned char data[] = {0x1D,0x50,x,y};
[self addBytesCommand:data Length:4];
}
/**
希望能夠直接設(shè)置橫向和縱向移動單位,改成下面寫法
- parameter horizontal: 橫向移動單位
- parameter vertical: 縱向移動單位
*/
- (void)printDotDistanceW:(CGFloat)w h:(CGFloat)h {
unsigned char width = (unsigned char)(25.4/w);
unsigned char height = (unsigned char)(25.4/h);
unsigned char data[] = {0x1D,0x50,width,height};
[self addBytesCommand:data Length:4];
}
- LCSockerPrinterManager打印小票管理類
@interface LCSockerPrinterManager : NSObject
/** 單利 */
+ (instancetype)shareInstance;
/** 銷毀單利 */
+ (void)destoryInstance;
/** 連接打印機 */
- (void)connectWithHost:(NSString *)host port:(uint16_t)port timeout:(NSTimeInterval)timeout;
/** 基礎(chǔ)設(shè)置 */
- (void)basicSetting;
/** 清空緩存數(shù)據(jù) */
- (void)clearData;
/** 寫入單行文字 */
- (void)writeData_title:(NSString *)title Scale:(kCharScale)scale Type:(kAlignmentType)type;
/** 寫入多行文字 */
- (void)writeData_items:(NSArray *)items;
/** 打印圖片 */
- (void)writeData_image:(UIImage *)image alignment:(kAlignmentType)alignment maxWidth:(CGFloat)maxWidth;
/** 條目,菜單,有間隔, 字典數(shù)組中字典用key1, key2,...表示 */
- (void)writeData_content:(NSArray *)items;
/** 打印分割線 */
- (void)writeData_line;
/** 打開錢箱 */
- (void)openCashDrawer;
/** 打印小票 */
- (void)printReceipt;
- 使用方法
// LCSocketPrintViewController.m
- (void)printReceipt:(id)sender {
NSString *host = @"192.168.1.241";
UInt16 port = 9100;
NSTimeInterval timeout = 10;
LCSockerPrinterManager *manager = [LCSockerPrinterManager shareInstance];
[manager connectWithHost:host port:port timeout:timeout];
[manager basicSetting];
[manager writeData_title:@"XXX外賣店" Scale:scale_2 Type:MiddleAlignment];
[manager writeData_items:@[@"收銀員:001", @"交易時間:2016-03-17", @"交易號:201603170001"]];
[manager writeData_line];
[manager writeData_content:@[@{@"key1":@"名稱", @"key2":@"單價", @"key3":@"數(shù)量", @"key4":@"總價"}]];
[manager writeData_line];
[manager writeData_content:@[@{@"key1":@"漢堡", @"key2":@"10.00", @"key3":@"2", @"key4":@"20.00"}, @{@"key1":@"炸雞", @"key2":@"8.00", @"key3":@"1", @"key4":@"8.00"}]];
[manager writeData_line];
[manager writeData_items:@[@"支付方式:現(xiàn)金", @"應(yīng)收:98.00", @"實際:100.00", @"找零:2.00"]];
[manager openCashDrawer];
[manager printReceipt];
}