49 lines
1.6 KiB
Dart
Raw Normal View History

import '../io_tool/io_manager.dart';
import '../io_tool/io_tool.dart';
import 'io_type.dart';
abstract class IOData {
List<int> messageDetail();
}
abstract class SenderProtocol extends IOData {
CommandType? commandType; //指令类型
final int header = 0XEF01EE02; //帧头 固定取值 0XEF01EE02长度 4 字节
final int ask = 0X01 ; // 包类型0X01 表示请求包0X11 表示应答包,长度 1 字节
int? _commandIndex; //帧序号
final int identifier = 0x22 ; // 高 4 位表示包版本,低 4 位用来指示后面数据的加密类型,长度为 1 字节加密类型取值说明0明文1AES1282SM4事先约定密钥3SM4设备指定密钥
List<int>? commandData = []; //数据域
final int? tail = 0xFF; //帧尾
SenderProtocol(this.commandType) {
_commandIndex = IoManager().commandIndex;
}
//TODO:拼装数据
List<int> packageData() {
commandData = messageDetail();
List<int> commandList = [];
commandList.add(header); //帧头
commandList.add(_commandIndex!); //帧序号
commandList.addAll(intToInt8List(dataSourceLength()));
int type = commandType!.typeValue;
commandList.addAll(intToInt8List(type)); //指令类型
commandList.addAll(commandData!); //数据域
commandList.add(checkSum(commandList)); //校验和
commandList.add(tail!); //帧尾
//帧头
// commandList.add(0xEF);
// commandList.add(0x01);
// commandList.add(0xEE);
// commandList.add(0x02);
return commandList;
}
//TODO:校验和
int dataSourceLength() => commandData!.length;
}