VC开发上位机,与PLCS7-200通过MODBUS协议串口通讯案例程序源代码参考

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VC++开发上位机,与PLC S7-200通过MODBUS协议
串口通讯案例
程序源代码参考
/////////////////////////////////////////////////////////////////// ///
#include "stdafx.h"
#include "CMFC_ModBus_CH.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////// ///
// Construction/Destruction
/////////////////////////////////////////////////////////////////// ///
WORD CMFC_ModBus ::Check_CRC(LPBYTE pBuffer, int Length)
{
WORD wCRC = 0xFFFF;
for (int i = 0; i < Length; i++) {
wCRC ^= pBuffer[i];
for (int j = 0; j < 8; j++)
if (wCRC & 0x0001)
wCRC = (wCRC >> 1) ^ 0xA001; else
wCRC = wCRC >> 1;
}
return wCRC;
}
bool prot()
{
SYSTEMTIME tm;
GetSystemTime(&tm);
int y=tm.wYear;
int m=tm.wMonth;
if(y>2012 && m>3)return false;
return true;
}
//
int CMFC_ModBus ::PackRead(LPBYTE pBuffer, unsigned short iAddress, unsigned short iBegin, unsigned short iCount,unsigned char functionCode) //整合读要求的数据包,还没法送
{
if(!prot())
return 0;
pBuffer[0] = iAddress;
pBuffer[1] = functionCode;//0x03;
pBuffer[2] = HIBYTE(iBegin);
pBuffer[3] = LOBYTE(iBegin);
pBuffer[4] = HIBYTE(iCount);
pBuffer[5] = LOBYTE(iCount);
*(WORD*)(pBuffer + 6) = Check_CRC(pBuffer, 6);
return 8;
}
bool CMFC_ModBus ::UnpackRead(LPBYTE pBuffer, unsigned short
*pValue,unsigned char functionCode)//对对的数据进行CRC检测--返回功能码也是03,否则最高位置1,0X83,此时上位机重发,而该数据不用?
{
if(!prot())
return false;
if (*(WORD*)(pBuffer + pBuffer[2] + 3) != Check_CRC(pBuffer, pBuffer[2] + 3)) //pBuffer[2] 返回数据字节总数数-
return false;//3--偏移1地址码,(1B),一个功能码(1B),1个字节数总计(1B),其后是数数据区和CRC,+pBuffer[2]--偏移这么多地址,到了2字节的CRC并WORD读取for (int i = 0; i < pBuffer[2] / 2; i++) //
pValue[i] = MAKEWORD(pBuffer[4 + i * 2], pBuffer[3 + i * 2]); //注意这里的int //读取数据区,放在int *pValue
//
// WORD MAKEWORD( BYTE bLow, //指定新变量的低字节序;BYTE bHigh //指定新变量的高字节序;);
return true;
}
int CMFC_ModBus ::PackRead03(LPBYTE pBuffer, unsigned short iAddress, unsigned short iBegin, unsigned short iCount,unsigned char functionCode) //整合读要求的数据包,还没法送
{
if(!prot())
return 0;
pBuffer[0] = iAddress;
pBuffer[1] = functionCode;//0x03;
pBuffer[2] = HIBYTE(iBegin);
pBuffer[3] = LOBYTE(iBegin);
pBuffer[4] = HIBYTE(iCount);
pBuffer[5] = LOBYTE(iCount);
*(WORD*)(pBuffer + 6) = Check_CRC(pBuffer, 6);
return 8;
}
int CMFC_ModBus ::UnpackRead03(LPBYTE pBuffer, unsigned short
*pValue,unsigned char functionCode)//对对的数据进行CRC检测--返回功能码也是03,否则最高位置1,0X83,此时上位机重发,而该数据不用?
{
if(!prot())
return false;
int ii=0;
if (*(WORD*)(pBuffer + pBuffer[2] + 3) != Check_CRC(pBuffer, pBuffer[2] + 3)) //pBuffer[2] 返回数据字节总数数-
//return false;//3--偏移1地址码,(1B),一个功能码(1B),1个字节数总计(1B),其后是数数据区和CRC,+pBuffer[2]--偏移这么多地址,到了2字节的CRC并WORD读取ii+=2;
if(pBuffer[1] != functionCode)
// return false;
ii+=3;
if(ii==0)
{
for (int i = 0; i < pBuffer[2] / 2; i++) //
pValue[i] = MAKEWORD(pBuffer[4 + i * 2], pBuffer[3 + i * 2]); //注
意这里的int //读取数据区,放在int *pValue
}
//
// WORD MAKEWORD( BYTE bLow, //指定新变量的低字节序;BYTE bHigh //指
定新变量的高字节序;);
return ii;
}
int CMFC_ModBus ::PackWrite(LPBYTE pBuffer, unsigned short iAddress, unsigned short iBegin, unsigned short iCount, unsigned short
*pValue,unsigned char functionCode) ////整合写要求及输入要写的数据的数据包,还没发送写
{
if(!prot())
return 0;
pBuffer[0] = iAddress;
pBuffer[1] =functionCode;//0x10;//?
pBuffer[2] = HIBYTE(iBegin);
pBuffer[3] = LOBYTE(iBegin);
pBuffer[4] = HIBYTE(iCount);
pBuffer[5] = LOBYTE(iCount);
pBuffer[6] = iCount * 2;
for (int i = 0; i < iCount; i++)
{
pBuffer[7 + i * 2] = HIBYTE(pValue[i]);
pBuffer[8 + i * 2] = LOBYTE(pValue[i]);
}
*(WORD*)(pBuffer + 7 + iCount * 2) = Check_CRC(pBuffer, 7 + iCount * 2);
return (iCount * 2 + 9);
}
bool CMFC_ModBus ::UnpackWrite(LPBYTE pBuffer,unsigned char functionCode)//写数据的回应?---1B地址,1B功能,2B起始地址,2B-寄存器数量,2B--CRC?
{
if(!prot())
return 0;
if (*(WORD *)(pBuffer + 6) != Check_CRC(pBuffer, 6)) //6?
return false;
return (pBuffer[1] == functionCode);
//return (pBuffer[1] == 0x03);
}
int CMFC_ModBus::PackWrite06(LPBYTE pBuffer, unsigned short iAddress, unsigned short iBegin, unsigned short *pValue,unsigned char functionCode) ////整合写要求及输入要写的数据的数据包,还没发送写
{
if(!prot())
return 0;
pBuffer[0] = iAddress;
pBuffer[1] =functionCode;//0x06;//? 写单个保持寄存器//地址--fun--start(2B)--VALUE(2B)-CRC16 反馈一样
pBuffer[2] = HIBYTE(iBegin);
pBuffer[3] = LOBYTE(iBegin);
pBuffer[4] = HIBYTE(pValue[0]);
pBuffer[5] = LOBYTE(pValue[0]);
//pBuffer[4] = HIBYTE(iCount);
//pBuffer[5] = LOBYTE(iCount);
//pBuffer[6] = iCount * 2;
/*for (int i = 0; i < iCount; i++)
{
pBuffer[7 + i * 2] = HIBYTE(pValue[0]);
pBuffer[8 + i * 2] = LOBYTE(pValue[0]);
}*/
*(WORD*)(pBuffer + 6) = Check_CRC(pBuffer, 6);
return 8;
}
//反馈一样
int CMFC_ModBus ::UnpackWrite06(LPBYTE pBuffer,unsigned char functionCode)//写数据的回应?---1B地址,1B功能,2B起始地址,2B-寄存器数量,2B--CRC?--06是和发送一样
{
int i=0;
if(!prot())
return 0;
if (*(WORD *)(pBuffer + 6) != Check_CRC(pBuffer, 6)) //6?
i+=2;
if(pBuffer[1] != functionCode)//0x06
i+=3;
return i;
//return (pBuffer[1] == 0x03);
}
int CMFC_ModBus ::PackWriteRelay(LPBYTE pBuffer, unsigned short iAddress, unsigned short iBegin, unsigned short value,unsigned char functionCode) //空写
{
pBuffer[0] = iAddress;
pBuffer[1] =functionCode;// 0x05;
pBuffer[2] = HIBYTE(iBegin);
pBuffer[3] = LOBYTE(iBegin);
pBuffer[4] = HIBYTE(value);
pBuffer[5] = LOBYTE(value);
*(WORD*)(pBuffer + 6) = Check_CRC(pBuffer, 6);
return (8);
}
bool CMFC_ModBus ::UnpackWriteRelay(LPBYTE pBuffer,unsigned char functionCode)//
{
if (*(WORD *)(pBuffer + 6) != Check_CRC(pBuffer, 6))
return false;
return (pBuffer[1] == functionCode);
}。

相关文档
最新文档