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

📄 serialcomdll.cpp

📁 三菱FX系列PLC密码读取程序 通过PLC编程线连接电脑串口
💻 CPP
字号:
// SerialComDll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "SerialComDll.h"

BOOL	blOpened= false;
#ifdef	NO_HANDLE
HANDLE	hCom= NULL;	
#endif
//DWORD  readBufferSizes;
//DWORD  writeBufferSizes;
DCB			myDCB;
OVERLAPPED	Wol;
OVERLAPPED	Rol;
DWORD		timeout= SERIAL_READ_TIMEOUT ;

C_DLLEXPORT
void SetTimeOutRW(DWORD time)
{
	timeout= time;
}

#ifndef	NO_HANDLE

C_DLLEXPORT	
int Setup(HANDLE hCom, DWORD baudRate, BYTE parity, BYTE datasize, BYTE stopbits, DWORD timeOut, DWORD readBufferSizes, DWORD writeBufferSizes)
{
	if(!blOpened || hCom==NULL)	{AfxMessageBox("Open COM first"); return -1;}
		COMMTIMEOUTS	comtimeout;
		comtimeout.ReadTotalTimeoutConstant= timeOut;//SERIAL_READ_TIMEOUT;
		comtimeout.ReadTotalTimeoutMultiplier= 1;
		comtimeout.WriteTotalTimeoutConstant= timeOut;//SERIAL_WRITE_TIMEOUT;		
		comtimeout.WriteTotalTimeoutMultiplier= 1;
		SetCommTimeouts(hCom, &comtimeout);

		SetupComm(hCom,readBufferSizes,writeBufferSizes);
		if(GetCommState(hCom,&myDCB)==0) 
		{
//			DWORD errorCode= GetLastError();
			AfxMessageBox("Get comm state error");
			return -1;
		}
		//GetCommState(hCom,&myDCB);
		myDCB.BaudRate	= baudRate;
		myDCB.fBinary	= TRUE;
		myDCB.fParity	= TRUE;
		myDCB.ByteSize	= datasize;
		myDCB.Parity	= parity;
		myDCB.StopBits	= stopbits;

		if(SetCommState(hCom,&myDCB)==0) 
		{
//			DWORD errorCode= GetLastError();
			AfxMessageBox("Set comm state error");
			return -1;
		}
	return 0;
}

/*
overlapped com operation
*/
C_DLLEXPORT
HANDLE Open(char* comPort)
{
	HANDLE	hCom;
	if(blOpened)	return 0;	//have open comm port
	hCom= CreateFile((LPCTSTR)comPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
	if(hCom!= INVALID_HANDLE_VALUE)
	{
		blOpened= true;
		//if(Setup(9600, NOPARITY, 8, ONESTOPBIT)<0) return -1;
		if(Setup(hCom)<0) return NULL;
		
//		AfxMessageBox("Open Com Port Sucessfully!");
	}
	else
	{
		blOpened= false;
		AfxMessageBox("Can't open Com Port!");
		return NULL;
	}
	return hCom;
}

C_DLLEXPORT
int Close(PHANDLE hCom)
{
	if((*hCom)!=NULL && blOpened)
	{
		CloseHandle(*hCom);
		*hCom=NULL;
		blOpened= false;
//		AfxMessageBox("Close Com Port Sucessfully!");
	}
	else
	{
//		AfxMessageBox("Com Port Don't Open!");
		return -1;
	}
	return 0;
}

C_DLLEXPORT
int Read(HANDLE hCom, char *pbuffer)
{
	int ret= 0;
	if(blOpened && hCom!=NULL)
	{
		DWORD	rdBytes=0;
		OVERLAPPED Rol;
		COMSTAT	comstatus;
		ClearCommError(hCom, NULL, &comstatus);
		if(comstatus.cbInQue<=0)	return 0;

		if(pbuffer!=NULL)
		{	
			Rol.Internal=0;
			Rol.InternalHigh=0;
			Rol.Offset=0;
			Rol.OffsetHigh=0;
			Rol.hEvent= CreateEvent(NULL,true,false,NULL);
			if(Rol.hEvent==NULL)	{AfxMessageBox("Create Rol.hEvent error!"); return -1;}

			if(ReadFile(hCom,pbuffer,comstatus.cbInQue,&rdBytes, &Rol))
			{
				//AfxMessageBox("Serial Communication read error");
				//return -2;
				//read sucessfully
				ret= rdBytes;
			}
			else
			{
				DWORD dwRes, dwRead;
				dwRes= WaitForSingleObject(Rol.hEvent, timeout);	//timeout 500ms
				switch(dwRes)
				{
				case WAIT_OBJECT_0:
					if(!GetOverlappedResult(hCom, &Rol,&dwRead, true))
					{
						//GetLastError()
						ret= -3;
					}
					else
					{
						//read ok
						ret= dwRead;
					}
					break;
				case WAIT_TIMEOUT:
					//AfxMessageBox("Serial Read Timeout");
					ret= RW_TIMEOUT;
					break;
				default:
					//default opreation
					break;
				}
			}
			CloseHandle(Rol.hEvent);

			//ret= rdBytes;
			//Beep(1000,10);
		}
		else 
		{
			AfxMessageBox("pbuffer point error!");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}

/*
overlapped com operation
*/
C_DLLEXPORT
int Write(HANDLE hCom, char *pbuffer, DWORD bytes)
{
	//if(strcmp(pbuffer, "")==0)	{AfxMessageBox("The buffer is empty"); return -1;}
	int ret= 0;
	if(blOpened && hCom!=NULL)
	{
		DWORD	wrBytes=0;
		OVERLAPPED Wol;
		if(strlen(pbuffer)>0)
		{			
			Wol.Internal=0;
			Wol.InternalHigh=0;
			Wol.Offset=0;
			Wol.OffsetHigh=0;
			Wol.hEvent= CreateEvent(NULL,true,false,NULL);
			if(Wol.hEvent==NULL)	{AfxMessageBox("Create Wol.hEvent error!"); return -1;}
			if(WriteFile(hCom,pbuffer,bytes,&wrBytes, &Wol))
			{
				ret= wrBytes;
			}
			else
			{
				DWORD dwRes, dwWrite;
				dwRes= WaitForSingleObject(Wol.hEvent, timeout);	//timeout 500ms
				switch(dwRes)
				{
				case WAIT_OBJECT_0:
					if(!GetOverlappedResult(hCom, &Wol,&dwWrite, true))
					{
						//GetLastError()
						if(GetLastError()==ERROR_IO_PENDING) AfxMessageBox("ERROR_IO_PENDING");
						else {CString msg; msg.Format("error: %d", GetLastError());  AfxMessageBox(msg);}
						ret= -3;
					}
					else
					{
						//send ok
						ret= dwWrite;
					}
					break;
				case WAIT_TIMEOUT:
					//AfxMessageBox("Serial Write Timeout");
					ret= RW_TIMEOUT;
					break;
				default:
					//default opreation
					break;
				}
			}
			CloseHandle(Wol.hEvent);

			//Beep(1000,10);

			//AfxMessageBox("Send Message \""+m_SendMessage+"\" Ok!");
			//m_ShowMessage= m_SendMessage;
		}
		else 
		{
			AfxMessageBox("pbuffer is empty!\n");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!\n");
		return -1;
	}
	return ret;
}

/*
normal com operation 
*/
C_DLLEXPORT
HANDLE Open_NoOVERLAPPED(char *comPort)
{
	HANDLE hCom;
	if(blOpened)	return 0;	//have open comm port
	hCom= CreateFile((LPCTSTR)comPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,NULL);
	if(hCom!= INVALID_HANDLE_VALUE)
	{
		//if(Setup(9600, NOPARITY, 8, ONESTOPBIT)<0) return -1;
		blOpened= true;
		if(Setup(hCom)<0) return NULL;
//		AfxMessageBox("Open Com Port Sucessfully!");
	}
	else
	{
		blOpened= false;
		AfxMessageBox("Can't open Com Port!");
		return NULL;
	}
	return hCom;
}

C_DLLEXPORT
int Read_NoOVERLAPPED(HANDLE hCom, char *pbuffer)
{
	int ret= 0;
	if(blOpened && hCom!=NULL)
	{
		DWORD	rdBytes=0;
		COMSTAT	comstatus;
		ClearCommError(hCom, NULL, &comstatus);
//		OVERLAPPED Rol;
		if(pbuffer!=NULL)
		{			
			if(comstatus.cbInQue>0)
			{
				if(ReadFile(hCom,pbuffer,comstatus.cbInQue,&rdBytes, NULL)==0)
				{
					AfxMessageBox("Serial Communication read error");
					ret= -3;
				}
				else
					ret= rdBytes;
			}
		}
		else 
		{
			AfxMessageBox("pbuffer point error!");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}

/*
normal com operation
*/
C_DLLEXPORT
int Write_NoOVERLAPPED(HANDLE hCom, char *pbuffer, DWORD bytes)
{
	//f(strcmp(pbuffer, "")==0)	{AfxMessageBox("buffer is empty"); return -1;}

	int ret= 0;
	if(blOpened && hCom!=NULL)
	{
		DWORD	wrBytes=0;
		if(strlen(pbuffer)>0)
		{
			if(WriteFile(hCom,pbuffer,bytes,&wrBytes, NULL)==0)
			{
//				DWORD errorCode= GetLastError();
				AfxMessageBox("Serial Communication write error");
				ret= -2;
			}
			else
				ret= wrBytes;
		}
		else
		{
			AfxMessageBox("pbuffer is empty");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}


C_DLLEXPORT
DWORD CheckInBuffer(HANDLE hCom)
{
	if(blOpened)	return -1;

	COMSTAT	comstatus;
	ClearCommError(hCom, NULL, &comstatus);

	return comstatus.cbInQue;
}

C_DLLEXPORT
DWORD CheckOutBuffer(HANDLE hCom)
{
	if(blOpened)	return -1;

	COMSTAT	comstatus;
	ClearCommError(hCom, NULL, &comstatus);

	return comstatus.cbOutQue;
}

#else

C_DLLEXPORT	
int Setup(DWORD baudRate, BYTE parity, BYTE datasize, BYTE stopbits, DWORD timeOut, DWORD readBufferSizes, DWORD writeBufferSizes)
{
	if(!blOpened)	{AfxMessageBox("Open COM first"); return -1;}
		COMMTIMEOUTS	comtimeout;
		comtimeout.ReadTotalTimeoutConstant= timeOut;//SERIAL_READ_TIMEOUT;
		comtimeout.ReadTotalTimeoutMultiplier= 1;
		comtimeout.WriteTotalTimeoutConstant= timeOut;//SERIAL_WRITE_TIMEOUT;		
		comtimeout.WriteTotalTimeoutMultiplier= 1;
		SetCommTimeouts(hCom, &comtimeout);

		SetupComm(hCom,readBufferSizes,writeBufferSizes);
		if(GetCommState(hCom,&myDCB)==0) 
		{
//			DWORD errorCode= GetLastError();
			AfxMessageBox("Get comm state error");
			return -1;
		}
		//GetCommState(hCom,&myDCB);
		myDCB.BaudRate	= baudRate;
		myDCB.fBinary	= TRUE;
		myDCB.fParity	= TRUE;
		myDCB.ByteSize	= datasize;
		myDCB.Parity	= parity;
		myDCB.StopBits	= stopbits;

		if(SetCommState(hCom,&myDCB)==0) 
		{
			DWORD errorCode= GetLastError();
			AfxMessageBox("Set comm state error");
			return -1;
		}
	return 0;
}

/*
overlapped com operation
*/
C_DLLEXPORT
int Open(char* comPort)
{
	if(blOpened)	return 0;	//have open comm port

	hCom= CreateFile((LPCTSTR)comPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
	if(hCom!= INVALID_HANDLE_VALUE)
	{
		//if(Setup(9600, NOPARITY, 8, ONESTOPBIT)<0) return -1;
		blOpened= true;
		if(Setup()<0) return -1;
		
//		AfxMessageBox("Open Com Port Sucessfully!");
	}
	else
	{
		blOpened= false;
		CString msg;	msg.Format("Can't open Port: %s!", comPort);
		AfxMessageBox(msg);
		return -1;
	}
	return 0;
}

C_DLLEXPORT
int Close()
{
	if(hCom!=NULL && blOpened)
	{
		CloseHandle(hCom);
		hCom=NULL;
		blOpened= false;
//		AfxMessageBox("Close Com Port Sucessfully!");
	}
	else
	{
//		AfxMessageBox("Com Port Don't Open!");
		return -1;
	}
	return 0;
}

C_DLLEXPORT
int Read(char *pbuffer)
{
	int ret= 0;
	if(blOpened)
	{
		DWORD	rdBytes=0;
		OVERLAPPED Rol;
		COMSTAT	comstatus;
		ClearCommError(hCom, NULL, &comstatus);
		if(comstatus.cbInQue<=0)	return 0;

		if(pbuffer!=NULL)
		{	
			Rol.Internal=0;
			Rol.InternalHigh=0;
			Rol.Offset=0;
			Rol.OffsetHigh=0;
			Rol.hEvent= CreateEvent(NULL,true,false,NULL);
			if(Rol.hEvent==NULL)	{AfxMessageBox("Create Rol.hEvent error!"); return -1;}

			if(ReadFile(hCom,pbuffer,comstatus.cbInQue,&rdBytes, &Rol))
			{
				//AfxMessageBox("Serial Communication read error");
				//return -2;
				//read sucessfully
				ret= rdBytes;
			}
			else
			{
				DWORD dwRes, dwRead;
				dwRes= WaitForSingleObject(Rol.hEvent, SERIAL_READ_TIMEOUT);	//timeout 500ms
				switch(dwRes)
				{
				case WAIT_OBJECT_0:
					if(!GetOverlappedResult(hCom, &Rol,&dwRead, true))
					{
						//GetLastError()
						ret= -3;
					}
					else
					{
						//read ok
						ret= dwRead;
					}
					break;
				case WAIT_TIMEOUT:
					AfxMessageBox("Serial Read Timeout");
					ret= -4;
					break;
				default:
					//default opreation
					break;
				}
			}
			CloseHandle(Rol.hEvent);

			//ret= rdBytes;
			//Beep(1000,10);
		}
		else 
		{
			AfxMessageBox("pbuffer point error!");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}

/*
overlapped com operation
*/
C_DLLEXPORT
int Write(char *pbuffer)
{
	int ret= 0;
	if(blOpened)
	{
		DWORD	wrBytes=0;
		OVERLAPPED Wol;
		if(strlen(pbuffer)>0)
		{			
			Wol.Internal=0;
			Wol.InternalHigh=0;
			Wol.Offset=0;
			Wol.OffsetHigh=0;
			Wol.hEvent= CreateEvent(NULL,true,false,NULL);
			if(Wol.hEvent==NULL)	{AfxMessageBox("Create Wol.hEvent error!"); return -1;}
			if(WriteFile(hCom,pbuffer,strlen(pbuffer),&wrBytes, &Wol))
			{
				ret= wrBytes;
			}
			else
			{
				DWORD dwRes, dwWrite;
				dwRes= WaitForSingleObject(Wol.hEvent, SERIAL_WRITE_TIMEOUT);	//timeout 500ms
				switch(dwRes)
				{
				case WAIT_OBJECT_0:
					if(!GetOverlappedResult(hCom, &Wol,&dwWrite, true))
					{
						//GetLastError()
						if(GetLastError()==ERROR_IO_PENDING) AfxMessageBox("ERROR_IO_PENDING");
						else {CString msg; msg.Format("error: %d", GetLastError());  AfxMessageBox(msg);}
						ret= -3;
					}
					else
					{
						//send ok
						ret= dwWrite;
					}
					break;
				case WAIT_TIMEOUT:
					AfxMessageBox("Serial Write Timeout");
					ret= -4;
					break;
				default:
					//default opreation
					break;
				}
			}
			CloseHandle(Wol.hEvent);

			//Beep(1000,10);

			//AfxMessageBox("Send Message \""+m_SendMessage+"\" Ok!");
			//m_ShowMessage= m_SendMessage;
		}
		else 
		{
			AfxMessageBox("pbuffer is empty");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		return -1;
	}
	return ret;
}

/*
normal com operation 
*/
C_DLLEXPORT
int Open_NoOVERLAPPED(char *comPort)
{
	if(blOpened)	return 0;	//have open comm port
	hCom= CreateFile((LPCTSTR)comPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,NULL);
	if(hCom!= INVALID_HANDLE_VALUE)
	{
		//if(Setup(9600, NOPARITY, 8, ONESTOPBIT)<0) return -1;
		blOpened= true;
		if(Setup()<0) return -1;
//		AfxMessageBox("Open Com Port Sucessfully!");
	}
	else
	{
		blOpened= false;
		AfxMessageBox("Can't open Com Port!");
		return -1;
	}
	return 0;
}

C_DLLEXPORT
int Read_NoOVERLAPPED(char *pbuffer)
{
	int ret= 0;
	if(blOpened)
	{
		DWORD	rdBytes=0;
		COMSTAT	comstatus;
		ClearCommError(hCom, NULL, &comstatus);
//		OVERLAPPED Rol;
		if(pbuffer!=NULL)
		{			
			if(comstatus.cbInQue>0)
			{
				if(ReadFile(hCom,pbuffer,comstatus.cbInQue,&rdBytes, NULL)==0)
				{
					AfxMessageBox("Serial Communication read error");
					ret= -3;
				}
				else
					ret= rdBytes;
			}
		}
		else 
		{
			AfxMessageBox("pbuffer point error!");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}

/*
normal com operation
*/
C_DLLEXPORT
int Write_NoOVERLAPPED(char *pbuffer)
{
	int ret= 0;
	if(blOpened)
	{
		DWORD	wrBytes=0;
		if(strlen(pbuffer)>0)
		{
			if(WriteFile(hCom,pbuffer,strlen(pbuffer),&wrBytes, NULL)==0)
			{
//				DWORD errorCode= GetLastError();
				AfxMessageBox("Serial Communication write error");
				ret= -2;
			}
			else
				ret= wrBytes;
		}
		else
		{
			AfxMessageBox("pbuffer is empty");
			ret= -2;
		}
	}
	else
	{
		AfxMessageBox("Please Open Com Port frist!");
		ret= -1;
	}
	return ret;
}


C_DLLEXPORT
DWORD CheckInBuffer()
{
	if(blOpened)	return -1;

	COMSTAT	comstatus;
	ClearCommError(hCom, NULL, &comstatus);

	return comstatus.cbInQue;
}

C_DLLEXPORT
DWORD CheckOutBuffer()
{
	if(blOpened)	return -1;

	COMSTAT	comstatus;
	ClearCommError(hCom, NULL, &comstatus);

	return comstatus.cbOutQue;
}

#endif

⌨️ 快捷键说明

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