2012-01-28 8 views
5

ile Ubuntu'daki seri bağlantı noktasını okuyun ve yazın Seri bağlantı noktasında veri okumak ve yazmak için Ubuntu üzerinde LibSerial kullanıyorum.C/C++ ve LibSerial

Şu anda, seri bağlantı noktası üzerinden dizeleri yazabiliyor ve alabiliyorum, ancak kodum çok iyi çalışmıyor: özellikle Okuma işlevini yalnızca okumak için okuma işlevini kontrol etmek istiyorum Akış programını bloklamadan başka bir komut göndermek için okunacak bilgi olmadığında okunacak ve çıkacak bir şey var.

yapmak istiyorum:

  • sonra başka komutunu Şimdi cevap

için

  • Bekle yaz yanıtı için bir komutunu
  • Bekle yaz, ben açabiliyorum ilk komutu göndermek ve bir süre döngüsünde okuma işlevini kullanarak cevabı okumak ancak başka bir şey yapamam. İkinci komutu gönderemiyorum çünkü while döngüsü hiçbir zaman çıkmıyor, böylece program okumaya devam ediyor.

    Yardım edebilir misiniz, lütfen?

    Bu

    i kullanıyorum kodudur: (Oku ve kod sonundadır fonksiyon yazmak) Ben sadece döngü sırasında son şartı olarak while(serial_port.rdbuf()->in_avail() > 0) kullanmaya ihtiyacım var

    #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 ; 
    } 
    
  • cevap

    4

    . Sonra tüm mevcut verileri okuyacaktır (“cevap”) ve bundan sonra ikinci komutu gönderebilirsiniz.

    +0

    sorun olduğunu ben kullanırsanız ** serial_port.rdbuf() -> in_avail()> 0 ** asla süre döngüde girer. Çok garip görünüyor çünkü seri portda veriler var ama program onları okuyor. Aksine, ** kullanırken (1) ** doğru okur. –

    +0

    Peki o zaman, gerçek cevabın tüm baytları okunduğunda hangi karaktere ulaşırsınız? Bu karakteri kontrol edebilirsiniz ('\ 0' veya '\ n' olması gerektiğini düşünüyorum) ve bunu aldıktan sonra döngüden 'break' yapabilirsiniz. –

    1

    Denemeyi ve yakalamayı kullanmak yardımcı olabilir. :)

    Bunu deneyin: Küresel bir işaretçi SerialPort * pu zaten bildirildi ve bağlantı noktası açıldı.

    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; 
    } 
    

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