1

我正在尝试制作一个应用程序,使用底座到 RS232 线(我从 RedPark 购买)将 iPhone 与另一个硬件通信。我也在使用 redpark 提供的库。一开始我做了一个简单的代码,它工作得很好。UInt8 infoCmd[5] = {0x3E,0x3E,0x05,0x80,0xff};[rscMgr 写入:infoCmd 长度:5];
然后我想向它添加更多命令,所以我创建了一个方法来返回我需要的不同命令组合。

- (UInt8 *)requestCommand:(int)commandName{
    UInt8 * command;
    if (commandName == DATADUMP) {
        command=[Communication buildDataDump];
    }
    if (commandName == GETSERIALINFO) {
        command=[Communication buildGetSerailInfo];   
    }
    return command;
}
+ (UInt8 *)buildGetSerailInfo{
    UInt8 *command = malloc(sizeof(UInt8)*5);
    command[0]=SYN;
    command[1]=SYN;
    command[2]=ENQ;
    command[3]=GETSERIALINFO;
    //command[4] = {SYN, SYN, ENQ, GETSERIALINFO};
    return command;    
}

问题是,我的一些命令包含 200 字节长的数据。如何创建一个更容易添加字节的 UInt8 数组?我是编程新手,请详细解释一下。非常感谢您。

4

1 回答 1

1

实际上,您只会通过线路发送数据,行字节。我在一个项目中做了类似的事情(不是有线,而是通过 TCP/IP 的 RS232 命令),如果您使用 NSMutableData 实例,它变得非常简单。

我的代码片段:

static u_int8_t codeTable[] =   { 0x1b, 0x74, 0x10 };
static u_int8_t charSet[]   =   { 0x1b, 0x52, 0x10 };
static u_int8_t formatOff[] =   { 0x1b, 0x21, 0x00 };
static u_int8_t reverseOn[] =   { 0x1d, 0x42, 0x01 };
static u_int8_t reverseOff[]=   { 0x1d, 0x42, 0x00 };
static u_int8_t paperCut[]  =   { 0x1d, 0x56, 0x0 };  


NSMutableData *mdata = [NSMutableData dataWithBytes:&formatOff length:sizeof(formatOff)];
[mdata appendBytes:&formatOff length:sizeof(formatOff)];
[mdata appendBytes:&reverseOff length:sizeof(reverseOff)];    
[mdata appendData: [NSData dataWithBytes: &codeTable length:sizeof(codeTable)]];
[mdata appendData: [NSData dataWithBytes: &charSet length:sizeof(charSet)]];

如您所见,我只是逐字节附加数据。

于 2012-06-29T20:55:14.557 回答