Added the possibility to specify the baudrate of the GPS NMEA device
authorOlivier Delbeke <olivier.delbeke@awtce.be>
Mon, 31 Mar 2014 05:55:09 +0000 (07:55 +0200)
committerOlivier Delbeke <olivier.delbeke@awtce.be>
Mon, 31 Mar 2014 05:55:09 +0000 (07:55 +0200)
plugins/common/serialport.hpp
plugins/gpsnmea/README
plugins/gpsnmea/gpsnmea.cpp

index bd367ff..469c5f3 100644 (file)
 
 class SerialPort: public AbstractIo
 {
+private:
+       speed_t speed;
+
 public:
        SerialPort(std::string _tty)
                :tty(_tty)
        {
-
+               speed = B9600;
        }
 
        ~SerialPort()
@@ -27,6 +30,32 @@ public:
                close();
        }
 
+       int setSpeed(int newspeed)
+       {
+               int ret = 0;
+               switch(newspeed)
+               {
+                       case 2400: 
+                               speed = B2400;
+                               break;
+                       case 4800: 
+                               speed = B4800;
+                               break;
+                       case 9600: 
+                               speed = B9600;
+                               break;
+                       case 19200: 
+                               speed = B19200;
+                               break;
+                       case 38400: 
+                               speed = B38400;
+                               break;
+                       default:
+                               ret = -EINVAL;
+               }
+               return ret;
+       }
+
        bool open()
        {
                fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
@@ -53,8 +82,8 @@ public:
 
                //oldtio.c_cc[VEOL]     = '\r';
 
-               cfsetispeed(&oldtio, B9600);
-               cfsetospeed(&oldtio, B9600);
+               cfsetispeed(&oldtio, speed);
+               cfsetospeed(&oldtio, speed);
 
                tcflush(fd, TCIFLUSH);
                tcsetattr(fd, TCSANOW, &oldtio);
index 3a129d3..f8d0f0f 100644 (file)
@@ -12,6 +12,7 @@ To use the Database plugin, add the following to the "sources" array in /etc/amb
        "name" : "gpsnmea",
        "path" : "/usr/lib/automotive-message-broker/gpsnmea.so",
        "device" : "/dev/ttyUSB0",
+       "baudrate" : "4800",
        "bluetoothAdapter" : "00:00:00:00:00:00"
 }
 
@@ -26,6 +27,9 @@ path to plugin on the filesystem.
 "device"
 gps device.  This must be a serial device.  It can also be a bluetooth address.
 
+"baudrate"
+Baudrate for serial devices : 2400,4800,9600,19200 or 38400. Default is 9600.
+
 Default: none
 
 "bluetoothAdapter"
index dcac3e6..618ea31 100644 (file)
@@ -374,6 +374,7 @@ extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<str
 GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> config)
        :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b")
 {
+       int baudrate = 0;
        location =new Location(re, mUuid);
 
        VehicleProperty::registerProperty(GPSTIME,[](){ return new BasicPropertyType<double>(GPSTIME,0); });
@@ -417,6 +418,11 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> conf
 
        std::string btaddapter = config["bluetoothAdapter"];
 
+       if(config.find("baudrate")!= config.end())
+       {
+               baudrate = boost::lexical_cast<int>( config["baudrate"] );
+       }
+
        if(config.find("device")!= config.end())
        {
                std::string dev = config["device"];
@@ -428,6 +434,12 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> conf
 
                device = new SerialPort(dev);
 
+               if(baudrate!=0)
+               {
+                       if((static_cast<SerialPort*>(device))->setSpeed(baudrate))
+                               DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl;
+               }
+
                if(!device->open())
                {
                        DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<<config["device"]<<endl;