From 44058e02ecf29afbfe72f7425e7fa5045c2e4d49 Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Mon, 31 Aug 2020 10:55:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8A=A5=E6=96=87MBAP?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Modbus_communication/Modbus_TCP/common.cpp | 10 ++++++++-- Modbus_communication/Modbus_TCP/common.h | 3 +++ Modbus_communication/Modbus_TCP/main.cpp | 9 +++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp index 4750310..68ad345 100644 --- a/Modbus_communication/Modbus_TCP/common.cpp +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -281,7 +281,13 @@ string Write_date(int Function_code, unsigned int Operations_Number) * 输入 : æ—  * 输出 : MBAP报文头内容 *********************************************************************************************/ -void Crate_MBAP() +void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Number) { - + Message[0] = 0x00; + Message[1] = 0x00; + Message[2] = 0x00; + Message[3] = 0x00; + Message[4] = 0x00; + Message[5] = Count_Write_date_number(Function_code, Operations_Number) + 0x06; + Message[6] = DEVICE_ID; } \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h index 8d7c125..7f9b972 100644 --- a/Modbus_communication/Modbus_TCP/common.h +++ b/Modbus_communication/Modbus_TCP/common.h @@ -10,6 +10,9 @@ #pragma comment(lib,"ws2_32.lib") using namespace std; +#define DEVICE_ID 0x10 //É豸ID + + bool Check_IP(char* IP); bool InitSocket_Version(void); diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp index b63786d..828c3f5 100644 --- a/Modbus_communication/Modbus_TCP/main.cpp +++ b/Modbus_communication/Modbus_TCP/main.cpp @@ -1,6 +1,10 @@ #include "main.h" //#include #define DEVICE_ID 0x10 +void test(UINT8 *Message) +{ + Message[7] = 0x10; +} int main() { @@ -14,9 +18,10 @@ int main() Message[4] = 0x00; Message[5] = Operations_Number; Message[6] = DEVICE_ID; - for (int i = 0; i < 7; i++) + test(Message); + for (int i = 0; i < 8; i++) { - printf("%02X", Message[i]); + printf("%02X ", Message[i]); } getchar();