目标C - 如何格式化ESC / POS命令?

时间:2011-04-09 21:48:42

标签: objective-c

下面是一些显示来自Epson的ESC / POS命令的示例代码,不幸的是我是关于字符格式和目标c的新手。

PRINT #1, CHR$(&H1B);"@";
PRINT #1, CHR$(&H1B);"a";CHR$(1);
PRINT #1, CHR$(&H1B);"!";CHR$(0);
PRINT #1, CHR$(&H1B);"J";CHR$(4);
PRINT #1, “January 14, 1998 15:00”;
PRINT #1, CHR$(&H1B);"d";CHR$(3);
PRINT #1, CHR$(&H1B);"a";CHR$(0);
PRINT #1, CHR$(&H1B);"!";CHR$(1);
PRINT #1, "TM-U200B $20.00";CHR$(&HA);
PRINT #1, "TM-U200D $21.00";CHR$(&HA);
PRINT #1, "PS-170 $17.00";CHR$(&HA);
PRINT #1, CHR$(&HA);
PRINT #1, CHR$(&H1B);”!”;CHR$(17);
PRINT #1, CHR$(&H1B);”U”;CHR$(1);
PRINT #1, "TOTAL $58.00";CHR$(&HA);
PRINT #1, CHR$(&H1B);"U";CHR$(0);
PRINT #1, CHR$(&H1B);”!”;CHR$(0);
PRINT #1, "------------------------------";CHR$(&HA);
PRINT #1, "PAID $60.00";CHR$(&HA);
PRINT #1, "CHANGE $ 2.00";CHR$(&HA);
PRINT #1, CHR$(&H1D);"V";CHR$(66);CHR$(0);
END

任何人都知道如何将上述内容转换为NSData格式(在单个NSData对象或多个NSData对象中)?非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

我建议创建一个格式化程序类,它保存一个NSMutableData对象,表示要发送到打印机的字节。例如,

// EpsonFormatter.h
#import <Foundation/Foundation.h>

@interface EpsonFormatter : NSObject {
    NSMutableData *_data;
}

@property (readonly) NSMutableData *data;

- (void)initializePrinter;
- (void)selectCenterJustification;
- (void)selectStandardPrinterMode;
- (void)appendString:(NSString *)string;
@end

// EpsonFormatter.m
#import "EpsonFormatter.h"

@implementation EpsonFormatter

#define EpsonFormatterESCString "\x1b"

@synthesize data = _data;

- (id)init {
    self = [super init];
    if (self) {
        _data = [[NSMutableData alloc] init];
    }
    return self;
}

- (void)dealloc {
    [_data release];
    [super dealloc];
}

- (void)initializePrinter {
    [_data appendBytes:EpsonFormatterESCString "@" length:2];
}

- (void)selectCenterJustification {
    [_data appendBytes:EpsonFormatterESCString "a" "\x1" length:3];
}

- (void)selectStandardPrinterMode {
    [_data appendBytes:EpsonFormatterESCString "!" "\x0" length:3];
}

- (void)appendString:(NSString *)string {
    [_data appendData:[string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
}

@end

你会像这样使用这个类:

#import "EpsonFormatter.h"
EpsonFormatter *epsonFormatter = [[[EpsonFormatter alloc] init] autorelease];
[epsonFormatter initializePrinter];
[epsonFormatter selectCenterJustification];
[epsonFormatter selectStandardPrinterMode];
[epsonFormatter appendString:@"January 14, 1998 15:00"];
// …

// In order to obtain the underlying NSData object:
NSData *outputData = epsonFormatter.data;