2012-01-28 41 views
5

私はUbuntuでLibSerialを使用してシリアルポートのデータを読み書きしています。C/C++とLibSerialを使用したUbuntuのシリアルポートの読み込みと書き込み

現時点ではシリアルポート経由で文字列を送受信できますが、コードはうまく機能しません:特に私は読み取り機能を制御して、フロープログラムをbloickingせずに別のコマンドを送信するために読むべき情報がないときに何かが読んで終了する。

私がやりたい:

  • は、今、私はできるよ答え

のために別のコマンドに

  • 待ちを書く答え
  • するためのコマンドに
  • 待ちを書きます最初のコマンドを送信し、whileループでread関数を使用して答えを読み取るが、私は何もできない。 whileループが終了しないので、プログラムが読み込みを続けるため、2番目のコマンドを送信できません。

    お願いします。

    これは私が使用しているコードです: を(読み取りをし、関数を書くには、コードの末尾にある)

    #include <SerialStream.h> 
    #include <iostream> 
    #include <unistd.h> 
    #include <cstdlib> 
    #include <string> 
    
    int 
    main(int argc, 
         char** argv ) 
    { 
        // 
        // Open the serial port. 
        // 
        using namespace std; 
        using namespace LibSerial ; 
        SerialStream serial_port ; 
        char c; 
        serial_port.Open("/dev/ttyACM0") ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " 
            << "Error: Could not open serial port." 
            << std::endl ; 
         exit(1) ; 
        } 
        // 
        // Set the baud rate of the serial port. 
        // 
        serial_port.SetBaudRate(SerialStreamBuf::BAUD_9600) ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "Error: Could not set the baud rate." << 
    std::endl ; 
         exit(1) ; 
        } 
        // 
        // Set the number of data bits. 
        // 
        serial_port.SetCharSize(SerialStreamBuf::CHAR_SIZE_8) ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "Error: Could not set the character size." << 
    std::endl ; 
         exit(1) ; 
        } 
        // 
        // Disable parity. 
        // 
        serial_port.SetParity(SerialStreamBuf::PARITY_NONE) ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "Error: Could not disable the parity." << 
    std::endl ; 
         exit(1) ; 
        } 
        // 
        // Set the number of stop bits. 
        // 
        serial_port.SetNumOfStopBits(1) ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "Error: Could not set the number of stop bits." 
            << std::endl ; 
         exit(1) ; 
        } 
        // 
        // Turn off hardware flow control. 
        // 
        serial_port.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE) ; 
        if (! serial_port.good()) 
        { 
         std::cerr << "Error: Could not use hardware flow control." 
            << std::endl ; 
         exit(1) ; 
        } 
        // 
        // Do not skip whitespace characters while reading from the 
        // serial port. 
        // 
        // serial_port.unsetf(std::ios_base::skipws) ; 
        // 
        // Wait for some data to be available at the serial port. 
        // 
        // 
        // Keep reading data from serial port and print it to the screen. 
        // 
        // Wait for some data to be available at the serial port. 
        // 
        while(serial_port.rdbuf()->in_avail() == 0) 
        { 
         usleep(100) ; 
        } 
    
    
        char out_buf[] = "check"; 
        serial_port.write(out_buf, 5); <-- FIRST COMMAND 
        while(1 ) 
        { 
         char next_byte; 
         serial_port.get(next_byte); HERE I RECEIVE THE FIRST ANSWER 
         std::cerr << next_byte; 
    
        } 
        std::cerr << std::endl ; 
        return EXIT_SUCCESS ; 
    } 
    
  • 答えて

    4

    私はあなただけであなたの最後のwhileループの条件としてwhile(serial_port.rdbuf()->in_avail() > 0)を使用する必要があると思います。その後、利用可能なすべてのデータ(「回答」)が読み込まれ、その後に2番目のコマンドを送信することができます。

    +0

    の問題は、私が使用している場合** serial_port.rdbuf() - > in_avail()> 0 **それは決してwhileループに入りません。 シリアルポートにはデータがありますが、プログラムはそれらを読み取らないので、非常に奇妙なようです。 逆に、** while(1)**を使用すると正しく読み込まれます。 –

    +0

    それでは、実際の答えのすべてのバイトが読み込まれた後、どの文字が得られますか?あなたはその文字をチェックすることができます(私は '\ 0'または '\ n'でなければならないと思います)。 –

    1

    tryとcatchを使用すると便利です。 :)

    これを試してみてください: グローバルポインタSerialPort * puがすでに宣言されていて、ポートが開かれました。

    int rxstring(char *cstr, unsigned int len, bool print_str) 
    
    { 
    
    char temp=0; 
    int i=0; 
    while(temp!='\n') 
    { 
        try 
        { 
         temp=pu->ReadByte(100); 
        } 
        catch(SerialPort::ReadTimeout &e) 
        { 
         //cout<<"Read Timeout"<<endl; 
         return 1; 
        } 
        if((temp!='\n')&&(temp!=0)&&(temp!=' ')) 
        { 
         cstr[i]=temp; 
         ++i; 
         //cout<<i++<<temp<<'x'<<endl; 
        } 
    } 
    cstr[i]='\0'; 
    if(print_str) 
        puts(cstr); 
    return 0; 
    } 
    

    参考: http://libserial.sourceforge.net/doxygen/class_serial_port.html#a15

    関連する問題