[英]Error message: Serial communication between c++ GUI and Arduino
I am about to program a c++ GUI application (wxWidgets) to control an Arduino. 我将要编程一个c ++ GUI应用程序(wxWidgets)以控制Arduino。 I want to use the SerialClass.h and SerialClass.cpp from the Arduino playground site ( http://playground.arduino.cc/Interfacing/CPPWindows ). 我想使用Arduino运动场网站( http://playground.arduino.cc/Interface/CPPWindows )上的SerialClass.h和SerialClass.cpp。 I already built a console application with these .h and .cpp files that is working fine. 我已经用这些.h和.cpp文件构建了一个控制台应用程序,并且运行正常。 Lately, I somehow get a strange error message: 最近,我以某种方式收到一条奇怪的错误消息:
In constructor Serial::Serial(char*)
在构造函数中Serial::Serial(char*)
error: cannot convert 'char*' to 'const WCHAR*' for argument '1' to 'void* CreateFileW(const WCHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)' 错误:无法将参数'1'的'char *'转换为'const WCHAR *'到'void * CreateFileW(const WCHAR *,DWORD,DWORD,_SECURITY_ATTRIBUTES *,DWORD,DWORD,void *)'
I don't get that message. 我没收到那条消息。 What should be changed in the SerialClass.h or SerialClass.cpp for it to be working? 要使其正常工作,应该在SerialClass.h或SerialClass.cpp中进行哪些更改? The Arduino code is fine. Arduino代码很好。 For completeness I attach the c++ code for the console application. 为了完整起见,我附加了控制台应用程序的c ++代码。 A lot of google-fu was to no avail. 许多Google Fu都无济于事。
#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 connected\n");
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;
}
}
EDIT: Here is the code for SerialClass.h and Serial.cpp: 编辑:这是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);
}
}
}
}
And here is the SerialClass.h: 这是SerialClass.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");
That line passes a char*. 该行通过char *。 To pass a WCHAR* instead you need to change it to: 要传递WCHAR *,您需要将其更改为:
Serial serial(L"COM4");
the CreateFile() function in your constructor requires a const WCHAR * so you somehow need to convert you const char * to const wchar* . 构造函数中的CreateFile()函数需要const WCHAR *,因此您需要以某种方式将const char *转换为const wchar *。
here is a post to do the conversion. 这是进行转换的帖子。
Although I don't have much info on this , the post describes several ways to do it One of them being : 尽管我对此没有太多信息,但该帖子描述了几种实现方法。其中之一是:
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;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.