Microsoft Visual C++ 2010 和 Arduino UNO 通过 USB 之间的串行通信
Serial Comms between Microsoft Visual C++ 2010 and Arduino UNO via USB
我需要通过 USB 在 Microsoft Windows Visual C++ 2010 和 Arduino 微控制器之间建立串行通信。运动跟踪算法产生 X 和 Y 坐标,需要将其发送到 Arduino,后者控制两个平移和倾斜伺服系统。
我是一名机械工程专业的大四学生,对 Microsoft Visual Studios 和 C++ 的使用经验很少,所以请多多包涵,如果我的术语有误,还请多多包涵...
我在多个论坛上进行了广泛的研究,但找不到针对我的问题的答案:
我遇到的所有解决方案都只支持在 Visual Studios 中创建普通/"empty" 项目时的通信。可以在此处找到示例:Serial communication (for Arduino) using Visual Studio 2010 and C
当我尝试在 "Win32 Console Application" 项目中调试相同的代码体(在 "empty" 项目中成功运行)时,出现以下错误:
错误 C2065:'LcommPort':未声明的标识符
错误 C2228:“.c_str”左侧必须有 class/struct/union
不幸的是,我不能简单地将我的项目从 "Win32 Console Application" 更改为普通的 "Empty" 项目,因为运动跟踪算法需要使用控制台应用程序类型的项目。
我使用的代码主体如下(这是一个简化的测试源文件,用于确认MS Visual和Arduino之间是否建立了通信,其中LED打开和关闭的频率被改变通过串行连接):
#include <Windows.h>
#include "ArduinoSerial.h"
#include "StdAfx.h"
int main() {
try {
ArduinoSerial arduino( "COM3" );
Sleep( 2000 ); // Initial wait to allow Arduino to boot after reset
char buffer[] = { 25, 100 };
arduino.Write( buffer, 2 ); // Send on/off delays to Arduino (if return value is 0, something went wrong)
}
catch ( const ArduinoSerialException &e ) {
MessageBoxA( NULL, e.what(), "ERROR", MB_ICONERROR | MB_OK );
}
return 0;
}
对应的错误源代码在第9行代码中找到:
#include <algorithm>
#include <sstream>
#include <Windows.h>
#include "stdafx.h"
#include "ArduinoSerial.h"
ArduinoSerial::ArduinoSerial( const std::string commPort ) {
comm = CreateFile( TEXT( commPort.c_str() ),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
if ( comm == INVALID_HANDLE_VALUE ) {
std::ostringstream error;
error << "Unable to acquire handle for " << commPort << ": ";
DWORD lastError = GetLastError();
if ( lastError == ERROR_FILE_NOT_FOUND ) {
error << "Invalid port name";
}
else {
error << "Error: " << lastError;
}
throw ArduinoSerialException( error.str() );
}
DCB dcb;
SecureZeroMemory( &dcb, sizeof DCB );
dcb.DCBlength = sizeof DCB;
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
if ( !SetCommState( comm, &dcb ) ) {
CloseHandle( comm );
std::ostringstream error;
error << "Unable to set comm state: Error " << GetLastError();
throw ArduinoSerialException( error.str() );
}
PurgeComm( comm, PURGE_RXCLEAR | PURGE_TXCLEAR );
}
std::size_t ArduinoSerial::Read( char buffer[], const std::size_t size ) {
DWORD numBytesRead = 0;
BOOL success = ReadFile( comm, buffer, size, &numBytesRead, NULL );
if ( success ) {
return numBytesRead;
}
else {
return 0;
}
}
std::size_t ArduinoSerial::Write( char buffer[], const std::size_t size ) {
DWORD numBytesWritten = 0;
BOOL success = WriteFile( comm, buffer, size, &numBytesWritten, NULL );
if ( success ) {
return numBytesWritten;
}
else {
return 0;
}
}
ArduinoSerial::~ArduinoSerial() {
CloseHandle( comm );
}
ArduinoSerialException::ArduinoSerialException( const std::string message ) :
std::runtime_error( message ) {
}
非常感谢任何帮助或建议。
I am presented with the following errors:
error C2065: 'LcommPort' : undeclared identifier error C2228: left of '.c_str' must have class/struct/union
这段代码
TEXT( commPort.c_str())
实际上变成了
LcommPort.c_str()
这就是您收到此编译器错误的原因。
你应该注意到 TEXT()
是一个用于字符文字的预处理器宏,根据项目编译的模式 (Unicode/ASCII) 在它们前面加上 L
前缀。它显然不适用于任何变量。
直接使用commPort.c_str()
,或const std::wstring commPort
。
我需要通过 USB 在 Microsoft Windows Visual C++ 2010 和 Arduino 微控制器之间建立串行通信。运动跟踪算法产生 X 和 Y 坐标,需要将其发送到 Arduino,后者控制两个平移和倾斜伺服系统。
我是一名机械工程专业的大四学生,对 Microsoft Visual Studios 和 C++ 的使用经验很少,所以请多多包涵,如果我的术语有误,还请多多包涵...
我在多个论坛上进行了广泛的研究,但找不到针对我的问题的答案:
我遇到的所有解决方案都只支持在 Visual Studios 中创建普通/"empty" 项目时的通信。可以在此处找到示例:Serial communication (for Arduino) using Visual Studio 2010 and C
当我尝试在 "Win32 Console Application" 项目中调试相同的代码体(在 "empty" 项目中成功运行)时,出现以下错误:
错误 C2065:'LcommPort':未声明的标识符 错误 C2228:“.c_str”左侧必须有 class/struct/union
不幸的是,我不能简单地将我的项目从 "Win32 Console Application" 更改为普通的 "Empty" 项目,因为运动跟踪算法需要使用控制台应用程序类型的项目。
我使用的代码主体如下(这是一个简化的测试源文件,用于确认MS Visual和Arduino之间是否建立了通信,其中LED打开和关闭的频率被改变通过串行连接):
#include <Windows.h>
#include "ArduinoSerial.h"
#include "StdAfx.h"
int main() {
try {
ArduinoSerial arduino( "COM3" );
Sleep( 2000 ); // Initial wait to allow Arduino to boot after reset
char buffer[] = { 25, 100 };
arduino.Write( buffer, 2 ); // Send on/off delays to Arduino (if return value is 0, something went wrong)
}
catch ( const ArduinoSerialException &e ) {
MessageBoxA( NULL, e.what(), "ERROR", MB_ICONERROR | MB_OK );
}
return 0;
}
对应的错误源代码在第9行代码中找到:
#include <algorithm>
#include <sstream>
#include <Windows.h>
#include "stdafx.h"
#include "ArduinoSerial.h"
ArduinoSerial::ArduinoSerial( const std::string commPort ) {
comm = CreateFile( TEXT( commPort.c_str() ),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
if ( comm == INVALID_HANDLE_VALUE ) {
std::ostringstream error;
error << "Unable to acquire handle for " << commPort << ": ";
DWORD lastError = GetLastError();
if ( lastError == ERROR_FILE_NOT_FOUND ) {
error << "Invalid port name";
}
else {
error << "Error: " << lastError;
}
throw ArduinoSerialException( error.str() );
}
DCB dcb;
SecureZeroMemory( &dcb, sizeof DCB );
dcb.DCBlength = sizeof DCB;
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
if ( !SetCommState( comm, &dcb ) ) {
CloseHandle( comm );
std::ostringstream error;
error << "Unable to set comm state: Error " << GetLastError();
throw ArduinoSerialException( error.str() );
}
PurgeComm( comm, PURGE_RXCLEAR | PURGE_TXCLEAR );
}
std::size_t ArduinoSerial::Read( char buffer[], const std::size_t size ) {
DWORD numBytesRead = 0;
BOOL success = ReadFile( comm, buffer, size, &numBytesRead, NULL );
if ( success ) {
return numBytesRead;
}
else {
return 0;
}
}
std::size_t ArduinoSerial::Write( char buffer[], const std::size_t size ) {
DWORD numBytesWritten = 0;
BOOL success = WriteFile( comm, buffer, size, &numBytesWritten, NULL );
if ( success ) {
return numBytesWritten;
}
else {
return 0;
}
}
ArduinoSerial::~ArduinoSerial() {
CloseHandle( comm );
}
ArduinoSerialException::ArduinoSerialException( const std::string message ) :
std::runtime_error( message ) {
}
非常感谢任何帮助或建议。
I am presented with the following errors:
error C2065: 'LcommPort' : undeclared identifier error C2228: left of '.c_str' must have class/struct/union
这段代码
TEXT( commPort.c_str())
实际上变成了
LcommPort.c_str()
这就是您收到此编译器错误的原因。
你应该注意到 TEXT()
是一个用于字符文字的预处理器宏,根据项目编译的模式 (Unicode/ASCII) 在它们前面加上 L
前缀。它显然不适用于任何变量。
直接使用commPort.c_str()
,或const std::wstring commPort
。