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

📄 comm1.cpp

📁 VC6++ program demo source for communication with PLC to read and write data.
💻 CPP
字号:
// comm.cpp: implementation of the comm class.
// Lop chua cac thu tuc truyen thong qua cong RS232C
// DANG THANH TRUNG 11/1999
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LinkPLC.h"
#include "comm1.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

comm::comm()
{

}

comm::~comm()
{
	CloseHandle(hComm);
}

/////////////////////////////////////////////////////////////////////

int comm::ReadComm( char *lpBuf,int so_byte)
{
//	LPDWORD lpErrors;
//	DWORD dwCommEvent;
//	LPCOMSTAT lpStat;
	DWORD nBytesRead = 0;;
	char chRead = 0x00;
	
	if (!SetCommMask(hComm,EV_RXCHAR))
	{
		MessageBox(NULL,"Error Setting Communications Event Mask", "Communication System Message", MB_OK);
		return 0;
	}

 	OVERLAPPED osWrite = {0};
 	
	TransmitCommChar(hComm,ENQ);
//	if (WaitCommEvent(hComm,&dwCommEvent,NULL))//Co 1 ky tu vao Buffer

int bResult=ReadFile(hComm, lpBuf,so_byte,&nBytesRead,NULL);
// MessageBox(NULL,"Error in ReadFile Call", "Communication System Message", MB_OK);
// Check for end of file. 


	FlushFileBuffers(hComm);


if  (bResult ==FALSE)
{ 
	return 0;
} 
 
 return nBytesRead;
}

/////////////////////////////////////////////////////////////////////

BOOL comm::WriteComm( char *lpBuf)
{
//	LPDWORD lpErrors;
//	DWORD dwCommEvent;
//	LPCOMSTAT lpStat;
	DWORD dwRead = 0;;
	char chRead = 0x00;
	char ReadStr[21]="";
	
	if (!SetCommMask(hComm,EV_RXCHAR))
	{
		MessageBox(NULL, "Error Setting Communications Event Mask", "Communication System Message", MB_OK);
		return FALSE;
	}

//	MessageBox(NULL,lpBuf, "Communication System Message", MB_OK);
 	
 	OVERLAPPED osWrite = {0};
 	DWORD dwWritten;
 	
//	TransmitCommChar(hComm,ACK);

//	if(WaitCommEvent(hComm,EV_CTS,NULL))
	{
		TransmitCommChar(hComm,ENQ);
		int n_iStrlen=strlen((char *)lpBuf);

		if(!WriteFile(hComm,lpBuf,n_iStrlen,&dwWritten, &osWrite))
			MessageBox(NULL,"Error in WriteFile Call", "Communication System Message", MB_OK);
	}


//	PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
	FlushFileBuffers(hComm);

return TRUE;

}

/////////////////////////////////////////////////////////////////////

BOOL comm::Connect(char *file, int m_BaudRate, int m_ByteSize, int m_Parity, int m_StopBits)
{
	hComm = CreateFile(file,GENERIC_READ | GENERIC_WRITE,
	0,0,OPEN_EXISTING,0,0);
	if (hComm == INVALID_HANDLE_VALUE)
	{
		MessageBox(NULL,"COM Opening Error", "Communication System Message", MB_OK);
		return 1;
	}//else
//		MessageBox(NULL,"COM Opening OK!", "Communication System Message", MB_OK);

	DCB dcb;
	FillMemory(&dcb, sizeof(dcb),0);
	if(!GetCommState(hComm, &dcb))
	{
		MessageBox(NULL,"Error in GetCommState", "Communication System Message", MB_OK);
		return 2;
	}

	dcb.BaudRate = m_BaudRate;
	dcb.ByteSize = m_ByteSize;
	dcb.Parity = m_Parity;
	dcb.StopBits = m_StopBits;
	
	if(!SetCommState(hComm, &dcb))
	{
		MessageBox(NULL,"Error in SetCommState", "Communication System Message", MB_OK);
		return 3;
	}
	// hoan tat connect
//	MessageBox(NULL,"SetCommState is OK", "Communication System Message", MB_OK);

	//Setting TimeOut
	COMMTIMEOUTS timeouts;
	timeouts.ReadIntervalTimeout = MAXDWORD;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutMultiplier = 0;
	timeouts.WriteTotalTimeoutConstant = 0;
	if (!SetCommTimeouts(hComm,&timeouts))
		MessageBox(NULL,"Error in Setting timeouts", "Communication System Message", MB_OK);
	
	return 0;

}

void comm::Disconnect()
{
	CloseHandle(hComm);
}

int comm::PlcRead(char *lpBuf)
{
	DWORD nBytesRead = 0;
	char chRead = 0x00;
	char sTempBuf[100];

	if (!SetCommMask(hComm,EV_RXCHAR))
	{
		MessageBox(NULL,"Error Setting Communications Event Mask", "Communication System Message", MB_OK);
		return 0;
	}

 	OVERLAPPED osWrite = {0};
 	
	TransmitCommChar(hComm,ENQ);

//	int bResult=ReadFile(hComm, lpBuf,so_byte,&nBytesRead,NULL);
	int bResult;
	int iCounter=0;
	do{
		do{
			bResult=ReadFile(hComm, sTempBuf+iCounter,1,&nBytesRead,NULL);
		}while (nBytesRead<1);

		iCounter++;

		if  (bResult ==FALSE)
		{ 
			MessageBox(NULL,"MS","jbfdsabfjasbfsajd",MB_OK);
			return 0;
		} 

		if(iCounter>=20)
		{
			MessageBox(NULL,"MS","jbfdsabfjasbfsajd",MB_OK);
			return 0;
		}

	}while (sTempBuf[iCounter-1]!=ETX);


	strncpy(lpBuf, sTempBuf,iCounter);

//	FlushFileBuffers(hComm);

	return iCounter;
}

⌨️ 快捷键说明

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