From 3811aa4723c554d1835da6fa281c85940164f124 Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Sat, 29 Aug 2020 16:12:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8A=9F=E8=83=BD=E7=A0=81?= =?UTF-8?q?=E9=80=89=E6=8B=A9=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 | 29 +++++++++++++++ Modbus_communication/Modbus_TCP/main.cpp | 41 +++++++++++++++------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp index d35802b..a4fdd9e 100644 --- a/Modbus_communication/Modbus_TCP/common.cpp +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -78,3 +78,32 @@ void Input_IP(string& IP, unsigned int *Port_number) } while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围0---65535 } + +/********************************************************************************************* +* 功能    : 选择功能码 +* 描述   : 在功能码 0x01 0x03 0x0F 0x10中选择一个功能码 +* 输入 : 无 +* 输出 : 选择的功能码 +*********************************************************************************************/ +int Input_Function_code(void) +{ + int Function_code; + cin >> hex; + int i = 1; + printf("功能码 : 0x01 0x03 0x0F 0x10 \n"); + printf("说明 : 读线圈 读寄存器 写多个线圈 写多个寄存器\n"); + do + { + if (i == 1) + { + i = 0; + cout << "请输入选择的功能码:"; + } + else + cout << "输入功能码错误,请重新选择的功能码"; + cin >> Function_code; + cin.clear(); + cin.sync(); + } while (Function_code != 0x01 && Function_code != 0x03 && Function_code != 0x0F && Function_code != 0x10); + return Function_code; +} diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp index bf05931..bd2a329 100644 --- a/Modbus_communication/Modbus_TCP/main.cpp +++ b/Modbus_communication/Modbus_TCP/main.cpp @@ -1,20 +1,35 @@ #include "main.h" -string input() -{ - string str; - cout << "stringݣ" << endl; - getline(cin, str, '\n'); - cout << str << endl; - return str; -} - int main() { - string IP; - unsigned int Port_number; - Input_IP(IP, &Port_number); - Tcp_client(IP, Port_number); + //string IP; + //unsigned int Port_number; + //Input_IP(IP, &Port_number); + //Tcp_client(IP, Port_number); + + int Function_code; + while (1) + { + cin >> hex; + int i = 1; + printf(" 0x01 0x03 0x0F 0x10 \n"); + printf("˵ Ȧ Ĵ дȦ дĴ\n"); + do + { + if (i == 1) + { + i = 0; + cout << "ѡĹ룺"; + } + else + cout << "빦ѡĹ"; + cin >> Function_code; + cin.clear(); + cin.sync(); + } while (Function_code != 0x01 && Function_code != 0x03 && Function_code != 0x0F && Function_code != 0x10); + printf("%02X", Function_code); + getchar(); + } getchar(); return 0; } \ No newline at end of file