dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: libserial usage

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    libserial usage

    Hi,

    I'm trying to implement serial communication with a microcotronller by using the libserial library. http://libserial.sourceforge.net/med...erial_Tutorial

    I'm using a USB-serial adapter and it works perfectly with kermit. I do this setup:
    Code:
    set line /dev/ttyUSB0 
    set speed 9600
    set carrier-watch off
    set flow-control none
    This is the kermit output when I send a wrong command:
    Code:
    IDAC V2.0>:hola                 
    
    COMMAND NOT FOUND
    
    IDAC V2.0>:
    This means... the uC sends me IDAC V2.0> (which is kind of pwd) and then I write the "hola" command. The answer from the uC is "COMMAND NOT FOUND"

    Ok, everything works under kermit. Then I tried a simple program in C++ to do this:
    Code:
    #include <SerialStream.h>
    #include <string>
    #include <iostream>
    
    using namespace std;
    using namespace LibSerial;
    
    int main(){
     char next_char[100];
     int i;
     SerialStream my_serial_stream ;
    
     my_serial_stream.Open( "/dev/ttyUSB0" ) ;
    my_serial_stream.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
    my_serial_stream.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
    my_serial_stream.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
    my_serial_stream.SetParity( SerialStreamBuf::PARITY_NONE ) ;
    my_serial_stream.SetNumOfStopBits(1) ;
    
     my_serial_stream << "hola\n";
     my_serial_stream.read(next_char,10);
     cout<<next_char;
    
     my_serial_stream.Close();
     return 0;
    }
    and the ouput is confusing since it gets truncated in different positions each time I run the program:
    Code:
    fede@jenna:/usr2/fuentes/prueba$ ./a.out
    hol��ӷfede@jenna:/usr2/fuentes/prueba$ ./a.out
    hola
    
    COfede@jenna:/usr2/fuentes/prueba$
    First of all, I wonder why I see what I send to the serial port... Then, I wonder why I can't get the complete answer from the uC which should be COMMAND NOT FOUND and the path name.

    I'm really stuck and I will appreciate very much any help!!! Thanks,

    fede.

  2. #2
    Join Date
    Mar 2001
    Posts
    2,527

    Re: libserial usage

    Try sending a carriage return at the beginning of the first send from your app. This will get the prompt going on the controller at the other end. Perhaps the controller is not yet ready to recieve your incorrect command.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Feb 2009
    Posts
    3

    Re: libserial usage

    It didn't work...

    I tried with:
    Code:
     my_serial_stream << "\rhola\n";
    getting an strange result like this:
    Code:
    fede@jenna:/usr2/fuentes/prueba$ ./a.out
    
    ��ɷfede@jenna:/usr2/fuentes/prueba$ ./a.out
    
    
    �"ȷfede@jenna:/usr2/fuentes/prueba$ ./a.out
    
    
    COM�fede@jenna:/usr2/fuentes/prueba$ ./a.out
    
    
    COMMANDfede@jenna:/usr2/fuentes/prueba$ ./a.out
    
    
    COMMANDfede@jenna:/usr2/fuentes/prueba$
    Then, I tried with \r instead of \n and it looks to be the same...

    fede.

  4. #4
    Join Date
    Feb 2009
    Posts
    3

    Re: libserial usage

    Ok, I think I already figure it out!!!

    The problem raised from the fact that VMin=1 and VTime=0 by default. I don't fully understand the meaning of VMin and VTime yet, but it seems that this setup is good if you expect only one character and it is a bad thing to use these values if you expect for many characters in a single burst . After reading the link found in the libserial-doc pages: http://www.unixwiz.net/techtips/termios-vmin-vtime.html, I changed the VTime=1 and VMin=100 and things started working (if VTime=0, you must be sure to select the exact number for VMin otherwise the read() never returns).

    This is the code:
    Code:
    #include <SerialStream.h>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    using namespace LibSerial;
    
    int main(){
     char next_char[100];
     int i;
    
     SerialStream my_serial_stream;
    
     my_serial_stream.Open( "/dev/ttyUSB0") ;
    my_serial_stream.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
    my_serial_stream.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
    my_serial_stream.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
    my_serial_stream.SetParity( SerialStreamBuf::PARITY_NONE ) ;
    my_serial_stream.SetNumOfStopBits(1) ;
    
     my_serial_stream.SetVTime(1);
     my_serial_stream.SetVMin(100);
    
     my_serial_stream << "hola\n";
     my_serial_stream.read(next_char,100);
    
     cout<<next_char;
    
     my_serial_stream.Close();
     return 0;
    }
    and the output was as expected.

    Thanks a lot anyhow!!!! Ciao,

    fede.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)