Selaa lähdekoodia

修改部分bug,RTU交互模块基本划分

Modbus_RTU
zcn1123 4 vuotta sitten
vanhempi
commit
7d93cc5a08
7 muutettua tiedostoa jossa 154 lisäystä ja 76 poistoa
  1. +4
    -2
      Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj
  2. +8
    -2
      Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters
  3. +72
    -1
      Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp
  4. +6
    -0
      Modbus_communication/Modbus_RTU_Salve/RTU_Salve.h
  5. +60
    -67
      Modbus_communication/Modbus_RTU_Salve/common.cpp
  6. +3
    -3
      Modbus_communication/Modbus_RTU_Salve/common.h
  7. +1
    -1
      Modbus_communication/Modbus_RTU_Salve/main.h

+ 4
- 2
Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj Näytä tiedosto

@@ -64,12 +64,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="common.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="RTU_common.cpp" />
<ClCompile Include="RTU_Salve.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="common.h" />
<ClInclude Include="main.h" />
<ClInclude Include="RTU_common.h" />
<ClInclude Include="RTU_Salve.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">


+ 8
- 2
Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters Näytä tiedosto

@@ -18,7 +18,10 @@
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="RTU_common.cpp">
<ClCompile Include="RTU_Salve.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="common.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
@@ -26,7 +29,10 @@
<ClInclude Include="main.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="RTU_common.h">
<ClInclude Include="RTU_Salve.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="common.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>

+ 72
- 1
Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp Näytä tiedosto

@@ -1 +1,72 @@
#include
#include "RTU_Salve.h"

char read_buf[MAX_NUMBER];



/*********************************************************************************************
* 功能     :  获取串口读取缓存区中数据的字节数
* 描述    : 向串口写入数据
* 返回值 : 缓存区中数据的字节数
* m_hComm : 串口句柄
********************************************************************************************/
unsigned int GetBytesInCOM(HANDLE m_hComm)
{
DWORD dwError = 0; /** 错误码 */
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
memset(&comstat, 0, sizeof(COMSTAT));

unsigned int BytesInQue = 0;
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
if (ClearCommError(m_hComm, &dwError, &comstat))
{
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
}
return BytesInQue;
}



int test(void)
{
string COMM;
cin >> COMM;
HANDLE Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), 9600, 8, 0, 1);
if (Handle_Com == INVALID_HANDLE_VALUE)
{
cout << "初始化串口失败" << endl;
getchar();
return 0;
}

char write_buf[MAX_NUMBER];
memset(write_buf, 0, MAX_NUMBER);
DWORD dwRead;
while (true)
{
//RTU主站,生成并发送请求,计时,读取响应报文,先发送后接收
//生成请求报文
//发送请求报文

PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存
while (1)
{
if (true == SendData(Handle_Com, "ok", 2))
{
cout << "发送请求报文成功" << endl;
break;
}
}
BOOL bReadOK = ReadFile(Handle_Com, read_buf, 256, &dwRead, NULL);
if (bReadOK && (dwRead > 0))
{
read_buf[dwRead] = '\0';
printf("收到响应:%s \r\n", read_buf);
}
else
cout << "loss" << endl;
}
CloseHandle(Handle_Com);
getchar();
return 0;
}

+ 6
- 0
Modbus_communication/Modbus_RTU_Salve/RTU_Salve.h Näytä tiedosto

@@ -0,0 +1,6 @@
#ifndef __RTU_SALVE_H
#define __RTU_SALVE_H

#include "common.h"

#endif

+ 60
- 67
Modbus_communication/Modbus_RTU_Salve/common.cpp Näytä tiedosto

@@ -1,6 +1,4 @@
#include "RTU_common.h"

char read_buf[MAX_NUMBER];
#include "common.h"

/*******************************************************************************************
 * 功能     : 打开串口
@@ -9,10 +7,6 @@ char read_buf[MAX_NUMBER];
* date_bits: 数据位(有效范围4~8)
* stop_bit : 停止位
* parity   : 奇偶校验。默认为无校验。NOPARITY 0; ODDPARITY 1;EVENPARITY 2

* dcb.StopBits= 0,1,2对应的是1bit,1.5bits,2bits.
* dcb.ByteSize=6,7,8时 dcb.StopBits不能为1
* dcb.ByteSize=5时 dcb.StopBits不能为2
********************************************************************************************/
HANDLE Init_COM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity)
{
@@ -70,7 +64,7 @@ HANDLE Init_COM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE
* m_hComm : 串口句柄
* data : 要写入的数据
* len : 写入数据的长度
********************************************************************************************/
**********************************************************************************************/
bool SendData(HANDLE m_hComm, char* data, int len)
{
if (m_hComm == INVALID_HANDLE_VALUE)
@@ -89,26 +83,11 @@ bool SendData(HANDLE m_hComm, char* data, int len)
}

/*********************************************************************************************
* 功能     :  获取串口读取缓存区中数据的字节数
* 描述    : 向串口写入数据
* 返回值 : 缓存区中数据的字节数
* m_hComm : 串口句柄
********************************************************************************************/
unsigned int GetBytesInCOM(HANDLE m_hComm)
{
DWORD dwError = 0; /** 错误码 */
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
memset(&comstat, 0, sizeof(COMSTAT));

unsigned int BytesInQue = 0;
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
if (ClearCommError(m_hComm, &dwError, &comstat))
{
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
}
return BytesInQue;
}

* 功能     :  Ms延时器
* 描述    : ms级准确延时
* 输入 : lTime 延时的ms数
* 返回值 : 无
**********************************************************************************************/
void MSleep(long lTime)
{
LARGE_INTEGER litmp;
@@ -129,46 +108,60 @@ void MSleep(long lTime)
} while (dfTim<dfSpec);
}

int test(void)
/*********************************************************************************************
* 功能     :  输入端口名称
* 描述    : 获取串口通信的端口名称
* 输入 : 无
* 返回值 : COMM 端口名
**********************************************************************************************/
string Input_COMM(void)
{
string COMM;
cin >> COMM;
HANDLE Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), 9600, 8, 0, 1);
if (Handle_Com == INVALID_HANDLE_VALUE)
{
cout << "初始化串口失败" << endl;
getchar();
return 0;
}
}

char write_buf[MAX_NUMBER];
memset(write_buf, 0, MAX_NUMBER);
DWORD dwRead;
while (true)
{
//RTU主站,生成并发送请求,计时,读取响应报文,先发送后接收
//生成请求报文
//发送请求报文
/*********************************************************************************************
* 功能     :  输入波特率
* 描述    : 获取串口通信的波特率
* 输入 : 无
* 返回值 : Boud_Rate 波特率
**********************************************************************************************/
unsigned int Input_Boud_Rate(void)
{

}

/*********************************************************************************************
* 功能     :  输入数据位
* 描述    : 获取串口通信的数据位个数
* 输入 : 无
* 返回值 : Date_Bits 数据位
**********************************************************************************************/
unsigned int Input_Date_Bits(void)
{

}

/*********************************************************************************************
* 功能     :  输入停止位
* 描述    : 获取串口通信的停止位
* 输入 : Date_Bits 数据位
* Stop_Bits= 0,1,2对应的是1bit,1.5bits,2bits.
* Date_Bits=6,7,8时 Stop_Bits不能为1
* Date_Bits=5时 Stop_Bits不能为2
* 返回值 : Stop_Bits 数据位
**********************************************************************************************/
unsigned int Input_Stop_Bits(unsigned int Date_Bits)
{

}

/*********************************************************************************************
* 功能     :  选择校验位
* 描述    : 获取串口通信的校验位
* 输入 : 无
* 返回值 : Parity数据位
**********************************************************************************************/
unsigned int Input_Parity(void)
{

PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存
while (1)
{
if (true == SendData(Handle_Com, "ok", 2))
{
cout << "发送请求报文成功" << endl;
break;
}
}
BOOL bReadOK = ReadFile(Handle_Com, read_buf, 256, &dwRead, NULL);
if (bReadOK && (dwRead > 0))
{
read_buf[dwRead] = '\0';
printf("收到响应:%s \r\n", read_buf);
}
else
cout << "loss" << endl;
}
CloseHandle(Handle_Com);
getchar();
return 0;
}

+ 3
- 3
Modbus_communication/Modbus_RTU_Salve/common.h Näytä tiedosto

@@ -1,5 +1,5 @@
#ifndef __RTU_COMMON_H
#define __RTU_COMMON_H
#ifndef __COMMON_H
#define __COMMON_H

#include <Windows.h>
#include <string>
@@ -8,5 +8,5 @@ using namespace std;

#define MAX_NUMBER 300

int test();
#endif

+ 1
- 1
Modbus_communication/Modbus_RTU_Salve/main.h Näytä tiedosto

@@ -1,6 +1,6 @@
#ifndef __MAIN_H
#define __MAIN_H

#include "RTU_common.h"
#include "RTU_Salve.h"

#endif

Ladataan…
Peruuta
Tallenna