簡體   English   中英

錯誤消息:C ++ GUI和Arduino之間的串行通信

[英]Error message: Serial communication between c++ GUI and Arduino

我將要編程一個c ++ GUI應用程序(wxWidgets)以控制Arduino。 我想使用Arduino運動場網站( http://playground.arduino.cc/Interface/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");

構造函數中的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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM