2013-09-28 54 views
0

我試圖在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)); 
} 

這就是工作。任何建議,以優化我有什麼?

謝謝!

回答

0

它已經告訴你到底發生了什麼。你把FB :: JSObjectPtr類型作爲第一個參數,但是你傳入一個字符串作爲參數;它不能將字符串轉換爲JavaScript對象,因此失敗。

我真的不明白爲什麼sendMulti可能會做它正在做的事情;它需要一個字符數組中的字符串,['l','i','k','e',' ','t','h','i','s']。爲什麼要這麼做?這對我來說似乎有點不可思議。

相反,爲什麼不只是將sendMulti的第一個參數改爲const std :: string &,並在傳入時使用該字符串?

我想的完整性,我應該說,你也許可以讓它工作在當前的方式調用它像這樣:

var strArray = "Sc48E".split(''); 
ser.sendmulti(strArray, strArray.length); 

...注意,這是真的低效因爲它必須調用回到JavaScript來獲取每個字符,而不是隻是在一個塊中獲取字符串。

+0

感謝您的回覆Taxilian。這絕對有道理。這些都不是我的代碼,我對C++的經驗(或者大多數編碼)是有限的。這就是說,我會嘗試實施它,看看它是如何發展的。 同意使用字符串與字符數組。我假設我可以發送一個完整的字符串到串口沒有問題? 另一個說明,即使我在重建它時沒有更改過任何東西,但爲什麼send()可以在包含的.dll中工作,但不是新構建的任何想法?它給出與sendmulti()相同的錯誤,並且這種改變對我來說沒有意義。 謝謝! – codefame

+0

看起來'send_msg.push_back'函數需要一個char,這就解釋了作者爲什麼要使用它。我確實使用你的建議得到它的工作。將用我上面的最終代碼進行編輯。 – codefame