From e4d72be6e2d7e5de3703fe3cda0fd107e47c19cb Mon Sep 17 00:00:00 2001 From: Olivier Delbeke Date: Mon, 31 Mar 2014 07:55:09 +0200 Subject: [PATCH] Added the possibility to specify the baudrate of the GPS NMEA device --- plugins/common/serialport.hpp | 35 ++++++++++++++++++++++++++++++++--- plugins/gpsnmea/README | 4 ++++ plugins/gpsnmea/gpsnmea.cpp | 12 ++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/plugins/common/serialport.hpp b/plugins/common/serialport.hpp index bd367ff..469c5f3 100644 --- a/plugins/common/serialport.hpp +++ b/plugins/common/serialport.hpp @@ -15,11 +15,14 @@ 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); diff --git a/plugins/gpsnmea/README b/plugins/gpsnmea/README index 3a129d3..f8d0f0f 100644 --- a/plugins/gpsnmea/README +++ b/plugins/gpsnmea/README @@ -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" diff --git a/plugins/gpsnmea/gpsnmea.cpp b/plugins/gpsnmea/gpsnmea.cpp index dcac3e6..618ea31 100644 --- a/plugins/gpsnmea/gpsnmea.cpp +++ b/plugins/gpsnmea/gpsnmea.cpp @@ -374,6 +374,7 @@ extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map config) :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b") { + int baudrate = 0; location =new Location(re, mUuid); VehicleProperty::registerProperty(GPSTIME,[](){ return new BasicPropertyType(GPSTIME,0); }); @@ -417,6 +418,11 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map conf std::string btaddapter = config["bluetoothAdapter"]; + if(config.find("baudrate")!= config.end()) + { + baudrate = boost::lexical_cast( config["baudrate"] ); + } + if(config.find("device")!= config.end()) { std::string dev = config["device"]; @@ -428,6 +434,12 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map conf device = new SerialPort(dev); + if(baudrate!=0) + { + if((static_cast(device))->setSpeed(baudrate)) + DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl; + } + if(!device->open()) { DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<