我创建了一个程序,使用C ++从COM端口读取串行数据,但是由于我的项目发生了变化,我必须从BT端口读取数据。由于我使用蓝牙适配器将我的计算机连接到设备,我预计阅读过程是相同的,但显然不是。由于我使用Window OS来执行此任务,GetLastError()返回2,这意味着找不到指定的文件。但是,当我使用Arduino的串行监视器时,数据读得很好。有谁知道如何从C ++的BT端口读取?我顺便使用Windows 8,这是我的代码:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
///-----------------------------Open port-----------------------------------------------------------------
// Name of port where device is found
LPCWSTR port = L"COM40";
// Open port for reading
HANDLE hComm = ::CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// Check if port has been opened succesfully
if (hComm == INVALID_HANDLE_VALUE) std::cout << "Failed to open " << port << " error: " << GetLastError() << std::endl;
else std::cout << port << " has been opened succesfully\n";
///-----------------------------Configure port------------------------------------------------------------
// Create DCB structure
DCB dcb = { 0 };
// Get Comm state
if (!::GetCommState(hComm, &dcb)) std::cout << "Failed to get Comm state, error: " << GetLastError() << std::endl;
// Configure strcutre
dcb.DCBlength = sizeof(DCB);
// Set Baud rate
dcb.BaudRate = CBR_9600;
// Set number of bytes in bits that are recieved through the port
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
// Check if port has been configured correctly
if (!::SetCommState(hComm, &dcb)) std::cout << "\nFailed to set Comm State, error: " << GetLastError();
else std::cout << "Comm state has been set succesfully\n";
///-----------------------------Read data-------------------------------------------------------------------
char buffer;
DWORD maxBytes = 1;
if (!::ReadFile(hComm, &buffer, maxBytes, NULL, NULL)) std::cout << "\nFailed to read from " << port << " error: " << GetLastError() << std::endl;
else std::cout << "File has been read succesfully\n";
///-----------------------------Write to text file----------------------------------------------------------
std::fstream file;
int counter = 0;
// Writing to text file will be done later
while (ReadFile(hComm, &buffer, maxBytes, NULL, NULL))
{
std::cout << buffer;
}
///-----------------------------Close port------------------------------------------------------------------
CloseHandle(hComm);
file.close();
std::cout << "\nCOM40 has been closed!\n";
return 0;
}