Written a while ago... As seen in the tutorial I wrote.
authordherring <dherring@c4713191-9d10-0410-88e3-c3bdc45b6a85>
Tue, 4 Apr 2006 16:59:45 +0000 (16:59 +0000)
committerdherring <dherring@c4713191-9d10-0410-88e3-c3bdc45b6a85>
Tue, 4 Apr 2006 16:59:45 +0000 (16:59 +0000)
MainWindow.cpp [new file with mode: 0644]
MainWindow.h [new file with mode: 0644]
MainWindow.ui [new file with mode: 0644]
SerialPort.cpp [new file with mode: 0644]
SerialPort.h [new file with mode: 0644]
SerialPort.ui [new file with mode: 0644]
ascii85.h [new file with mode: 0644]
main.cpp [new file with mode: 0644]
qtcomm.pro [new file with mode: 0644]

diff --git a/MainWindow.cpp b/MainWindow.cpp
new file mode 100644 (file)
index 0000000..9b618e1
--- /dev/null
@@ -0,0 +1,66 @@
+#include "MainWindow.h"\r
+#include <QByteArray>\r
+#include <QString>\r
+\r
+MainWindow::MainWindow(SerialPort *sp, QWidget *parent) : QMainWindow(parent)\r
+{\r
+       setupUi(this);\r
+       this->sp=sp;\r
+       \r
+       QObject::connect(sp, SIGNAL(portOpened()), this, SLOT(portOn()));\r
+       QObject::connect(sp, SIGNAL(portClosed()), this, SLOT(portOff()));\r
+       QObject::connect(sp, SIGNAL(dataReady()), this, SLOT(dataListener()));\r
+       \r
+       portOpen=false;\r
+}\r
+\r
+void MainWindow::on_actionOpen_triggered(bool checked)\r
+{\r
+       sp->show();\r
+}\r
+\r
+void MainWindow::on_actionClose_triggered(bool checked)\r
+{\r
+       sp->closePort();\r
+}\r
+\r
+void MainWindow::on_actionExit_triggered(bool checked)\r
+{\r
+       this->close();\r
+}\r
+\r
+void MainWindow::on_sendButton_clicked()\r
+{\r
+       QString text=inputText->text();\r
+       QByteArray bytes=text.toAscii();\r
+       sp->send(bytes.size(),bytes.data());\r
+       if(portOpen)\r
+       {\r
+               serialLog->append("<font color=blue>"+text+"</font>");\r
+       }\r
+       else\r
+       {\r
+               serialLog->append("<font color=red>"+text+"</font>");\r
+       }\r
+       inputText->clear();\r
+}\r
+\r
+void MainWindow::portOn()\r
+{\r
+       this->statusBar()->showMessage("Port open");\r
+       portOpen=true;\r
+}\r
+\r
+void MainWindow::portOff()\r
+{\r
+       this->statusBar()->showMessage("Port closed");\r
+       portOpen=false;\r
+}\r
+\r
+void MainWindow::dataListener()\r
+{\r
+       char buffer[256];\r
+       int rec=sp->receive(255,buffer);\r
+       buffer[rec]='\0';\r
+       serialLog->append(buffer);\r
+}\r
diff --git a/MainWindow.h b/MainWindow.h
new file mode 100644 (file)
index 0000000..478b258
--- /dev/null
@@ -0,0 +1,25 @@
+#include "ui_MainWindow.h"\r
+#include "SerialPort.h"\r
+\r
+class MainWindow : public QMainWindow, private Ui::MainWindow\r
+{\r
+       Q_OBJECT\r
+       \r
+       public:\r
+               MainWindow(SerialPort *sp, QWidget *parent=0);\r
+       \r
+       private:\r
+               SerialPort *sp;\r
+               bool portOpen;\r
+       \r
+       private slots:\r
+               void on_actionOpen_triggered(bool checked=false);\r
+               void on_actionClose_triggered(bool checked=false);\r
+               void on_actionExit_triggered(bool checked=false);\r
+               void on_sendButton_clicked();\r
+       \r
+       public slots:\r
+               void portOn();\r
+               void portOff();\r
+               void dataListener();\r
+};\r
diff --git a/MainWindow.ui b/MainWindow.ui
new file mode 100644 (file)
index 0000000..77a2081
--- /dev/null
@@ -0,0 +1,148 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>448</width>
+    <height>287</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>QtComm</string>
+  </property>
+  <widget class="QWidget" name="centralwidget" >
+   <widget class="QLabel" name="label" >
+    <property name="geometry" >
+     <rect>
+      <x>9</x>
+      <y>9</y>
+      <width>347</width>
+      <height>20</height>
+     </rect>
+    </property>
+    <property name="text" >
+     <string>Serial Log:</string>
+    </property>
+   </widget>
+   <widget class="QLabel" name="label_2" >
+    <property name="geometry" >
+     <rect>
+      <x>9</x>
+      <y>180</y>
+      <width>347</width>
+      <height>20</height>
+     </rect>
+    </property>
+    <property name="text" >
+     <string>Input Text:</string>
+    </property>
+   </widget>
+   <widget class="QLineEdit" name="inputText" >
+    <property name="geometry" >
+     <rect>
+      <x>9</x>
+      <y>208</y>
+      <width>347</width>
+      <height>23</height>
+     </rect>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="sendButton" >
+    <property name="geometry" >
+     <rect>
+      <x>362</x>
+      <y>206</y>
+      <width>77</width>
+      <height>27</height>
+     </rect>
+    </property>
+    <property name="text" >
+     <string>Send</string>
+    </property>
+   </widget>
+   <widget class="QTextEdit" name="serialLog" >
+    <property name="geometry" >
+     <rect>
+      <x>9</x>
+      <y>35</y>
+      <width>431</width>
+      <height>139</height>
+     </rect>
+    </property>
+    <property name="readOnly" >
+     <bool>true</bool>
+    </property>
+   </widget>
+  </widget>
+  <widget class="QMenuBar" name="menubar" >
+   <property name="geometry" >
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>448</width>
+     <height>23</height>
+    </rect>
+   </property>
+   <widget class="QMenu" name="menuFile" >
+    <property name="title" >
+     <string>File</string>
+    </property>
+    <addaction name="actionOpen" />
+    <addaction name="actionClose" />
+    <addaction name="separator" />
+    <addaction name="actionExit" />
+   </widget>
+   <addaction name="menuFile" />
+  </widget>
+  <widget class="QStatusBar" name="statusbar" >
+   <property name="geometry" >
+    <rect>
+     <x>0</x>
+     <y>265</y>
+     <width>448</width>
+     <height>22</height>
+    </rect>
+   </property>
+  </widget>
+  <action name="actionOpen" >
+   <property name="text" >
+    <string>Open</string>
+   </property>
+  </action>
+  <action name="actionClose" >
+   <property name="text" >
+    <string>Close</string>
+   </property>
+  </action>
+  <action name="actionExit" >
+   <property name="text" >
+    <string>Exit</string>
+   </property>
+  </action>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>inputText</sender>
+   <signal>returnPressed()</signal>
+   <receiver>sendButton</receiver>
+   <slot>click()</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>272</x>
+     <y>236</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>402</x>
+     <y>245</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
diff --git a/SerialPort.cpp b/SerialPort.cpp
new file mode 100644 (file)
index 0000000..9710f32
--- /dev/null
@@ -0,0 +1,81 @@
+#include "SerialPort.h"\r
+\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
+\r
+SerialPort::SerialPort(QWidget *parent) : QDialog(parent)\r
+{\r
+       setupUi(this);\r
+       serial=new QextSerialPort();\r
+       timer=new QTimer(this);\r
+       connect(timer, SIGNAL(timeout()), this, SLOT(checkPort()));\r
+}\r
+\r
+void SerialPort::on_openButton_clicked()\r
+{\r
+       // Make sure things are clear\r
+       serial->close();\r
+       emit(portClosed());\r
+       \r
+       // read in all the settings and open the port\r
+       serial->setPortName(choosePort->currentText());\r
+       switch(chooseBaudRate->currentIndex())\r
+       {\r
+               case 0:\r
+                       serial->setBaudRate(BAUD9600);\r
+                       break;\r
+               case 1:\r
+                       serial->setBaudRate(BAUD19200);\r
+                       break;\r
+               case 2:\r
+                       serial->setBaudRate(BAUD38400);\r
+                       break;\r
+               case 3:\r
+                       serial->setBaudRate(BAUD57600);\r
+                       break;\r
+               case 4:\r
+                       serial->setBaudRate(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
+       \r
+       if(serial->open())\r
+       {\r
+               timer->start();\r
+               emit(portOpened());\r
+       }\r
+}\r
+\r
+void SerialPort::closePort()\r
+{\r
+       serial->close();\r
+       emit(portClosed());\r
+}\r
+\r
+void SerialPort::send(int count, char *bytes)\r
+{\r
+       serial->writeData(bytes, count);\r
+       serial->flush();\r
+}\r
+\r
+// read in up to count bytes\r
+// return the number of bytes read\r
+int SerialPort::receive(int count, char *bytes)\r
+{\r
+       return serial->readData(bytes, count);\r
+}\r
+\r
+void SerialPort::checkPort()\r
+{\r
+       if(serial->bytesAvailable()>0)\r
+       {\r
+               emit(dataReady());\r
+       }\r
+}\r
diff --git a/SerialPort.h b/SerialPort.h
new file mode 100644 (file)
index 0000000..034e845
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef OPENSERIAL_H\r
+#define OPENSERIAL_H\r
+\r
+#include "ui_SerialPort.h"\r
+#include <QTimer>\r
+class QextSerialPort;\r
+\r
+class SerialPort : public QDialog, private Ui::SerialPort\r
+{\r
+       Q_OBJECT\r
+       \r
+       public:\r
+               SerialPort(QWidget *parent=0);\r
+               int receive(int count, char *bytes);\r
+       \r
+       private:\r
+               QextSerialPort *serial;\r
+               QTimer *timer;\r
+       \r
+       private slots:\r
+               void on_openButton_clicked();\r
+       \r
+       public slots:\r
+               void closePort();\r
+               void send(int count, char *bytes);\r
+               void checkPort();\r
+       \r
+       signals:\r
+               void portOpened();\r
+               void portClosed();\r
+               void dataReady();\r
+};\r
+\r
+#endif\r
diff --git a/SerialPort.ui b/SerialPort.ui
new file mode 100644 (file)
index 0000000..f187f65
--- /dev/null
@@ -0,0 +1,297 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>SerialPort</class>
+ <widget class="QDialog" name="SerialPort" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>181</width>
+    <height>223</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>Open Serial</string>
+  </property>
+  <layout class="QGridLayout" >
+   <property name="margin" >
+    <number>9</number>
+   </property>
+   <property name="spacing" >
+    <number>6</number>
+   </property>
+   <item row="4" column="1" >
+    <widget class="QComboBox" name="chooseStopBits" >
+     <item>
+      <property name="text" >
+       <string>1</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>1.5</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>2</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="2" column="1" >
+    <widget class="QComboBox" name="chooseDataBits" >
+     <property name="currentIndex" >
+      <number>3</number>
+     </property>
+     <item>
+      <property name="text" >
+       <string>5</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>6</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>7</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>8</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="0" column="0" >
+    <widget class="QLabel" name="labelPort" >
+     <property name="text" >
+      <string>Port</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="5" column="1" >
+    <widget class="QComboBox" name="chooseFlowControl" >
+     <item>
+      <property name="text" >
+       <string>Off</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Hardware</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Xon/Xoff</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="1" column="1" >
+    <widget class="QComboBox" name="chooseBaudRate" >
+     <property name="currentIndex" >
+      <number>3</number>
+     </property>
+     <item>
+      <property name="text" >
+       <string>9600</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>19200</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>38400</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>57600</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>115200</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="1" column="0" >
+    <widget class="QLabel" name="labelBaudRate" >
+     <property name="text" >
+      <string>Baud Rate</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="0" column="1" >
+    <widget class="QComboBox" name="choosePort" >
+     <item>
+      <property name="text" >
+       <string>COM1</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>COM2</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="5" column="0" >
+    <widget class="QLabel" name="labelFlowControl" >
+     <property name="text" >
+      <string>Flow Control</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="4" column="0" >
+    <widget class="QLabel" name="labelStopBits" >
+     <property name="text" >
+      <string>Stop Bits</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="0" >
+    <widget class="QLabel" name="labelDataBits" >
+     <property name="text" >
+      <string>Data Bits</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="3" column="0" >
+    <widget class="QLabel" name="labelParity" >
+     <property name="text" >
+      <string>Parity</string>
+     </property>
+     <property name="buddy" >
+      <cstring>labelPort</cstring>
+     </property>
+    </widget>
+   </item>
+   <item row="3" column="1" >
+    <widget class="QComboBox" name="chooseParity" >
+     <item>
+      <property name="text" >
+       <string>None</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Odd</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Even</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Mark</string>
+      </property>
+     </item>
+     <item>
+      <property name="text" >
+       <string>Space</string>
+      </property>
+     </item>
+    </widget>
+   </item>
+   <item row="6" column="0" colspan="2" >
+    <layout class="QHBoxLayout" >
+     <property name="margin" >
+      <number>0</number>
+     </property>
+     <property name="spacing" >
+      <number>6</number>
+     </property>
+     <item>
+      <widget class="QPushButton" name="openButton" >
+       <property name="text" >
+        <string>Open</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="cancelButton" >
+       <property name="text" >
+        <string>Cancel</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <pixmapfunction></pixmapfunction>
+ <tabstops>
+  <tabstop>choosePort</tabstop>
+  <tabstop>chooseBaudRate</tabstop>
+  <tabstop>chooseDataBits</tabstop>
+  <tabstop>chooseParity</tabstop>
+  <tabstop>chooseStopBits</tabstop>
+  <tabstop>chooseFlowControl</tabstop>
+  <tabstop>openButton</tabstop>
+  <tabstop>cancelButton</tabstop>
+ </tabstops>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>openButton</sender>
+   <signal>clicked()</signal>
+   <receiver>SerialPort</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>91</x>
+     <y>240</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>96</x>
+     <y>254</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>cancelButton</sender>
+   <signal>clicked()</signal>
+   <receiver>SerialPort</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>174</x>
+     <y>240</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>179</x>
+     <y>282</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
diff --git a/ascii85.h b/ascii85.h
new file mode 100644 (file)
index 0000000..2095ea7
--- /dev/null
+++ b/ascii85.h
@@ -0,0 +1,64 @@
+// ASCII-85 encoding converts a 32-bit binary number into 5 human-readable ASCII characters\r
+// It is a useful tool for transmitting binary data in plaintext format, such as over a serial port.\r
+// Details on the format can be found in the Adobe PDF spec.\r
+// This is currently available online at\r
+// http://partners.adobe.com/public/developer/pdf/index_reference.html\r
+\r
+// write out a null-terminated base-85 encoding\r
+// Convert a 32bit binary number (int, float, ...) into the ASCII base-85 encoding\r
+// if bytes==0, write out dest=['z', '\0']\r
+// else write out dest=[c4, c3, c2, c1, c0, '\0']\r
+inline void bin2ascii(int bytes, char *dest)\r
+{\r
+       if(bytes==0)\r
+       {\r
+               dest[0]='z';\r
+               dest[1]='\0';\r
+               return;\r
+       }\r
+       \r
+       dest[0]=(char)(bytes%85+33); bytes/=85;\r
+       dest[1]=(char)(bytes%85+33); bytes/=85;\r
+       dest[2]=(char)(bytes%85+33); bytes/=85;\r
+       dest[3]=(char)(bytes%85+33); bytes/=85;\r
+       dest[4]=(char)(bytes+33); // nothing higher than 85 by now\r
+       dest[5]='\0';\r
+       return;\r
+}\r
+\r
+// if i==0, write out dest=['i', 'z', '\0']\r
+// else write out dest=['i', c4, c3, c2, c1, c0, '\0']\r
+inline void int2ascii(int i, char *dest)\r
+{\r
+       dest[0]='i';\r
+       bin2ascii(i, dest+1);\r
+       return;\r
+}\r
+\r
+// if f==0, write out dest=['f', 'z', '\0']\r
+// else write out dest=['f', c4, c3, c2, c1, c0, '\0']\r
+inline void float2ascii(float f, char *dest)\r
+{\r
+       float *fp=&f;\r
+       int *ip=(int *)fp;\r
+       \r
+       dest[0]='f';\r
+       bin2ascii(*ip, dest+1);\r
+       return;\r
+}\r
+\r
+// Convert an ASCII-85 encoded number back into its 32-bit representation\r
+// return the number of characters which were read, ignoring any \0 termination\r
+int ascii2bin(char *src, int *dest)\r
+{\r
+       int val=0;\r
+       \r
+       if(*src=='z')\r
+       {\r
+               *dest=0;\r
+               return 1;\r
+       }\r
+       \r
+       *dest=( (( ((src[0]-33)*85)+(src[1]-33) )*85 +(src[2]-33))*85 +(src[3]-33) )*85 + (src[4]-33);\r
+       return 5;\r
+}
\ No newline at end of file
diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..14b3901
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,14 @@
+// Talk to the robot over the serial link\r
+\r
+#include <QApplication>\r
+#include "MainWindow.h"\r
+#include "SerialPort.h"\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+       QApplication app(argc, argv);\r
+       SerialPort *sp=new SerialPort();\r
+       MainWindow *win=new MainWindow(sp);\r
+       win->show();\r
+       return app.exec();\r
+}\r
diff --git a/qtcomm.pro b/qtcomm.pro
new file mode 100644 (file)
index 0000000..2880fbc
--- /dev/null
@@ -0,0 +1,12 @@
+TEMPLATE = app\r
+TARGET = qtcomm\r
+DEPENDPATH += .;d:\qextserialport\r
+INCLUDEPATH += .;d:\qextserialport\r
+CONFIG -= debug\r
+#CONFIG += release\r
+\r
+# Input\r
+FORMS = MainWindow.ui SerialPort.ui\r
+HEADERS += MainWindow.h SerialPort.h ascii85.h\r
+SOURCES += main.cpp MainWindow.cpp SerialPort.cpp\r
+LIBS += -LD:\qextserialport\build -lqextserialport
\ No newline at end of file