]> git.tdb.fi Git - r2c2.git/commitdiff
Use IO::Serial in the Intellibox driver
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Jan 2013 10:50:50 +0000 (12:50 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Jan 2013 10:50:50 +0000 (12:50 +0200)
This code is untested and may contain bugs.

source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h

index 732e164c01b42660122d89fa15058b36fb94efc6..09e4792e36a0d5837ef12b14a48f7ce9fd04fbac 100644 (file)
@@ -14,44 +14,28 @@ using namespace Msp;
 namespace R2C2 {
 
 Intellibox::Intellibox(const string &dev):
+       serial(dev),
        power(false),
        halted(false),
        update_sensors(false),
        command_sent(false)
 {
-       serial_fd = ::open(dev.c_str(), O_RDWR);
-       if(serial_fd<0)
-               throw Exception("Couldn't open serial port\n");
+       static unsigned baud[]= { 2400, 4800, 9600, 19200, 0 };
 
-       static unsigned baud[]=
-       {
-                2400, B2400,
-                4800, B4800,
-                9600, B9600,
-               19200, B19200,
-               0
-       };
-
-       termios attr;
-       tcgetattr(serial_fd, &attr);
-       cfmakeraw(&attr);
-       attr.c_cflag |= CSTOPB;
+       serial.set_stop_bits(2);
 
        bool ok = false;
        bool p50 = false;
-       for(unsigned i=0; baud[i]; i+=2)
+       for(unsigned i=0; baud[i]; ++i)
        {
-               cfsetospeed(&attr, baud[i+1]);
-               tcsetattr(serial_fd, TCSADRAIN, &attr);
-
-               write(serial_fd, "\xC4", 1);
+               serial.set_baud_rate(baud[i]);
+               serial.put('\xC4');
 
-               pollfd pfd = { serial_fd, POLLIN, 0 };
-               if(poll(&pfd, 1, 500)>0)
+               if(IO::poll(serial, IO::P_INPUT, 500*Time::msec))
                {
                        IO::print("IB detected at %d bits/s\n", baud[i]);
                        char buf[2];
-                       p50 = (read(serial_fd, buf, 2)==2);
+                       p50 = (serial.read(buf, 2)==2);
                        ok = true;
                        break;
                }
@@ -61,7 +45,7 @@ Intellibox::Intellibox(const string &dev):
                throw Exception("IB not detected");
 
        if(p50)
-               write(serial_fd, "xZzA1\r", 6);
+               serial.write("xZzA1\r", 6);
 
        command(CMD_STATUS);
 }
@@ -327,8 +311,7 @@ void Intellibox::tick()
 
        if(!queue.empty() && command_sent)
        {
-               pollfd pfd = { serial_fd, POLLIN, 0 };
-               if(poll(&pfd, 1, 0)>0)
+               if(IO::poll(serial, IO::P_INPUT, Time::zero))
                {
                        process_reply(t);
                        queue.erase(queue.begin());
@@ -341,7 +324,7 @@ void Intellibox::tick()
        if(!queue.empty())
        {
                const CommandSlot &slot = queue.front();
-               write(serial_fd, slot.data, slot.length);
+               serial.write(reinterpret_cast<const char *>(slot.data), slot.length);
                command_sent = true;
        }
 }
@@ -350,13 +333,12 @@ void Intellibox::flush()
 {
        for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
        {
-               write(serial_fd, i->data, i->length);
-               pollfd pfd = { serial_fd, POLLIN, 0 };
+               serial.write(reinterpret_cast<const char *>(i->data), i->length);
                bool first = true;
-               while(poll(&pfd, 1, (first ? -1 : 0))>0)
+               while(first ? IO::poll(serial, IO::P_INPUT) : IO::poll(serial, IO::P_INPUT, Time::zero))
                {
                        char data[16];
-                       read(serial_fd, data, 16);
+                       serial.read(data, 16);
                        first = false;
                }
        }
@@ -661,7 +643,7 @@ unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
 {
        unsigned pos = 0;
        while(pos<len)
-               pos += read(serial_fd, buf+pos, len-pos);
+               pos += serial.read(reinterpret_cast<char *>(buf+pos), len-pos);
 
        return pos;
 }
index be25860f731d2dd41c6f08e95193ae05de31da5a..d3008d2f8ed200219d630f939d8c100f68f2dfee 100644 (file)
@@ -2,6 +2,7 @@
 #define LIBR2C2_INTELLIBOX_H_
 
 #include <map>
+#include <msp/io/serial.h>
 #include <msp/time/timestamp.h>
 #include "driver.h"
 
@@ -112,7 +113,7 @@ private:
                unsigned length;
        };
 
-       int serial_fd;
+       Msp::IO::Serial serial;
        bool power;
        bool halted;
        std::map<unsigned, Locomotive> locos;