错误消息:c++GUI和Arduino之间的串行通信
Error message: Serial communication between c++ GUI and Arduino
我即将编写一个c++GUI应用程序(wxWidgets(来控制Arduino。我想使用Arduino游乐场网站上的SerialClass.h和SerialClass.cpp(http://playground.arduino.cc/Interfacing/CPPWindows)。我已经用这些.h和.cpp文件构建了一个控制台应用程序,运行良好。最近,我不知怎么收到了一条奇怪的错误消息:
在构造函数Serial::Serial(char*)
中
错误:无法将参数"1"的"char*"转换为"const WCHAR*",将其转换为"void*CreateFileW(const WCHAR*,DWORD,DWORD,_SECURITY_ATTRIBUTES*,DWORD、DWORD,void*(">
我没有收到那个信息。SerialClass.h或SerialClass.cpp中应该更改什么才能正常工作?Arduino代码很好。为了完整起见,我附上了控制台应用程序的c++代码。谷歌上的许多傅都无济于事。
#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h" // Library described above
#include <string>
#include <iostream>
using namespace std;
bool weiter = true;
int dummy1 = 0;
int _tmain(int argc, _TCHAR* argv[]) {
cout << "*** This is my Arduino LED app! ***n" << endl;
//Serial* SP = new Serial("COM4");
//Serial serial("COM4");
Serial serial("COM4");
if (serial.IsConnected())
//printf("We are connectedn");
cout << "We are connected!n" << endl;
while (weiter == true) {
cout << "Press 1 for LED on; press 0 for LED off!" << endl;
cin >> dummy1;
if (dummy1 == 1) {
if (serial.IsConnected()){
serial.WriteData("o",1);
cout << "LED is on!" << endl;
cout << "Do you want to continue? 1 for continue, 0 for exit!" << endl;
//printf("nData sent successfully!n");
cin >> weiter;
}
}
else {
serial.WriteData("p", 1 );
cout << "LED is off!" << endl;
cout << "Do you want to continue? 1 for continue, 0 for exit!" << endl;
cin >> weiter;
}
}
if (weiter == 1)
{
weiter = true;
}
if (weiter == 0) {
weiter = false;
return 0;
}
}
编辑:这是SerialClass.h和Serial.cpp:的代码
#include "SerialClass.h"
Serial::Serial(char *portName)
{
//We're not yet connected
this->connected = false;
//Try to connect to the given port throuh CreateFile
this->hSerial = CreateFile(portName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//Check if the connection was successfull
if(this->hSerial==INVALID_HANDLE_VALUE)
{
//If not success full display an Error
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//Print Error if neccessary
printf("ERROR: Handle was not attached. Reason: %s not available.n", portName);
}
else
{
printf("ERROR!!!");
}
}
else
{
//If connected we try to set the comm parameters
DCB dcbSerialParams = {0};
//Try to get the current
if (!GetCommState(this->hSerial, &dcbSerialParams))
{
//If impossible, show an error
printf("failed to get current serial parameters!");
}
else
{
//Define serial connection parameters for the arduino board
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
//Setting the DTR to Control_Enable ensures that the Arduino is properly
//reset upon establishing a connection
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
//Set the parameters and check for their proper application
if(!SetCommState(hSerial, &dcbSerialParams))
{
printf("ALERT: Could not set Serial Port parameters");
}
else
{
//If everything went fine we're connected
this->connected = true;
//Flush any remaining characters in the buffers
PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
//We wait 2s as the arduino board will be reseting
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
这是系列课。h:
#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED
#define ARDUINO_WAIT_TIME 2000
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
class Serial
{
private:
//Serial comm handler
HANDLE hSerial;
//Connection status
bool connected;
//Get various information about the connection
COMSTAT status;
//Keep track of last error
DWORD errors;
public:
//Initialize Serial communication with the given COM port
Serial(char *portName);
//Close the connection
~Serial();
//Read data in a buffer, if nbChar is greater than the
//maximum number of bytes available, it will return only the
//bytes available. The function return -1 when nothing could
//be read, the number of bytes actually read.
int ReadData(char *buffer, unsigned int nbChar);
//Writes data from a buffer through the Serial connection
//return true on success.
bool WriteData(char *buffer, unsigned int nbChar);
//Check if we are actually connected
bool IsConnected();
};
#endif // SERIALCLASS_H_INCLUDED
Serial serial("COM4");
那行经过一个字符*。要通过WCHAR*,您需要将其更改为:
Serial serial(L"COM4");
构造函数中的CreateFile((函数需要一个const WCHAR*,因此您需要以某种方式将const char*转换为const WCHAR*。
这是一个做转换的帖子。
虽然我对此没有太多信息,但帖子描述了几种方法,其中之一是:
char *p="D:\"; //just for proper syntax highlighting ..."
const WCHAR *pwcsName;
// required size
int nChars = MultiByteToWideChar(CP_ACP, 0, p, -1, NULL, 0);
// allocate it
pwcsName = new WCHAR[nChars];
MultiByteToWideChar(CP_ACP, 0, p, -1, (LPWSTR)pwcsName, nChars);
// use it....
// delete it
delete [] pwcsName;
}
相关文章:
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- C++ 两台不同机器之间通过wifi进行套接字通信
- 线程之间的通信不起作用 - C++
- 通过USB在PC和Arduino之间进行串行通信
- 管理器类和对象类之间的通信
- 使用命名管道在 C# 和C++应用程序之间进行持续通信
- Python 3 套接字和 QTcpsocket (c++) 之间的通信失败
- 如何在嵌入式浏览器和Webside之间进行双向通信
- C++程序和未识别程序之间的双向通信
- 使用C++在同一系统上运行的 2 个服务之间进行通信
- C# 和C++应用程序之间的通信
- 如何在Qt WebChannel中以独立于平台的方式在HTML/Javascript和C++之间进行通信?
- 如何在 gtk+ c 中的回调之间进行通信
- Electron应用程序和Win32本机桌面应用程序之间的进程间通信
- 如何在线程之间进行通信和等效的waitforsingleobject()
- C++ 和 Simulink 之间通过 UDP 进行通信
- 在两个Windows QT5之间进行通信
- 覆盆子Pi(QT C )和Arduino(Arduino IDE)之间的通信
- Shell扩展窗口:最好的通信之间的ContextMenu和IconOverlay