我将要编程一个c ++ GUI应用程序(wxWidgets)以控制Arduino。我想使用Arduino游乐场站点(http://playground.arduino.cc/Interfacing/CPPWindows)的SerialClass.h和SerialClass.cpp。我已经用这些.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 ++代码。许多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;
  }
}


编辑:这是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);
         }
    }
}

}


这是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");


该行通过char *。要传递WCHAR *,您需要将其更改为:

Serial serial(L"COM4");

09-30 20:34