Question

I'm trying to squeeze the best performance out of a JavaScript->Firebreath->Serial port project as possible. Essentially my JavaScript code passes strings to the Firebreath plugin very quickly (40-100ms between each command), which then forwards those strings to the serial port.

Though everything works, occasionally a string is missed or the serial port freezes and has to be reset. I've verified that it's not the hardware device I'm using and suspect the problem could exist within these functions. Any suggestions for optimizing the speed of receiving the string (something like: "Sc48E") from JavaScript, parsing it into a char (unfortunately this is required) and sending it to the serial port?

Thanks for your input!

Using jUART: https://github.com/billhsu/jUART

C++ Code:

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?   
    char cMsg[5];  // max length of incoming packets

    for(int i = 0; i < sLength; i++) {
        cMsg[i] = msg[i];
        send_msg.push_back(cMsg[i]); // store in write buffer; this function requires a char
    }
    if (!write_in_progress) // if nothing is currently being written, then start 
        send_multi_start(); 
}

void SerialAPI::send_multi_start(void) 
{
    boost::asio::async_write(serial, 
    boost::asio::buffer(&send_msg.front(), 5), 
    boost::bind(&SerialAPI::send_multi_complete, 
    this, 
    boost::asio::placeholders::error)); 
}

Logging Info

I've added logging in SerialAPI.h:

void sendmulti(const std::string& msg)
{
    io.post(boost::bind(&SerialAPI::do_multi_send, this, msg));
    FBLOG_TRACE("sendmulti()", "SerialAPI.h--sendmulti() works");
}

...and in 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 char *cMsg = msg.c_str();

    // logging
    FBLOG_TRACE("do_multi_send()", "debug cMsg: " << cMsg);

    for(int i = 0; i < 5; i++) {
        send_msg.push_back(cMsg[i]); // store in write buffer 
    }

    if (!write_in_progress) // if nothing is currently being written, then start 
    send_multi_start();
}

And this is the minimal output I get:

31 [2756] INFO FireBreath <> - ..\..\..\..\src\PluginAuto\Win\np_winmain.cpp:29 - NP_Initialize - Initialization done
34 [2756] INFO FireBreath <> - ..\..\src\NpapiCore\NpapiPluginModule_NPP.cpp:150 - FB::Npapi::NpapiPluginModule::NPP_New - NPP_New: 02F60E30
37 [2756] INFO FireBreath <> - ..\..\src\PluginCore\PluginCore.cpp:40 - FB::PluginCore::setPlatform - os: 00D2E8B0; browser: NPAPI
85 [2756] INFO FireBreath <> - ..\..\src\NpapiCore\NpapiPluginModule_NPP.cpp:208 - FB::Npapi::NpapiPluginModule::NPP_Destroy - NPP_Destroy: 02F60E30
15338 [2756] INFO FireBreath <> - ..\..\src\NpapiCore\NpapiPluginModule_NPP.cpp:150 - FB::Npapi::NpapiPluginModule::NPP_New - NPP_New: 02F60EB0
15340 [2756] INFO FireBreath <> - ..\..\src\PluginCore\PluginCore.cpp:40 - FB::PluginCore::setPlatform - os: 00D2E8B0; browser: NPAPI

Do it seem as though I have everything set up right?

On another note, I can't debug the app for the life of me. I've followed all the steps I can find for debugging Chrome on Windows, have the correct process chosen, breakpoints set, they're actually active in VS2010, yet when I run the app or send the function, they don't hit.

Thanks!

Was it helpful?

Solution

To start with, there is no reason to copy the std::string to a char*, just use the .c_str() that is already there.

void SerialAPI::do_multi_send(const std::string& msg) 
{
    bool write_in_progress = !send_msg.empty(); // is there anything currently being written?   
    char *cMsg = msg.c_str();

    for(int i = 0; i < sLength; i++) {
        send_msg.push_back(cMsg[i]); // store in write buffer; this function requires a char
    }
    if (!write_in_progress) // if nothing is currently being written, then start 
        send_multi_start(); 
}

void SerialAPI::send_multi_start(void) 
{
    boost::asio::async_write(serial, 
    boost::asio::buffer(&send_msg.front(), 5), 
    boost::bind(&SerialAPI::send_multi_complete, 
    this, 
    boost::asio::placeholders::error)); 
}

Other than that, I don't know what to suggest; however, I don't think this is the problem. every 40ms is not really very often; you should be able to do this quite a bit in that period of time. I'd add some logging (see the logging page on the firebreath website) and see if you can track the order that things are happening and what is actually running. This will give you a lot more visibility into what is going on.

OTHER TIPS

This is classic producer-consumer design pattern which is widely used with these plugins.

http://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top