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

📄 main.cpp

📁 《Visual C++串口通信开发入门与编程实践》(源码) 本书着重介绍计算机串口通信的硬件原理、软件开发和工程实例。案例式教学
💻 CPP
字号:
//	HelloWorld.cpp - Sample application for CSerial
//
//	Copyright (C) 1999-2003 Ramon de Klein (Ramon.de.Klein@ict.nl)
//
// The Serial library is free software. You can use it for free in your
// own applications and/or libraries, without paying any royalties.
// Please keep in mind that I have put a lot of effort in this library,
// so don't redistibute the code without my credits. If you make
// modifications, then please mark them clearly. I don't want to be
// blamed for bugs of others.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

#define STRICT
#include <tchar.h>
#include <windows.h>
#include <Serial.h>

int ShowError (LONG lError, LPCTSTR lptszMessage)
{
	// Generate a message text
	
	TCHAR* buffer;
	::FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		lError,
		0,
		( LPTSTR )&buffer,
		0,
		NULL );

	LPTSTR tszMessage = new TCHAR[lstrlen(lptszMessage)+lstrlen(buffer)+8]; 
	wsprintf(tszMessage,_T("%s\n%s"), lptszMessage, buffer);

	// Display message-box and return with an error-code
	::MessageBox(0,tszMessage,_T("Hello world"), MB_ICONSTOP|MB_OK);
	LocalFree(buffer);
	delete [] tszMessage;
	return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	CSerial serial;
	LONG    lLastError = ERROR_SUCCESS;

	// Attempt to open the serial port (COM1)
	lLastError = serial.Open(_T("COM1"),0,0,false);
	if (lLastError != ERROR_SUCCESS)
		return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));

	// Setup the serial port (9600,N81) using hardware handshaking
	lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
	if (lLastError != ERROR_SUCCESS)
		return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));

	//// Setup handshaking
	//lLastError = serial.SetupHandshaking(CSerial::EHandshakeHardware);
	//if (lLastError != ERROR_SUCCESS)
	//	return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port handshaking"));

	// The serial port is now ready and we can send/receive data. If
	// the following call blocks, then the other side doesn't support
	// hardware handshaking.
	lLastError = serial.Write("Hello world\n");
	if (lLastError != ERROR_SUCCESS)
		return ::ShowError(serial.GetLastError(), _T("Unable to send data"));

	// Close the port again
	serial.Close();
	return 0;
}

⌨️ 快捷键说明

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