Added functions for changing the port settings.
authordherring <dherring@c4713191-9d10-0410-88e3-c3bdc45b6a85>
Tue, 4 Apr 2006 19:26:05 +0000 (19:26 +0000)
committerdherring <dherring@c4713191-9d10-0410-88e3-c3bdc45b6a85>
Tue, 4 Apr 2006 19:26:05 +0000 (19:26 +0000)
SerialPort.cpp
SerialPort.h

index 9710f32..cb4e6e0 100644 (file)
@@ -2,7 +2,6 @@
 \r
 // Specify the MSWindows naming scheme (COM1, COM2, ...)\r
 #define _TTY_WIN_\r
-#include <qextserialbase.h>\r
 #include <qextserialport.h>\r
 \r
 #include <QString>\r
@@ -13,39 +12,108 @@ SerialPort::SerialPort(QWidget *parent) : QDialog(parent)
        serial=new QextSerialPort();\r
        timer=new QTimer(this);\r
        connect(timer, SIGNAL(timeout()), this, SLOT(checkPort()));\r
+       \r
+       // default settings\r
+       port="COM1";\r
+       baud=BAUD57600;\r
+       bits=DATA_8;\r
+       par=PAR_NONE;\r
+       stop=STOP_1;\r
+       flow=FLOW_OFF;\r
+       timeout=100;\r
+       \r
+       updateDisplay();\r
 }\r
 \r
-void SerialPort::on_openButton_clicked()\r
+void SerialPort::updateDisplay()\r
 {\r
-       // Make sure things are clear\r
-       serial->close();\r
-       emit(portClosed());\r
+       if(port=="COM1")\r
+       {\r
+               choosePort->setCurrentIndex(0);\r
+       }\r
+       else\r
+       {\r
+               choosePort->setCurrentIndex(1);\r
+       }\r
+       \r
+       switch(baud)\r
+       {\r
+               case BAUD9600:\r
+                       chooseBaudRate->setCurrentIndex(0);\r
+                       break;\r
+               case BAUD19200:\r
+                       chooseBaudRate->setCurrentIndex(1);\r
+                       break;\r
+               case BAUD38400:\r
+                       chooseBaudRate->setCurrentIndex(2);\r
+                       break;\r
+               case BAUD57600:\r
+                       chooseBaudRate->setCurrentIndex(3);\r
+                       break;\r
+               case BAUD115200:\r
+                       chooseBaudRate->setCurrentIndex(4);\r
+                       break;\r
+               default:\r
+                       // no matching index\r
+                       break;\r
+       }\r
        \r
+       chooseDataBits->setCurrentIndex((int)bits);\r
+       chooseParity->setCurrentIndex((int)par);\r
+       chooseStopBits->setCurrentIndex((int)stop);\r
+       chooseFlowControl->setCurrentIndex((int)flow);\r
+}\r
+\r
+void SerialPort::on_openButton_clicked()\r
+{\r
        // read in all the settings and open the port\r
-       serial->setPortName(choosePort->currentText());\r
+       port=choosePort->currentText();\r
        switch(chooseBaudRate->currentIndex())\r
        {\r
                case 0:\r
-                       serial->setBaudRate(BAUD9600);\r
+                       baud=BAUD9600;\r
                        break;\r
                case 1:\r
-                       serial->setBaudRate(BAUD19200);\r
+                       baud=BAUD19200;\r
                        break;\r
                case 2:\r
-                       serial->setBaudRate(BAUD38400);\r
+                       baud=BAUD38400;\r
                        break;\r
                case 3:\r
-                       serial->setBaudRate(BAUD57600);\r
+                       baud=BAUD57600;\r
                        break;\r
                case 4:\r
-                       serial->setBaudRate(BAUD115200);\r
+                       baud=BAUD115200;\r
        }\r
-       serial->setDataBits( (DataBitsType)(chooseDataBits->currentIndex()) );\r
-       serial->setParity( (ParityType)(chooseParity->currentIndex()) );\r
-       serial->setStopBits( (StopBitsType)(chooseStopBits->currentIndex()) );\r
-       serial->setFlowControl( (FlowType)(chooseFlowControl->currentIndex()) );\r
-       serial->setTimeout(0,100);\r
+       bits=(DataBitsType)(chooseDataBits->currentIndex());\r
+       par=(ParityType)(chooseParity->currentIndex());\r
+       stop=(StopBitsType)(chooseStopBits->currentIndex());\r
+       flow=(FlowType)(chooseFlowControl->currentIndex());\r
        \r
+       openPort();\r
+}\r
+\r
+void SerialPort::on_cancelButton_clicked()\r
+{\r
+       // reset the display\r
+       updateDisplay();\r
+}\r
+\r
+void SerialPort::openPort()\r
+{\r
+       // Make sure things are clear\r
+       serial->close();\r
+       emit(portClosed());\r
+       \r
+       // set the parameters\r
+       serial->setBaudRate(baud);\r
+       serial->setDataBits(bits);\r
+       serial->setParity(par);\r
+       serial->setStopBits(stop);\r
+       serial->setFlowControl(flow);\r
+       serial->setTimeout(0,timeout);\r
+       \r
+       // open the port\r
        if(serial->open())\r
        {\r
                timer->start();\r
@@ -79,3 +147,39 @@ void SerialPort::checkPort()
                emit(dataReady());\r
        }\r
 }\r
+\r
+void SerialPort::setPort(int p)\r
+{\r
+       if(p==1) port="COM1";\r
+       if(p==2) port="COM2";\r
+}\r
+\r
+void SerialPort::setBaudRate(BaudRateType baud)\r
+{\r
+       this->baud=baud;\r
+}\r
+\r
+void SerialPort::setDataBits(DataBitsType bits)\r
+{\r
+       this->bits=bits;\r
+}\r
+\r
+void SerialPort::setParity(ParityType par)\r
+{\r
+       this->par=par;\r
+}\r
+\r
+void SerialPort::setStopBits(StopBitsType stop)\r
+{\r
+       this->stop=stop;\r
+}\r
+\r
+void SerialPort::setFlowControl(FlowType flow)\r
+{\r
+       this->flow=flow;\r
+}\r
+\r
+void SerialPort::setTimeout(ulong ms)\r
+{\r
+       this->timeout=ms;\r
+}\r
index 034e845..4d72b25 100644 (file)
@@ -3,8 +3,11 @@
 \r
 #include "ui_SerialPort.h"\r
 #include <QTimer>\r
-class QextSerialPort;\r
 \r
+#include <qextserialbase.h>\r
+\r
+class QextSerialPort;\r
+       \r
 class SerialPort : public QDialog, private Ui::SerialPort\r
 {\r
        Q_OBJECT\r
@@ -12,15 +15,34 @@ class SerialPort : public QDialog, private Ui::SerialPort
        public:\r
                SerialPort(QWidget *parent=0);\r
                int receive(int count, char *bytes);\r
+               // allow programmatic setting of the parameters\r
+               // changes don't take effect until the next time the port opens\r
+               void setPort(int p);\r
+               void setBaudRate(BaudRateType);\r
+               void setDataBits(DataBitsType);\r
+               void setParity(ParityType);\r
+               void setStopBits(StopBitsType);\r
+               void setFlowControl(FlowType);\r
+               void setTimeout(ulong);\r
        \r
        private:\r
+               void updateDisplay();\r
                QextSerialPort *serial;\r
                QTimer *timer;\r
+               QString port;\r
+               BaudRateType baud;\r
+               DataBitsType bits;\r
+               ParityType par;\r
+               StopBitsType stop;\r
+               FlowType flow;\r
+               ulong timeout;\r
        \r
        private slots:\r
                void on_openButton_clicked();\r
+               void on_cancelButton_clicked();\r
        \r
        public slots:\r
+               void openPort();\r
                void closePort();\r
                void send(int count, char *bytes);\r
                void checkPort();\r