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

📄 rscomm.cpp

📁 haojige
💻 CPP
字号:
// RSComm.cpp: implementation of the RSComm class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Serial Communication.h"
#include "RSComm.h"

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

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

RSComm::RSComm()
{

}

RSComm::~RSComm()
{

}


/* 
   The setting should be in the format "BaudRate,parity,databits,stopbits" for example
   "9600,N,8,1". If the setting are not valid then -1 is returned, otherwise 0 is returned
*/
int RSComm::Settings(CString String)
{
	char Comma =',';
	int Index, Success;
	CString Temp;

	BaudRate = atoi(String);				// read BaudRate

	Index = String.Find(Comma, 0);
	Parity = String[Index + 1];				// read parity

    Temp = String.Right(String.GetLength() - Index - 3);
	DataBits = atoi(Temp);					// read data bits

	Index = String.Find(Comma, Index + 3);
	Temp = String.Right(String.GetLength() - Index - 1);
	StopBits = (float) atof(Temp);			// read stop bits

	Success = 0;							// assume settings are correct

	switch (BaudRate)						// check for valaid baudrate
	{
		case 110 :
		case 300 :
		case 1200 :
		case 2400 :
		case 4800 :
		case 9600 :
		case 19200 :
		case 38400 :
		case 57600 :
		case 115200 : break;
		default : Success = -1;
	}

	switch (Parity) 
	{
		case 'N' :
		case 'O' :
		case 'E' : break;
		default : Success = -1;
	}

	switch (DataBits) 
	{
		case 4 :
		case 5 :
		case 6 :
		case 7 :
		case 8 : break;
		default : Success = -1;
	}

	if (!(StopBits == 1 || StopBits == 1.5 || StopBits == 2))
		Success = -1;

	return Success;
}

int RSComm::PortOpen(bool Open)
{
	CString StringPort;
	int Success;

	if (Open)
	{
		ZeroMemory(&dcb, sizeof (DCB));
		dcb.DCBlength = sizeof(DCB);

		switch (BaudRate)
		{
			case 110 : dcb.BaudRate = CBR_110; break;
			case 300 : dcb.BaudRate = CBR_300; break;
			case 1200 : dcb.BaudRate = CBR_1200; break;
			case 2400 : dcb.BaudRate = CBR_2400; break;
			case 4800 : dcb.BaudRate = CBR_4800; break;
			case 9600 : dcb.BaudRate = CBR_9600; break;
			case 19200 : dcb.BaudRate = CBR_19200; break;
			case 38400 : dcb.BaudRate = CBR_38400; break;
			case 57600 : dcb.BaudRate = CBR_57600; break;
			case 115200 : dcb.BaudRate = CBR_115200; break;
		}

		dcb.fBinary = 1;
		
		dcb.fDtrControl = DTR_CONTROL_DISABLE;
		dcb.fRtsControl = RTS_CONTROL_DISABLE;

		dcb.ByteSize = DataBits;

		switch (Parity) 
		{
			case 'N' : dcb.Parity = NOPARITY; break;
			case 'O' : dcb.Parity = ODDPARITY; break;
			case 'E' : dcb.Parity = EVENPARITY; break;
		}

		if (StopBits == 1)
			dcb.StopBits = ONESTOPBIT;
		else if (StopBits == 1.5)
			dcb.StopBits = ONE5STOPBITS; 
		else if (StopBits == 2)
			dcb.StopBits = TWOSTOPBITS;

		StringPort.Format("COM%d", SerialPort);
		CommDevice = CreateFile(StringPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
		                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	
		if (CommDevice != INVALID_HANDLE_VALUE)
		{
			Success = 0;

			SetupComm(CommDevice, InputBuffer, OutputBuffer);
			SetCommState(CommDevice, &dcb);

			PurgeComm(CommDevice, (DWORD) PURGE_TXCLEAR | PURGE_RXCLEAR);

			COMMTIMEOUTS ctmo;
			ctmo.ReadIntervalTimeout = 50;
			ctmo.ReadTotalTimeoutMultiplier = 5;
			ctmo.ReadTotalTimeoutConstant = 50;	
			ctmo.WriteTotalTimeoutMultiplier = 50;
			ctmo.WriteTotalTimeoutConstant = 50;	
			SetCommTimeouts(CommDevice, &ctmo);		
		}
		else
			Success = -1;
	}
	else
	{
		Success = 0;
		CloseHandle(CommDevice);
	}
	
	return Success;
}

CString RSComm::Read(int Length)
{
	char Str[2048];
	CString String;
	
	ReadFile(CommDevice, (LPVOID)Str, (DWORD)Length,  &dw, NULL);	
	Str[Length] = NULL;

	String.Format("%s", Str);

	return String;
}

RSComm::Write(CString String)
{
	char Str[2048];

	sprintf(Str, "%s", String);

	PurgeComm(CommDevice, (DWORD) PURGE_TXCLEAR | PURGE_RXCLEAR);
	WriteFile(CommDevice, Str, strlen(Str), &dw, NULL);				
}

⌨️ 快捷键说明

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