1
votes

I have to read many(8) serial devices on my project. They are Pantilt, Camera, GPS, Compass etc. they all are RS232 devices but they have different command structure and behavior. e.g GPS starts sending data as soon as I open the port. where as PanTilt and Camera only responds when I send specific commands to them.

I use following environment

  • OS: Ubuntu 11.10
  • Language: C++
  • Framework: Qt 4.7

For PanTilt and Camera I want to develop function like this.

int SendCommand(string& command, string& response)
{ 
    port.write(command, strlen(command));
    while(1)
    {
       if(response contains '\n') break;
       port.read(response) // Blocking Read
    } 
    return num_of_bytes_read;
}

I want to implement this way as this function will be used as building block for more complex algorithm like this...

SendCoammd("GET PAN ANGLE", &angle);
  if(angle > 60)
     SendCommand("STOP PAN", &stopped?);
     if(stopped? == true)
        SendCommand("REVERSE PAN DIRECTION", &revesed?);
        if(reversed? == true)
           SendCommand("START PAN", &ok);

To do something like this I need strict synchronous behavior. Anybody has any idea how to approach this?

3
(unrelated nitpicker note: passing string & is not a good idea, probably you want const string & or just string; also, strlen on a string makes no sense, you should use its size method)Matteo Italia

3 Answers

1
votes

I found this tutorial very intresting and helpful. http://www.webalice.it/fede.tft/serial_port/serial_port.html

It shows how boost::asio can be used to perform both sync and async read/write.

Thank You for the help!

0
votes

what is the problem of using standard file API?

fd = open("/dev/ttyX");
write(fd, command.cstr(), command.size());
vector v(MAX_SIZE);
read(fd,&v[0], MAX_SIZE);
0
votes

Low-level communication with serial port in Qt can be established with QExtSerialPort library that partially implements QIODevice interface.

The high level protocols you have to implement yourself.