等会每个命令会按照如下格式贴出:
打印命令有两种:
说明: 对齐方式有两种,对应的十六进制 {0x1B,0x61,0x00}、{0x1B,0x61,0x01}、{0x1B,0x61,0x02} 或者 {0x1B,0x61,0x30}、{0x1B,0x61,0x31}、{0x1B,0x61,0x32}。
以上是我找到的比较完整的命令集合说明,希望能帮到他人。
这里有其他简友伊布林
提供的另一份打印机指令集的文档地址: 打印机指令集文档
这里有我最初用最原始的指令集拼接出来的NSData代码片段,供大家参考:
// 打印机支持的文字编码
NSLog(@"goodsArray:%@",goodsArray);
// 用到的goodsArray跟github中的商品数组是一样的。
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *title = @"测试电商";
NSString *str1 = @"测试电商服务中心(销售单)";
NSString *line = @"- - - - - - - - - - - - - - - -";
NSString *time = @"时间:2016-04-27 10:01:50";
NSString *orderNum = @"订单编号:4000020160427100150";
NSString *address = @"地址:深圳市南山区学府路东科技园店";
//初始化打印机
Byte initBytes[] = {0x1B,0x40};
NSData *initData = [NSData dataWithBytes:initBytes length:sizeof(initBytes)];
//换行
Byte nextRowBytes[] = {0x0A};
NSData *nextRowData = [NSData dataWithBytes:nextRowBytes length:sizeof(nextRowBytes)];
//居中
Byte centerBytes[] = {0x1B,0x61,1};
NSData *centerData= [NSData dataWithBytes:centerBytes length:sizeof(centerBytes)];
//居左
Byte leftBytes[] = {0x1B,0x61,0};
NSData *leftdata= [NSData dataWithBytes:leftBytes length:sizeof(leftBytes)];
NSMutableData *mainData = [[NSMutableData alloc]init];
//初始化打印机
[mainData appendData:initData];
//设置文字居中/居左
[mainData appendData:centerData];
[mainData appendData:[title dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[str1 dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
// UIImage *qrImage =[MMQRCode createBarImageWithOrderStr:@"RN3456789012"];
// UIImage *qrImage =[MMQRCode qrCodeWithString:@"http://www.sina.com" logoName:nil size:400];
// qrImage = [self scaleCurrentImage:qrImage];
//
// NSData *data = [IGThermalSupport imageToThermalData:qrImage];
// [mainData appendData:centerData];
// [mainData appendData:data];
// [mainData appendData:nextRowData];
[mainData appendData:leftdata];
[mainData appendData:[line dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[time dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[orderNum dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[address dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[line dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
NSString *name = @"商品";
NSString *number = @"数量";
NSString *price = @"单价";
[mainData appendData:leftdata];
[mainData appendData:[name dataUsingEncoding:enc]];
Byte spaceBytes1[] = {0x1B, 0x24, 150 % 256, 0};
NSData *spaceData1 = [NSData dataWithBytes:spaceBytes1 length:sizeof(spaceBytes1)];
[mainData appendData:spaceData1];
[mainData appendData:[number dataUsingEncoding:enc]];
Byte spaceBytes2[] = {0x1B, 0x24, 300 % 256, 1};
NSData *spaceData2 = [NSData dataWithBytes:spaceBytes2 length:sizeof(spaceBytes2)];
[mainData appendData:spaceData2];
[mainData appendData:[price dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
CGFloat total = 0.0;
for (NSDictionary *dict in goodsArray) {
[mainData appendData:[dict[@"name"] dataUsingEncoding:enc]];
Byte spaceBytes1[] = {0x1B, 0x24, 150 % 256, 0};
NSData *spaceData1 = [NSData dataWithBytes:spaceBytes1 length:sizeof(spaceBytes1)];
[mainData appendData:spaceData1];
[mainData appendData:[dict[@"amount"] dataUsingEncoding:enc]];
Byte spaceBytes2[] = {0x1B, 0x24, 300 % 256, 1};
NSData *spaceData2 = [NSData dataWithBytes:spaceBytes2 length:sizeof(spaceBytes2)];
[mainData appendData:spaceData2];
[mainData appendData:[dict[@"price"] dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
total += [dict[@"price"] floatValue] * [dict[@"amount"] intValue];
}
[mainData appendData:[line dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[@"总计:" dataUsingEncoding:enc]];
Byte spaceBytes[] = {0x1B, 0x24, 300 % 256, 1};
NSData *spaceData = [NSData dataWithBytes:spaceBytes length:sizeof(spaceBytes)];
[mainData appendData:spaceData];
NSString *totalStr = [NSString stringWithFormat:@"%.2f",total];
[mainData appendData:[totalStr dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:[line dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[mainData appendData:centerData];
[mainData appendData:[@"谢谢惠顾,欢迎下次光临!" dataUsingEncoding:enc]];
[mainData appendData:nextRowData];
[self.peripheral writeValue:mainData forCharacteristic:self.chatacter type:CBCharacteristicWriteWithoutResponse];
如果你连接成功,但是发出打印指令后,打印机没反应,很有可能是因为你的打印机一次发送的数据长度小于146,你把146改的更小一点试试看。
我测试的两台佳博打印机,一台没有长度限制,一台最多每次只能发送146个字节,否则会出现打印没反应的情况,需要重启打印机。
不同的打印机,可能对长度的限制不太一样,据群友反应有的打印机只能支持一次发送20个字节,所以你需要将宏里面的146改的更小一点。
Have Fun!