From e40abcd89d5f08367f932edbc9f8be88992b9a9c Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Tue, 1 Sep 2020 23:46:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=8C=E5=96=84RTU?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modbus_RTU_Salve/common.cpp | 138 +++++++++++++++++- 1 file changed, 130 insertions(+), 8 deletions(-) diff --git a/Modbus_communication/Modbus_RTU_Salve/common.cpp b/Modbus_communication/Modbus_RTU_Salve/common.cpp index e151fe4..a3a0f09 100644 --- a/Modbus_communication/Modbus_RTU_Salve/common.cpp +++ b/Modbus_communication/Modbus_RTU_Salve/common.cpp @@ -108,6 +108,32 @@ void MSleep(long lTime) } while (dfTim= 0x30 && Comm[i] <= 0x39) + continue; + } + return true; +} + /********************************************************************************************* * 功能     :  输入端口名称 * 描述    : 获取串口通信的端口名称 @@ -116,29 +142,111 @@ void MSleep(long lTime) **********************************************************************************************/ string Input_COMM(void) { - + int flage = 1; + string Comm; + do + { + if (flage == 1) + { + printf("请输入端口名称:"); + flage = 0; + } + else + printf("请重新输入端口名称:"); + cin >> Comm; + } while (!Check_Input_COMM(Comm)); + return Comm; +} + +/********************************************************************************************* +* 功能     :  检查波特率 +* 描述    : 检查串口通信的波特率 +* 输入 : Baud_Rate 波特率 +* 返回值 : true 波特率正确 +* false 波特率错误 +**********************************************************************************************/ +bool Check_Input_Baud_Rate(unsigned int Baud_Rate) +{ + unsigned int Compare_Baud_Rate[9] = {300,600,1200,2400,4800,9600,14400,19200,38400}; + for (int i = 0; i < 9; i++) + { + if (Baud_Rate == Compare_Baud_Rate[i]) + return true; + } + return false; } /********************************************************************************************* * 功能     :  输入波特率 * 描述    : 获取串口通信的波特率 * 输入 : 无 -* 返回值 : Boud_Rate 波特率 +* 返回值 : Baud_Rate 波特率 **********************************************************************************************/ -unsigned int Input_Boud_Rate(void) +unsigned int Input_Baud_Rate(void) { - + unsigned int Baud_Rate; + int flage = 1; + printf("支持的波特率有:300,600,1200,2400,4800,9600,14400,19200,38400 \n"); + do + { + if (flage == 1) + { + printf("请输入串口波特率:"); + flage = 0; + } + else + printf("请重新输入串口波特率:"); + cin >> Baud_Rate; + } while (!Check_Input_Baud_Rate(Baud_Rate)); + return Baud_Rate; } + /********************************************************************************************* * 功能     :  输入数据位 * 描述    : 获取串口通信的数据位个数 * 输入 : 无 * 返回值 : Date_Bits 数据位 **********************************************************************************************/ -unsigned int Input_Date_Bits(void) +BYTE Input_Date_Bits(void) { + BYTE Date_Bits; + int flage = 1; + printf("支持的数据位有:5,6,7,8 \n"); + do + { + if (flage == 1) + { + printf("请输入串口波特率:"); + flage = 0; + } + else + printf("请重新输入串口波特率:"); + cin >> Date_Bits; + } while (!(Date_Bits == 5 || Date_Bits == 6 || Date_Bits == 7 || Date_Bits == 8)); + return Date_Bits; +} +/********************************************************************************************* +* 功能     :  检查停止位 +* 描述    : 检测停止位是否和数据位相符 +* 输入 : Date_Bits 数据位 +* Stop_Bits 停止位 +* 返回值 : true 停止位正确 +* false 停止位错误 +**********************************************************************************************/ +bool Check_Input_Stop_Bits(BYTE Date_Bits, BYTE Stop_Bits) +{ + if (Stop_Bits == 0 || Stop_Bits == 1 || Stop_Bits == 2) + { + if (Stop_Bits == 1 && (Date_Bits == 6 || Date_Bits == 7 || Date_Bits == 8)) + return false; + if (Stop_Bits == 2 && Date_Bits == 5) + return false; + } + else + return false; + return true; } /********************************************************************************************* @@ -150,9 +258,23 @@ unsigned int Input_Date_Bits(void) * Date_Bits=5时 Stop_Bits不能为2 * 返回值 : Stop_Bits 数据位 **********************************************************************************************/ -unsigned int Input_Stop_Bits(unsigned int Date_Bits) +BYTE Input_Stop_Bits(BYTE Date_Bits) { - + BYTE Stop_Bits; + int flage = 1; + printf("支持的数据位有:5,6,7,8 \n"); + do + { + if (flage == 1) + { + printf("请输入串口波特率:"); + flage = 0; + } + else + printf("请重新输入串口波特率:"); + cin >> Stop_Bits; + } while (!Check_Input_Stop_Bits(Date_Bits, Stop_Bits)); + return Stop_Bits; } /********************************************************************************************* @@ -161,7 +283,7 @@ unsigned int Input_Stop_Bits(unsigned int Date_Bits) * 输入 : 无 * 返回值 : Parity数据位 **********************************************************************************************/ -unsigned int Input_Parity(void) +BYTE Input_Parity(void) { } \ No newline at end of file