我试图在jUART(https://github.com/billhsu/jUART)中排除sendmulti函数的故障(通过发送char数组的一部分到串口)。jUART/FireBreath插件错误
该特定函数未使用已在存储库中构建的.dll进行注册。我下载了&,但没有更改任何内容,使用regsvr32注册了新的.dll文件,并用新重建的文件替换了旧的.dll文件。
现在函数被注册并且可以被调用,但是根据我提供的输入,我得到了一些错误。
第一个错误:
ser.sendmulti("Sc48E");
Error in event handler: Error: Error calling method on NPObject.
ser.getLastException();
"Error: Argument 2is not optional."
所以我增加了第二个说法,现在我得到:
ser.sendmulti("Sc48E", 2);
Error: Error calling method on NPObject.
ser.getLastException();
"Invalid argument conversion from class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > to class boost::shared_ptr<class FB::JSAPI> at index 1"
不是真的知道在哪里可以从那里(完全不一个有经验的开发),所以我正在转向社区,看看接下来应该考虑什么,或者是否有人可以和我一起深入jUART找到解决方法。
的显着相关职能sendmulti,我可以找到:
SerialAPI.h
void sendmulti(const FB::JSObjectPtr& msg, int length)
{
unsigned char *message = new unsigned char[length];
FB::variant v;
for(int i = 0; i < length; i++)
{
v = msg->GetProperty(i);
message[i] = v.convert_cast<unsigned char>();
}
io.post(boost::bind(&SerialAPI::do_multi_send, this, (char *)message, length));
void do_multi_send(const char msg[], int length);
void send_multi_start(int length);
SerialAPI.cpp
void SerialAPI::do_multi_send(const char msg[], const int length)
{
bool write_in_progress = !send_msg.empty(); // is there anything currently being written?
for(int i = 0; i < length; i++)
{
send_msg.push_back(msg[i]); // store in write buffer
}
if (!write_in_progress) // if nothing is currently being written, then start
send_multi_start(length);
}
void SerialAPI::send_multi_start(int length)
{
boost::asio::async_write(serial,
boost::asio::buffer(&send_msg.front(), length),
boost::bind(&SerialAPI::send_multi_complete,
this,
boost::asio::placeholders::error));
}
我认为这些是直接相关的功能,尽管它值得注意的是,send()函数(仅发送一个字节)与第一个.dll一起工作,并给出了与新建的.dll相同的错误:
void SerialAPI::do_send(const char msg)
{
bool write_in_progress = !send_msg.empty(); // is there anything currently being written?
send_msg.push_back(msg); // store in write buffer
if (!write_in_progress) // if nothing is currently being written, then start
send_start();
}
谢谢!
* 工作代码 *
SerialAPI.h
void sendmulti(const std::string& msg)
{
io.post(boost::bind(&SerialAPI::do_multi_send, this, msg));
}
void do_multi_send(const std::string& msg);
SerialAPI.cpp
void SerialAPI::do_multi_send(const std::string& msg)
{
bool write_in_progress = !send_msg.empty(); // is there anything currently being written?
const int sLength = msg.length();
for(int i = 0; i < sLength; i++) {
const char cMsg = msg[i];
send_msg.push_back(cMsg); // store in write buffer
if (!write_in_progress) // if nothing is currently being written, then start
send_multi_start(sLength);
}
void SerialAPI::send_multi_start(int sLength)
{
boost::asio::async_write(serial,
boost::asio::buffer(&send_msg.front(), sLength),
boost::bind(&SerialAPI::send_multi_complete,
this,
boost::asio::placeholders::error));
}
这就是工作。任何建议,以优化我有什么?
谢谢!
感谢您的回复Taxilian。这绝对有道理。这些都不是我的代码,我对C++的经验(或者大多数编码)是有限的。这就是说,我会尝试实施它,看看它是如何发展的。 同意使用字符串与字符数组。我假设我可以发送一个完整的字符串到串口没有问题? 另一个说明,即使我在重建它时没有更改过任何东西,但为什么send()可以在包含的.dll中工作,但不是新构建的任何想法?它给出与sendmulti()相同的错误,并且这种改变对我来说没有意义。 谢谢! – codefame
看起来'send_msg.push_back'函数需要一个char,这就解释了作者为什么要使用它。我确实使用你的建议得到它的工作。将用我上面的最终代码进行编辑。 – codefame