本次实践过程中一种遇到4个问题,首先感谢斑马上海的技术支持,李工!谢谢!!!希望给后面有需要的朋友参考!
1.打印方式我选择的是cplp模式,而不是zpl
cplp的连接方式为:
id<ZebraPrinter,NSObject> printer =[ZebraPrinterFactory getInstance:connection withPrinterLanguage:PRINTER_LANGUAGE_CPCL];
整体代码块如下
```objectivec
NSString*errorMsg=@"";
BOOL didOpen = [connection open];
if(didOpen == YES) {
NSError *error = nil;
id<ZebraPrinter,NSObject> printer =[ZebraPrinterFactory getInstance:connection withPrinterLanguage:PRINTER_LANGUAGE_CPCL];
if(printer != nil) {
PrinterLanguage language = [printer getPrinterControlLanguage];
errorMsg=[NSString stringWithFormat:@"Printer Language %@",[self getLanguageName:language]];
errorMsg=@"Sending Data";
BOOL sentOK = [self printTestLabel:language onConnection:connection withError:&error];
if (sentOK == YES) {
errorMsg=@"Test Label Sent";
} else {
errorMsg=@"Test Label Failed to Print";
}
} else {
errorMsg=@"Could not Detect Language";
}
}else {
errorMsg=@"Could not connect to printer";
}
NSLog(@"%@",errorMsg);
[connection close];
2.UI主线程下发送打印指令是不执行的,你得自己开启线程来做这一块。
3.命令行里面每个都需要\r\n结尾
4.如果需要打印中文,你需要
命令行里需要设置:
**COUNTRY CHINA**
下面是测试字符串
NSString*str=@"! 0 200 200 228 1\r\nLABEL CONTRAST 0\r\nSPEED 4\r\nPAGE-WIDTH 400\r\nGAP-SENSE\r\nSETMAG 0 0\r\nPRESENT-AT 18 1\r\nCOUNTRY CHINA\r\nENCODING GB18030\r\nTEXT GBUNSG24.CPF 0 0 50 序号: 01\r\nTEXT GBUNSG24.CPF 0 0 160 时间:2019-10-15 10:31:31\r\nFORM\r\nPRINT\r\n";
否认是utf-8格式的,需要kCFStringEncodingGB_18030_2000 才能打印中文
NSString *testLabel;
unsigned long encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *data = [testLabel dataUsingEncoding:encode];
然后再发送打印
[connection write:data error:error];