错误消息:c++GUI和Arduino之间的串行通信

Error message: Serial communication between c++ GUI and Arduino

本文关键字:通信 之间 Arduino 消息 c++GUI 错误      更新时间:2023-10-16

我即将编写一个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;

}