From 31c399937fcd3f9b8a529648b9979ef031f3edc5 Mon Sep 17 00:00:00 2001 From: Songzex <2402265378@qq.com> Date: Thu, 12 Mar 2026 12:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84=E8=A7=84?= =?UTF-8?q?=E5=88=92=E6=9A=82=E5=81=9C=20=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E8=A7=84=E5=88=92=E7=BB=A7=E7=BB=AD=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=B7=AF=E5=BE=84=E8=A7=84=E5=BC=80=E5=A7=8B=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=20=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84=E8=A7=84?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E5=B7=A5=E4=BD=9C+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/network/tcp/tcp_client.dart | 27 +++++++- .../auth/presentation/bloc/auth_cubit.dart | 2 +- .../data/models/route_plan_send_entity.dart | 63 ++++++++++++++----- .../route_planning_repository_impl.dart | 6 +- 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/lib/core/network/tcp/tcp_client.dart b/lib/core/network/tcp/tcp_client.dart index ce6c1e15..252b5a21 100644 --- a/lib/core/network/tcp/tcp_client.dart +++ b/lib/core/network/tcp/tcp_client.dart @@ -181,6 +181,11 @@ class TcpClient { _socket!.write(jsonEncode(data)); } + void sendnew(List data){ + _socket!.add(data); + } + + void sendRaw(int cmd, List payload) { if (_socket == null) return; @@ -268,7 +273,8 @@ class TcpClient { if (_socket == null) return; final payload = routePlanSendEntity.toBytes(); print("sendPathPoint-0x01开始发送路径点数据"); - sendRaw(0x01, payload); // 第1种的测试 声明指令结构和类型0x01 为命令类型 + //sendRaw(0x01, payload); // 第1种的测试 声明指令结构和类型0x01 为命令类型 + sendnew(payload); // 第1种的测试 声明指令结构和类型0x01 为命令类型 // _socket!.add(payload);//第二种的测试 print("底层发送指令完成"); } @@ -276,11 +282,26 @@ class TcpClient { void sendDeviceStateChange(RoutePlanSendEntity routePlanSendEntity) { if (_socket == null) return; + + // 🔥 核心修复:构造完整的协议包(包含头部) final payload = routePlanSendEntity.toBytes(); - // 🔥 核心修复:直接发送字节数组,不再用 sendRaw 包装 - _socket!.add(payload); + + // final builder = BytesBuilder() + // ..addByte(0xAB) + // ..addByte(0xAA) + // ..addByte(0x02) // ✅ 设备状态控制指令 + // ..add(payload) + // ..add([0x00, 0x00]) // CRC 占位 + // ..addByte(0xAA) + // ..addByte(0xAB); + // + // final packet = builder.takeBytes(); + sendnew(payload); + // _socket!.add(packet); + var counts = routePlanSendEntity.pointCounts; print("sendDeviceStateChange 底层发送指令完成,数据:$counts"); + /// print("📡 发送的完整数据包:${builder.takeBytes()}"); } diff --git a/lib/features/auth/presentation/bloc/auth_cubit.dart b/lib/features/auth/presentation/bloc/auth_cubit.dart index f6e6627f..acf9c964 100644 --- a/lib/features/auth/presentation/bloc/auth_cubit.dart +++ b/lib/features/auth/presentation/bloc/auth_cubit.dart @@ -64,7 +64,7 @@ class AuthCubit extends Cubit { /// 当 HTTP 登录/注册成功后调用 Future loginSuccess(UserEntity user) async { await storage.saveUser(user); - //await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); + await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); // await _authTcpDatasource.sendAuthPacket(); //包括发送认证包和获取列表和切换函数 //tcp.startHeartbeat(interval: const Duration(seconds: 4)); //启动心跳 appCubit.setAuth(user); diff --git a/lib/features/devices/data/models/route_plan_send_entity.dart b/lib/features/devices/data/models/route_plan_send_entity.dart index fc91e904..11673cd8 100644 --- a/lib/features/devices/data/models/route_plan_send_entity.dart +++ b/lib/features/devices/data/models/route_plan_send_entity.dart @@ -1,7 +1,13 @@ -// lib/features/devices/data/models/route_plan_send_entity.dart -import 'dart:typed_data'; // ✅ 必须添加 +import 'dart:typed_data'; +import 'dart:convert'; class RoutePlanSendEntity { + final int header1 = 0xAB; + final int header2 = 0xAA; + final int endFlag1 = 0xAA; + final int endFlag2 = 0xAB; + final int crc16 = 0; + final int commandType; final int pointCounts; final double targetLatitude; @@ -16,23 +22,46 @@ class RoutePlanSendEntity { required this.speed, }); + /// 生成和Java二进制完全等效的字节数组(解决服务器识别问题) + Uint8List toBytes() { + final byteData = ByteData(27); + int offset = 0; - List toBytes() { - final bytes = []; - bytes.add(commandType); - bytes.addAll(_shortToBytes(pointCounts)); - bytes.addAll(_doubleToBytes(targetLatitude)); - bytes.addAll(_doubleToBytes(targetLongitude)); - bytes.addAll(_shortToBytes(speed)); - return bytes; - } + // 1. 严格按Java的byte类型写入(有符号单字节) + byteData.setInt8(offset++, header1); // 0xAB → 有符号-85 + byteData.setInt8(offset++, header2); // 0xAA → 有符号-86 - List _shortToBytes(int value) { - return [(value >> 8) & 0xFF, value & 0xFF]; - } + // 2. 命令类型(有符号byte) + byteData.setInt8(offset++, commandType); - List _doubleToBytes(double value) { - final byteData = ByteData(8)..setFloat64(0, value); + // 3. pointCounts(Java short → 有符号16位,小端) + byteData.setInt16(offset, pointCounts, Endian.little); + offset += 2; + + // 4. 纬度(Java double → 64位浮点,小端) + byteData.setFloat64(offset, targetLatitude, Endian.little); + offset += 8; + + // 5. 经度(Java double → 64位浮点,小端) + byteData.setFloat64(offset, targetLongitude, Endian.little); + offset += 8; + + // 6. speed(Java short → 有符号16位,小端) + byteData.setInt16(offset, speed, Endian.little); + offset += 2; + + // 7. CRC16(Java short → 有符号16位,小端) + byteData.setInt16(offset, crc16, Endian.little); + offset += 2; + + // 8. 包尾(有符号byte) + byteData.setInt8(offset++, endFlag1); // 0xAA → -86 + byteData.setInt8(offset++, endFlag2); // 0xAB → -85 + var uint8list = byteData.buffer.asUint8List(); + // 关键:直接返回原始二进制字节(不做任何映射/转换) + print("发送的指令字节数组,$uint8list"); return byteData.buffer.asUint8List(); } -} + + /// 发送给服务器的核心方法(模拟Java的OutputStream传输) +} \ No newline at end of file diff --git a/lib/features/devices/data/repositories/route_planning_repository_impl.dart b/lib/features/devices/data/repositories/route_planning_repository_impl.dart index 366207ab..43009ed9 100644 --- a/lib/features/devices/data/repositories/route_planning_repository_impl.dart +++ b/lib/features/devices/data/repositories/route_planning_repository_impl.dart @@ -145,7 +145,7 @@ class PathPlanner { _isPaused = true; var entity = new RoutePlanSendEntity( - commandType: 0x02, + commandType: 0x01, pointCounts: 2, targetLatitude: 0, targetLongitude: 0, @@ -160,7 +160,7 @@ class PathPlanner { _isPaused = false; var entity = new RoutePlanSendEntity( - commandType: 0x02, + commandType: 0x01, pointCounts: 3, targetLatitude: 0, targetLongitude: 0, @@ -182,7 +182,7 @@ class PathPlanner { _isPaused = false; // 清除暂停状态 var entity = new RoutePlanSendEntity( - commandType: 0x02, + commandType: 0x01, pointCounts: 0, targetLatitude: 0, targetLongitude: 0,