@@ -64,3 +64,87 @@ | |||
2020-9-8 14:55:47 Recv:00 00 00 00 00 FD 09 01 FB 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-8 14:56:20 Send:00 00 00 00 00 06 09 01 00 00 07 B0 | |||
2020-9-8 14:56:23 Recv:00 00 00 00 00 F9 09 01 F7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-9 10:8:59 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 10:8:59 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-9 10:9:49 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 10:9:49 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-9 10:11:35 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 10:11:35 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-9 10:11:43 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 10:11:43 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-9 10:12:22 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 10:12:22 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 | |||
2020-9-9 10:13:29 Send:00 00 00 00 00 06 01 01 00 7D 00 09 | |||
2020-9-9 10:13:29 Recv:00 00 00 00 00 05 01 01 02 00 00 | |||
2020-9-9 10:30:52 Send:00 00 00 00 00 06 01 01 00 00 00 09 | |||
2020-9-9 10:30:52 Recv:00 00 00 00 00 05 01 01 02 00 00 | |||
2020-9-9 10:32:36 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 10:32:36 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 | |||
2020-9-9 10:56:40 Send:00 00 00 00 00 06 01 01 00 00 00 0A | |||
2020-9-9 10:56:40 Recv:00 00 00 00 00 05 01 01 02 00 00 | |||
2020-9-9 10:57:6 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 10:57:6 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 | |||
2020-9-9 10:57:39 Send:00 00 00 00 00 06 01 01 00 00 00 64 | |||
2020-9-9 10:57:39 Recv:00 00 00 00 00 10 01 01 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-9 10:59:7 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-9 10:59:7 Recv:00 00 00 00 00 03 01 83 01 | |||
2020-9-9 11:0:3 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-9 11:0:3 Recv:00 00 00 00 00 03 01 83 01 | |||
2020-9-9 11:7:58 Send:00 00 00 00 00 07 01 0F 00 00 00 01 00 01 | |||
2020-9-9 11:7:58 Recv:00 00 00 00 00 03 01 8F 03 | |||
2020-9-9 11:9:39 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 11:9:39 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 11:10:33 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:11:12 Send:00 00 00 00 00 07 01 0F 00 00 00 02 01 03 | |||
2020-9-9 11:14:50 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:14:55 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-9 11:15:9 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 11:15:12 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 11:15:20 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:16:10 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:16:38 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:16:39 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-9 11:16:51 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 11:16:52 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 11:17:2 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 11:17:50 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:3:25 Send:00 00 00 00 00 06 01 01 00 00 00 C8 | |||
2020-9-9 13:5:44 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:6:15 Recv:31 31 | |||
2020-9-9 13:6:33 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:14:6 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:52:49 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:53:28 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:55:15 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:56:4 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:57:32 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 13:57:54 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:8:1 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:9:34 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:11:36 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:12:13 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:14:56 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:15:39 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:17:5 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:17:16 Send:00 00 00 00 00 06 01 01 00 01 00 01 | |||
2020-9-9 14:17:48 Send:00 00 00 00 00 06 01 01 00 01 00 01 | |||
2020-9-9 14:21:43 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 14:21:43 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 EE | |||
2020-9-9 14:23:16 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 14:23:16 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF | |||
2020-9-9 14:24:1 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 14:24:1 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 14:24:12 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:24:22 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:25:22 Send:00 00 00 00 00 06 01 01 00 00 07 D0 | |||
2020-9-9 14:25:36 Recv:00 00 00 00 00 FD 01 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF | |||
2020-9-9 14:25:45 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 14:25:47 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 14:25:56 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:26:17 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:31:4 Send:00 00 00 00 00 06 01 01 00 00 00 C8 | |||
2020-9-9 14:31:4 Recv:00 00 00 00 00 1C 01 01 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-9 14:31:17 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-9 14:31:17 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-9 14:31:22 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-9 14:31:29 Send:00 00 00 00 00 06 01 01 00 00 00 01 |
@@ -76,7 +76,7 @@ void Input_IP(string& IP, unsigned int *Port_number) | |||
cout << "请重新输入从站端口号:"; | |||
cin >> *Port_number; | |||
i = 0; | |||
} while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围0---65535 | |||
} while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围1---65535 | |||
} | |||
@@ -102,42 +102,52 @@ SOCKET Init_client(string IP, unsigned int Port_number) | |||
} | |||
cout << "连接TCP从站成功" << endl; | |||
TIMEVAL timeout; | |||
timeout.tv_sec = 20000; //ms | |||
timeout.tv_sec = 200; //ms | |||
timeout.tv_usec = 0; //us | |||
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间 | |||
return ClientSocket; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 清除接收缓冲区中的数据 | |||
* 描述 : 每一次发送请求前清空缓存区数据 | |||
* 功能 : 清除接收缓冲区中的数据和异常连接状态检测 | |||
* 描述 : 每一次发送请求前清空缓存区数据,检测当前套接字连接状态 | |||
* 输入 : ClientSocket 客户端套接字 | |||
* 输出 : 无 | |||
* 输出 : true 连接正常 false 连接异常 | |||
*********************************************************************************************/ | |||
void Clear_recv_buf(SOCKET clientSocket) | |||
bool Clear_recv_buf(SOCKET clientSocket) | |||
{ | |||
struct timeval tmOut; | |||
tmOut.tv_sec = 0; | |||
tmOut.tv_usec = 0; | |||
fd_set fds; | |||
int nRet; | |||
char tmp[2]; | |||
TIMEVAL timeout; | |||
timeout.tv_sec = 0; | |||
timeout.tv_usec = 0; | |||
fd_set reads; | |||
char tmp[100]; | |||
memset(tmp, 0, sizeof(tmp)); | |||
while (1) | |||
{ | |||
FD_ZERO(&fds); | |||
FD_SET(clientSocket, &fds); | |||
nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut); | |||
if (nRet == 0) | |||
break; | |||
recv(clientSocket, tmp, 1, 0); | |||
FD_ZERO(&reads); | |||
FD_SET(clientSocket, &reads); | |||
int len = recv(clientSocket, tmp, 50, 0); | |||
int a = select(0, &reads, 0, 0, &timeout); | |||
if ( a == 1 && len == 0) | |||
return false; | |||
if ( a == 1&& len == (-1)) | |||
return false; | |||
if ( a == 0 && len == (-1)) | |||
return true; | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 打印读取从站的线圈状态 | |||
* 描述 : 对响应报文中的数据进行解析和显示 | |||
* 输入 : Response_Message 响应报文 Request_Message 请求报文 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Printf_Coil_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
{ | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9])+1; | |||
unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9]); | |||
unsigned int Number = ((Request_Message[10] << 8) | Request_Message[11]); | |||
printf("线圈起始地址为%d \n",temp1); | |||
unsigned int temp = temp1; | |||
@@ -146,11 +156,17 @@ void Printf_Coil_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
unsigned int temp2 = temp + 7; | |||
if (temp2 > temp1 + Number - 1) | |||
temp2 = temp1 + Number - 1; | |||
printf("线圈第%d --- %d的状态为:%02X \n", temp2, temp, Response_Message[9+i]); | |||
printf("线圈%d --- %d的状态为:%02X \n", temp2, temp, Response_Message[9+i]); | |||
temp = temp + 8; | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 打印读取从站的寄存器状态 | |||
* 描述 : 对响应报文中的数据进行解析和显示 | |||
* 输入 : Response_Message 响应报文 Request_Message 请求报文 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Printf_Register_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
{ | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
@@ -161,25 +177,34 @@ void Printf_Register_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
{ | |||
printf("寄存器第%d的值为:%02X %02X \n", temp1++, Response_Message[9 + i], Response_Message[10+i]); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 打印读取异常响应报文信息 | |||
* 描述 : 对响应报文中的数据进行解析和显示 | |||
* 输入 : Response_Message 响应报文 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Printf_Anomaly_date(UINT8 *Response_Message) | |||
{ | |||
UINT8 a = Response_Message[8]; | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
switch (a) | |||
{ | |||
case 0x01: printf("%02X : 从站设备不支持此功能码",a); break; | |||
case 0x02: printf("%02X : 指定的数据地址在从站设备中不存在",a); break; | |||
case 0x03: printf("%02X : 指定的数据超过范围或者不允许使用",a); break; | |||
case 0x04: printf("%02X : 从站设备处理响应的过程中,出现未知错误等",a); break; | |||
default: printf("Unkown Other Error!!!!!"); | |||
case 0x01: printf("%02X : 从站设备不支持此功能码\n",a); break; | |||
case 0x02: printf("%02X : 指定的数据地址在从站设备中不存在\n",a); break; | |||
case 0x03: printf("%02X : 指定的数据超过范围或者不允许使用\n",a); break; | |||
case 0x04: printf("%02X : 从站设备处理响应的过程中,出现未知错误等\n",a); break; | |||
default: printf("Unkown Other Error!!!!!\n"); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 判断响应报文是否可以正常解析 | |||
* 描述 : 通过长度和异常码等判定该响应报文是否可以解析 | |||
* 输入 : Response_Message 响应报文 Request_Message请求报文 Response_Message_len 响应报文长度 | |||
* 输出 : true可以正常解析 false 无法解析 | |||
*********************************************************************************************/ | |||
bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, int Response_Message_len) | |||
{ | |||
if (Response_Message[7] == Request_Message[7] + 0x80 && Response_Message_len == 9)//先处理异常响应 | |||
@@ -217,8 +242,8 @@ bool Tcp_client(string IP, unsigned int Port_number) | |||
cout << "初始化主站失败" << endl; | |||
return false; | |||
} | |||
UINT8 Request_Message[260]; | |||
UINT8 Response_Message[260]; | |||
UINT8 Request_Message[300]; | |||
UINT8 Response_Message[300]; | |||
while (true) | |||
{ | |||
@@ -234,9 +259,23 @@ bool Tcp_client(string IP, unsigned int Port_number) | |||
} | |||
printf("\n"); | |||
Log_Note(Request_Message, 1, Request_Message_len); | |||
Clear_recv_buf(ClientSocket); | |||
send(ClientSocket, (char*)Request_Message, Request_Message_len, 0); | |||
memset(Response_Message, 0, 260); | |||
if (Clear_recv_buf(ClientSocket) == false) | |||
{ | |||
printf("连接异常,请检查连接状态。\n"); | |||
printf("**************************** Press Enter To Close ****************************\n"); | |||
getchar(); | |||
break; | |||
} | |||
int test = send(ClientSocket, (char*)Request_Message, Request_Message_len, 0); | |||
if (test == (-1)) | |||
{ | |||
printf("连接异常,请检查连接状态。\n"); | |||
printf("**************************** Press Enter To Close ****************************\n"); | |||
getchar(); | |||
break; | |||
} | |||
memset(Response_Message, 0, 300); | |||
int Response_Message_len = recv(ClientSocket, (char*)Response_Message, 260, 0); | |||
if (Response_Message_len > 0) | |||
{ | |||
@@ -244,14 +283,17 @@ bool Tcp_client(string IP, unsigned int Port_number) | |||
printf("从站响应 :"); | |||
for (int i = 0; i < Response_Message_len; i++) | |||
{ | |||
printf("0x%02x, ", Response_Message[i]); | |||
printf("%02x ", Response_Message[i]); | |||
} | |||
printf("\n"); | |||
if (Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len)) | |||
if (!Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len)) | |||
printf("响应报文异常\n\n"); | |||
} | |||
else | |||
cout << "响应超时" << endl; | |||
printf("**************************** Press Enter To Contioun ****************************\n"); | |||
getchar(); | |||
system("cls"); | |||
} | |||
//关闭套接字 | |||
closesocket(ClientSocket); | |||
@@ -156,7 +156,7 @@ unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_ | |||
} | |||
if (Function_code == 0x10) | |||
Write_date_number = Operations_Number * 2; | |||
return Write_date_number +1; | |||
return Write_date_number; | |||
} | |||
/********************************************************************************************* | |||
@@ -229,7 +229,7 @@ int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len, int Func | |||
{ | |||
if (Write_date.length() == 0) | |||
return Message_len; | |||
Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number)-1; | |||
Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number); | |||
Message_len++; | |||
const char *b = Write_date.c_str(); | |||
for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3) | |||
@@ -11,7 +11,7 @@ | |||
#pragma comment(lib,"ws2_32.lib") | |||
using namespace std; | |||
#define DEVICE_ID 0x09 //�구ID | |||
#define DEVICE_ID 0x01 //�구ID | |||