Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误消息:c和x2B之间的串行通信+;圭和阿杜伊诺 我打算编写一个C++ GUI应用程序(WxWIDGET)来控制一个ARDUINO。我想使用Arduino游乐场网站()中的SerialClass.h和SerialClass.cpp。我已经用这些.h和.cpp文件构建了一个运行良好的控制台应用程序。最近,我不知何故收到了一条奇怪的错误消息:_C++_User Interface_Arduino_Serial Port - Fatal编程技术网

错误消息:c和x2B之间的串行通信+;圭和阿杜伊诺 我打算编写一个C++ GUI应用程序(WxWIDGET)来控制一个ARDUINO。我想使用Arduino游乐场网站()中的SerialClass.h和SerialClass.cpp。我已经用这些.h和.cpp文件构建了一个运行良好的控制台应用程序。最近,我不知何故收到了一条奇怪的错误消息:

错误消息:c和x2B之间的串行通信+;圭和阿杜伊诺 我打算编写一个C++ GUI应用程序(WxWIDGET)来控制一个ARDUINO。我想使用Arduino游乐场网站()中的SerialClass.h和SerialClass.cpp。我已经用这些.h和.cpp文件构建了一个运行良好的控制台应用程序。最近,我不知何故收到了一条奇怪的错误消息:,c++,user-interface,arduino,serial-port,C++,User Interface,Arduino,Serial Port,在构造函数中Serial::Serial(char*) 错误:无法将参数“1”的“char*”转换为“const WCHAR*”,将其转换为“void*CreateFileW(const WCHAR*,DWORD,DWORD,_SECURITY_ATTRIBUTES*,DWORD,DWORD,void*)” 我不明白这个意思。要使SerialClass.h或SerialClass.cpp正常工作,应更改哪些内容?Arduino代码很好。为了完整性,我将控制台应用程序附加到C++代码中。谷歌的很多

在构造函数中
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 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:

#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
\ifndef SERIALCLASS\u包括在内
#定义包含的串行类
#定义ARDUINO_等待时间2000
#包括
#包括
#包括
类序列号
{
私人:
//串行通信处理程序
处理串行;
//连接状态
布尔连接;
//获取有关连接的各种信息
COMSTAT状态;
//跟踪最后一个错误
德沃德误差;
公众:
//初始化与给定COM端口的串行通信
串行(字符*端口名);
//关闭连接
~Serial();
//如果nbChar大于
//最大可用字节数,它将仅返回
//字节可用。当无法执行任何操作时,函数返回-1
//要读取,实际读取的字节数。
int ReadData(字符*缓冲区,无符号int-nbChar);
//通过串行连接从缓冲区写入数据
//成功后返回真实。
bool WriteData(字符*缓冲区,无符号整数nbChar);
//检查我们是否真的有联系
布尔断开连接();
};
#endif//SERIALCLASS\u包括在内
该行传递一个字符*。要传递WCHAR*,您需要将其更改为:

Serial serial(L"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;
}

构造函数中的CreateFile()函数需要常量WCHAR*,因此您需要将常量char*转换为常量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;

}

看起来函数采用了
WCHAR*
:。将您的
char*
转换为该数据类型,您就可以了。谢谢!我是一个编程新手。你能更具体一点说我要改变什么吗。我应该在什么文件(SerialClass.cpp?)中更改什么,然后在您的问题中包含相关的构造函数代码。目前,问题正文中没有足够的信息可以回答。我与Visual Studio在完全无关的项目上遇到过类似的问题,并通过在项目选项中启用Unicode设置来解决。几年前我就没有使用Visual Studio了,所以我无法确认。看起来该函数采用了
WCHAR*
:。将您的
char*
转换为该数据类型,您就可以了。谢谢!我是一个编程新手。你能更具体一点说我要改变什么吗。我应该在什么文件(SerialClass.cpp?)中更改什么,然后在您的问题中包含相关的构造函数代码。目前,问题正文中没有足够的信息可以回答。我与Visual Studio在完全无关的项目上遇到过类似的问题,并通过在项目选项中启用Unicode设置来解决。我几年前就没有使用Visual Studio了,所以无法确认。嗨,斯科特,谢谢你的提示,但我仍然收到相同的错误消息。SerialClass.cpp中一定有问题。这只是我的猜测。嗨,斯科特,谢谢你的提示,但我仍然收到相同的错误消息。SerialClass.cpp中一定有问题。那只是我的猜测。我想我找到了解决问题的办法。它与unicode字符集有关。当我在VisualStudio中设置“使用多字节字符集”时,它可以工作。另外,当我在Code::Blocks中停用“Unicode设置”时,它也可以正常工作。非常奇怪的错误。很难发现。感谢大家帮助我。是的,wchar用于Unicode,char用于ASCII。我想我找到了解决问题的方法。它与unicode字符集有关。当我在VisualStudio中设置“使用多字节字符集”时,它可以工作。另外,当我在Code::Blocks中停用“Unicode设置”时,它也可以正常工作。非常奇怪的错误。很难发现。谢谢大家的帮助。是的,wchar用于Unicode,char用于ASCII