Java代码翻写成OC样例

Java代码

// TranslateData
public void translateData(byte[] data, int size) {
    for (int i = 0; i < size; i++) {
        byte b = data[i];
        if (head_count < HEAD_LENGTH) {
            switch (head_count) {
                case 0:
                    if (b == HOST) {
                        head_count++;
                        commandBean.setHost(b);
                    } else {
                        head_count = 0;
                    }
                    break;
                case 1:
                    commandBean.setCommand(b);
                    head_count++;
                    break;
                case 2:
                    commandBean.setSequence(b);
                    head_count++;
                    break;
                case 3:
                    commandBean.setOption(b);
                    head_count++;
                    break;
                case 4:
                    commandBean.setLength(b);
                    head_count++;
                    data_count = 0;
                    break;
                default:
                    break;
            }
        } else {
            if (data_count < commandBean.getLength()) {
                temp[data_count++] = b;
            } else {
                byte bcc = Common.bcc(temp, 0, commandBean.getLength());
                if (bcc == b) {
                    commandBean.setData(temp);
                    doCommand(commandBean);
                }
                head_count = 0;
                data_count = 0;
            }
        }
    }
}
// commandBean
public class CommandBean
implements Serializable
{
    private byte command;
    private byte[] data;
    private byte host;
    private byte length;
    private byte option;
    private byte sequence;
    
    public byte getCommand()
    {
      return command;
    }
    
    public byte[] getData()
    {
      return data;
    }
    
    public byte getHost()
    {
      return host;
    }
    
    public int getLength()
    {
      return Common.byte2Int(length);
    }
    
    public byte getOption()
    {
      return option;
    }
    
    public byte getSequence()
    {
      return sequence;
    }
    
    public void setCommand(byte paramByte)
    {
      command = paramByte;
    }
    
    public void setData(byte[] paramArrayOfByte)
    {
      data = paramArrayOfByte;
    }
    
    public void setHost(byte paramByte)
    {
      host = paramByte;
    }
    
    public void setLength(byte paramByte)
    {
      length = paramByte;
    }
    
    public void setOption(byte paramByte)
    {
      option = paramByte;
    }
    
    public void setSequence(byte paramByte)
    {
      sequence = paramByte;
    }
}
// 有关调用方法
translateData(buffer, bytes);

我们来看看buffer的具体样例:
a0780000 02040004
a0800000 13140080 30005555 33b2ddd9 01400000 0000be92 4c
a0800000 130d0080 30005555 33b2ddd9 01400000 0000be92 55
a0800000 13120080 30005555 33b2ddd9 01400000 0000be92 4aa08000 00130500 803000bb bb33b2dd d9014000 00000083 3ac8
a0800000 130e0080 3000bbbb 33b2ddd9 01400000 0000833a c3a08000 00131100 80300055 5533b2dd d9014000 000000be 9249
我们用a0780000 02040004来进行举例

// a0|78|00|00|02|04|0004| 我们这样来进行拆分
// 首先我们先来翻写translateData的Objective-C
#pragma mark -执行命令
- (void) doCommand:(struct DataBean) cmd {
    // int result;
    switch (cmd.command) {
        case CMD_GET_VERSION:
            [TopToast showToptoastWithText:@"获得版本!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_ERROR:
            [TopToast showToptoastWithText:@"系统错误!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_READ_LABEL:
            [TopToast showToptoastWithText:@"读取标签!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_WRITE_LABEL:
            [TopToast showToptoastWithText:@"写入标签!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_INVENTORY:
            [TopToast showToptoastWithText:@"查询标签!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_CLEAR_CACHE:
            [TopToast showToptoastWithText:@"清除缓存!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_SET_OUTPUT:
            [TopToast showToptoastWithText:@"设置功率!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_GET_OUTPUT:
            [TopToast showToptoastWithText:@"获取功率!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_BATTERY:
            [TopToast showToptoastWithText:@"获取电量!" duration:1 height:64 backGroundColor:[UIColor blackColor] alpha:1.0];
        break;
        case CMD_CHARGING:
            // TODO
        break;
        default:
            break;
    }
}

#pragma mark- 数据解析
- (void) translateData: (Byte[])data size:(long)size {
    for (int i = 0; i < size; i++) {
        Byte b = data[i];
        if (_head_count < HEAD_LENGTH) {
            switch (_head_count) {
                case 0:
                    if (b == HOST) {
                        _head_count++;
                        _dataBean.host = b;
                    } else {
                        _head_count = 0;
                    }
                break;
                case 1:
                    _dataBean.command = b;
                    _head_count++;
                break;
                case 2:
                    _dataBean.sequence = b;
                    _head_count++;
                break;
                case 3:
                    _dataBean.option = b;
                    _head_count++;
                break;
                case 4:
                    _dataBean.length = b;
                    _head_count++;
                    _data_count = 0;
                break;
            }
        } else {
            if (_data_count < _dataBean.length) {
                _temp[_data_count++] = b;
            } else  {
                // 暂时不进行BCC校验
                _dataBean.data = _temp;
                [self doCommand:_dataBean];
                _head_count = 0;
                _data_count = 0;
            }
        }
    }
}

欢迎留言

avatar
  Subscribe  
Notify of