⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmbrtu.cpp

📁 实现了modbus RTU通讯协议的COM组件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	memcpy(cReceive, cReceive + 3, iLen);

	*pRetLen = iLen;

	return S_OK;
}

STDMETHODIMP CCMBRTU::ReadCoilsStatus(int iIndex, BOOL *pCoilStauts)
{
	// TODO: Add your implementation code here
	if (((BYTE)(cReceive[iIndex / 8]) & (Power(2, iIndex % 8))) == (Power(2, iIndex % 8)))
		*pCoilStauts = TRUE;
	else *pCoilStauts = FALSE;

	return S_OK;
}

STDMETHODIMP CCMBRTU::WriteCoilsStatus(int iIndex, BOOL bCoilStatus)
{
	// TODO: Add your implementation code here
	if (bCoilStatus)
	{
		cWrite[iIndex / 8] = ((BYTE)(cWrite[iIndex / 8]) | (Power(2, iIndex % 8)));
	}
	else
	{
		cWrite[iIndex / 8] = ((BYTE)(cWrite[iIndex / 8]) & ((Power(2, iIndex % 8)) ^ 0xFF));
	}
	return S_OK;
}

STDMETHODIMP CCMBRTU::WriteCoils(int iStation, int iStartAddr, int iQuantity)
{
	// TODO: Add your implementation code here
	BYTE cWriteCmd[2048];
	memset(cWriteCmd, 0, 2048);
	int iLength = iQuantity / 8;
	if (iQuantity % 8 > 0)
		iLength++;
	
	cWriteCmd[0]  = iStation;
	cWriteCmd[1]  = 15;
	cWriteCmd[2]  = iStartAddr / 256;
	cWriteCmd[3]  = iStartAddr % 256;
	cWriteCmd[4]  = iQuantity / 256;
	cWriteCmd[5]  = iQuantity % 256;
	cWriteCmd[6]  = iLength;

	for(int i = 0; i < iLength; i++)
	{
		cWriteCmd[i + 7] = cWrite[i];
	}

	CCrc crc;
	crc.CRC16((unsigned char *)cWriteCmd, 7 + iLength, cWriteCmd[7 + iLength], cWriteCmd[7 + iLength + 1]);

	DWORD dwTotal = 8;

	DWORD dwEvent;
	SetCommMask(hCom, EV_TXEMPTY);
	DWORD dwTick = GetTickCount();
	DWORD dwWrite = 0;
	DWORD dwRead = 0;
	DWORD dwTemp = 0;

	DWORD dwWriteLen = 7 + iLength + 2;
	if (WriteFile(hCom, cWriteCmd, dwWriteLen, &dwWrite, NULL))
	{
		WaitCommEvent(hCom, &dwEvent, NULL);
		memset(cReceive, 0, 2048);
		while(TRUE)
		{
			if(dwWrite == dwWriteLen)
			{
				ClearCommError(hCom,&dwError,&cs); 
				if((!cs.cbInQue && dwTemp == dwTotal ) || (GetTickCount() > dwTick + 5000))
					break;
				dwRead = 0;
				ReadFile(hCom, cReceive + dwTemp, cs.cbInQue, &dwRead, NULL);
				dwTemp += dwRead;
			}
		}
	}

	if (dwTemp != dwTotal)
		return S_FALSE;

	if (cReceive[1] != 15)
		return S_FALSE;

	return S_OK;
}

STDMETHODIMP CCMBRTU::WriteCoil(int iStation, int iStartAddr, BOOL bCoilStatus)
{
	// TODO: Add your implementation code here
	BYTE cWriteCmd[8];
	memset(cWriteCmd, 0, 8);
	short iVal;
	if (bCoilStatus)
		iVal = (short)0xFF00;
	else iVal = (short)0x0000;
	
	cWriteCmd[0]  = iStation;
	cWriteCmd[1]  = 5;
	cWriteCmd[2]  = iStartAddr / 256;
	cWriteCmd[3]  = iStartAddr % 256;
	cWriteCmd[4]  = iVal / 256;
	cWriteCmd[5]  = iVal % 256;
	
	CCrc crc;
	crc.CRC16((unsigned char *)cWriteCmd, 6, cWriteCmd[6], cWriteCmd[7]);

	DWORD dwTotal = 8;

	DWORD dwEvent;
	SetCommMask(hCom, EV_TXEMPTY);
	DWORD dwTick = GetTickCount();
	DWORD dwWrite = 0;
	DWORD dwRead = 0;
	DWORD dwTemp = 0;

	DWORD dwWriteLen = 8;
	if (WriteFile(hCom, cWriteCmd, dwWriteLen, &dwWrite, NULL))
	{
		WaitCommEvent(hCom, &dwEvent, NULL);
		memset(cReceive, 0, 2048);
		while(TRUE)
		{
			if(dwWrite == dwWriteLen)
			{
				ClearCommError(hCom,&dwError,&cs); 
				if((!cs.cbInQue && dwTemp == dwTotal ) || (GetTickCount() > dwTick + 5000))
					break;
				dwRead = 0;
				ReadFile(hCom, cReceive + dwTemp, cs.cbInQue, &dwRead, NULL);
				dwTemp += dwRead;
			}
		}
	}

	if (dwTemp != dwTotal)
		return S_FALSE;

	if (cReceive[1] != 5)
		return S_FALSE;

	return S_OK;
}

STDMETHODIMP CCMBRTU::ReadDiscretes(int iStation, int iStartAddr, int iQuantity, int *pRetLen)
{
	// TODO: Add your implementation code here
	BYTE cReadCmd[8];
	memset(cReadCmd, 0, 8);
	
	cReadCmd[0]  = iStation;
	cReadCmd[1]  = 2;
	cReadCmd[2]  = iStartAddr / 256;
	cReadCmd[3]  = iStartAddr % 256;
	cReadCmd[4]  = iQuantity / 256;
	cReadCmd[5]  = iQuantity % 256;

	CCrc crc;
	crc.CRC16((unsigned char *)cReadCmd, 6, cReadCmd[6], cReadCmd[7]);
	int iLen = 0;
	iLen = iQuantity / 8;
	if (iQuantity % 8)
		iLen++;

	DWORD dwTotal = iLen + 5;

	DWORD dwEvent;
	SetCommMask(hCom, EV_TXEMPTY);
	DWORD dwTick = GetTickCount();
	DWORD dwWrite = 0;
	DWORD dwRead = 0;
	DWORD dwTemp = 0;

	if (WriteFile(hCom, cReadCmd, 8, &dwWrite, NULL))
	{
		WaitCommEvent(hCom, &dwEvent, NULL);
		memset(cReceive, 0, 2048);
		while(TRUE)
		{
			if(dwWrite == 8)
			{
				ClearCommError(hCom,&dwError,&cs); 
				if((!cs.cbInQue && dwTemp == dwTotal ) || (GetTickCount() > dwTick + 5000))
					break;
				dwRead = 0;
				ReadFile(hCom, cReceive + dwTemp, cs.cbInQue, &dwRead, NULL);
				dwTemp += dwRead;
			}
		}
	}

	if (dwTemp != dwTotal)
		return S_FALSE;

	if (cReceive[1] != 2)
		return S_FALSE;

	memcpy(cReceive, cReceive + 3, iLen);

	*pRetLen = iLen;


	return S_OK;
}

STDMETHODIMP CCMBRTU::FourByteToInt(BYTE byteOneHi, BYTE byteOneLo, BYTE byteTwoHi, BYTE byteTwoLo, int *piVal)
{
	// TODO: Add your implementation code here
	BYTE byteVal[4];
	byteVal[0] = byteOneLo;
	byteVal[1] = byteOneHi;
	byteVal[2] = byteTwoLo;
	byteVal[3] = byteTwoHi;

	memcpy(piVal, byteVal, 4);
	return S_OK;
}

STDMETHODIMP CCMBRTU::IntToFourByte(int iVal, BYTE *pByteOneHi, BYTE *pByteOneLo, BYTE *pByteTwoHi, BYTE *pByteTwoLo)
{
	// TODO: Add your implementation code here
	BYTE byteVal[4];
	memcpy(byteVal, &iVal, 4);

	*pByteOneHi = byteVal[1];
	*pByteOneLo = byteVal[0];
	*pByteTwoHi = byteVal[3];
	*pByteTwoLo = byteVal[2];
	return S_OK;
}

STDMETHODIMP CCMBRTU::FourByteToFloat(BYTE byteOneHi, BYTE byteOneLo, BYTE byteTwoHi, BYTE byteTwoLo, float *pfVal)
{
	// TODO: Add your implementation code here
	BYTE byteVal[4];
	byteVal[0] = byteOneLo;
	byteVal[1] = byteOneHi;
	byteVal[2] = byteTwoLo;
	byteVal[3] = byteTwoHi;

	memcpy(pfVal, byteVal, 4);
	return S_OK;
}

STDMETHODIMP CCMBRTU::FloatToFourByte(float fVal, BYTE *pByteOneHi, BYTE *pByteOneLo, BYTE *pByteTwoHi, BYTE *pByteTwoLo)
{
	// TODO: Add your implementation code here
	BYTE byteVal[4];
	memcpy(byteVal, &fVal, 4);

	*pByteOneHi = byteVal[1];
	*pByteOneLo = byteVal[0];
	*pByteTwoHi = byteVal[3];
	*pByteTwoLo = byteVal[2];
	return S_OK;
}

STDMETHODIMP CCMBRTU::ReadRegFloat(int iStation, int iStartAddr, float *pFloat)
{
	// TODO: Add your implementation code here
	int iLen;
	ReadRegister(iStation, iStartAddr, 2, &iLen);
	if(iLen != 4)
		return S_FALSE;
	FourByteToFloat(cReceive[0], cReceive[1], cReceive[2], cReceive[3], pFloat);
	return S_OK;
}

STDMETHODIMP CCMBRTU::ReadRegInt(int iStation, int iStartAddr, int *piVal)
{
	// TODO: Add your implementation code here
	int iLen;
	ReadRegister(iStation, iStartAddr, 2, &iLen);
	if(iLen != 4)
		return S_FALSE;
	FourByteToInt(cReceive[0], cReceive[1], cReceive[2], cReceive[3], piVal);
	return S_OK;
}

STDMETHODIMP CCMBRTU::ReadRegShort(int iStation, int iStartAddr, short *psVal)
{
	// TODO: Add your implementation code here
	int iLen;
	ReadRegister(iStation, iStartAddr, 1, &iLen);
	if(iLen != 2)
		return S_FALSE;
	TwoByteToShort(cReceive[0], cReceive[1], psVal);
	return S_OK;
}

STDMETHODIMP CCMBRTU::WriteRegFloat(int iStation, int iStartAddr, float fVal)
{
	// TODO: Add your implementation code here
	memset(cWrite, 0, 4);
	FloatToFourByte(fVal, (unsigned char *)(cWrite), 
		            (unsigned char *)(cWrite + 1), 
					(unsigned char *)(cWrite + 2),
					(unsigned char *)(cWrite + 3));

	return WriteRegister(iStation, iStartAddr, 2);
}

STDMETHODIMP CCMBRTU::WriteRegInt(int iStation, int iStartAddr, int iVal)
{
	// TODO: Add your implementation code here
	memset(cWrite, 0, 4);
	IntToFourByte(iVal, (unsigned char *)(cWrite), 
		            (unsigned char *)(cWrite + 1), 
					(unsigned char *)(cWrite + 2),
					(unsigned char *)(cWrite + 3));

	return WriteRegister(iStation, iStartAddr, 2);
}

STDMETHODIMP CCMBRTU::WriteRegShort(int iStation, int iStartAddr, short sVal)
{
	// TODO: Add your implementation code here
	memset(cWrite, 0, 2);
	ShortToTwoByte(sVal, (unsigned char *)(cWrite),
						 (unsigned char *)(cWrite + 1));

	return WriteRegister(iStation, iStartAddr, 1);
}

BYTE CCMBRTU::Power(BYTE iJiShu, BYTE byteMi)
{
	BYTE byteReturn = 1;
	/*for(BYTE i = 0; i < byteMi; i++)
	{
		byteReturn *= iJiShu;
	}*/
	byteReturn <<= byteMi;

	return byteReturn;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -