├── Screenshot.png ├── Qt_GUI ├── hdlc_command_example.pro ├── defined_commands.h ├── main.cpp ├── mainwindow.h ├── hdlc_qt.h ├── hdlc_qt.cpp ├── mainwindow.ui └── mainwindow.cpp ├── .gitignore ├── Arduino_sketch ├── defined_commands.h ├── Arduhdlc.h ├── Arduino_sketch.ino ├── Arduhdlc.cpp └── LICENSE.txt ├── README.md └── LICENSE /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarkko-hautakorpi/arduhdlc-qt-gui-example/HEAD/Screenshot.png -------------------------------------------------------------------------------- /Qt_GUI/hdlc_command_example.pro: -------------------------------------------------------------------------------- 1 | 2 | QT += core gui serialport 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | TARGET = qt_ardu_hdlc_example 6 | TEMPLATE = app 7 | 8 | 9 | SOURCES += main.cpp\ 10 | mainwindow.cpp \ 11 | hdlc_qt.cpp 12 | 13 | HEADERS += mainwindow.h \ 14 | hdlc_qt.h \ 15 | defined_commands.h 16 | 17 | FORMS += mainwindow.ui 18 | 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | qrc_*.cpp 24 | ui_*.h 25 | Makefile* 26 | *-build-* 27 | 28 | # QtCreator 29 | 30 | *.autosave 31 | 32 | #QtCtreator Qml 33 | *.qmlproject.user 34 | *.qmlproject.user.* 35 | -------------------------------------------------------------------------------- /Qt_GUI/defined_commands.h: -------------------------------------------------------------------------------- 1 | #ifndef defined_commands_h 2 | #define defined_commands_h 3 | 4 | enum serial_commands { 5 | COMMAND_ERROR = 0, // 0 6 | COMMAND_SET_SERVO_POSITION, 7 | COMMAND_TOGGLE_LED, 8 | COMMAND_READ_SIGNATURE, 9 | COMMAND_ECHO_DATA, 10 | }; 11 | 12 | enum serial_responses { 13 | RESPONSE_ERROR = 0, // 0 14 | RESPONSE_VERSION, 15 | RESPONSE_BUTTON_PRESS, 16 | RESPONSE_READ_SIGNATURE, 17 | RESPONSE_ECHO_DATA, 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Arduino_sketch/defined_commands.h: -------------------------------------------------------------------------------- 1 | #ifndef defined_commands_h 2 | #define defined_commands_h 3 | 4 | enum serial_commands { 5 | COMMAND_ERROR = 0, // 0 6 | COMMAND_SET_SERVO_POSITION, 7 | COMMAND_TOGGLE_LED, 8 | COMMAND_READ_SIGNATURE, 9 | COMMAND_ECHO_DATA, 10 | }; 11 | 12 | enum serial_responses { 13 | RESPONSE_ERROR = 0, // 0 14 | RESPONSE_VERSION, 15 | RESPONSE_BUTTON_PRESS, 16 | RESPONSE_READ_SIGNATURE, 17 | RESPONSE_ECHO_DATA, 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Qt_GUI/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | a.setApplicationName("qt_arduino_hdlc_example"); 9 | a.setApplicationDisplayName("Qt to Arduino HDLC command router example"); 10 | a.setApplicationVersion("0.1"); 11 | a.setOrganizationName("Jarkko Hautakorpi"); 12 | a.setOrganizationDomain("https://plus.google.com/u/0/+JarkkoHautakorpi/"); 13 | w.show(); 14 | return a.exec(); 15 | } 16 | -------------------------------------------------------------------------------- /Arduino_sketch/Arduhdlc.h: -------------------------------------------------------------------------------- 1 | #ifndef arduhdlc_h 2 | #define arduhdlc_h 3 | 4 | #include "Arduino.h" 5 | #include 6 | #include 7 | #include 8 | 9 | typedef void (* sendchar_type) (uint8_t); 10 | typedef void (* frame_handler_type)(const uint8_t *framebuffer, uint16_t framelength); 11 | 12 | class Arduhdlc 13 | { 14 | public: 15 | Arduhdlc (sendchar_type, frame_handler_type, uint16_t max_frame_length); 16 | void charReceiver(uint8_t data); 17 | void frameDecode(const char *framebuffer, uint8_t frame_length); 18 | 19 | private: 20 | /* User must define a function, that sends a 8bit char over the chosen interface, usart, spi, i2c etc. */ 21 | sendchar_type sendchar_function; 22 | /* User must define a function, that will process the valid received frame */ 23 | /* This function can act like a command router/dispatcher */ 24 | frame_handler_type frame_handler; 25 | void sendchar(uint8_t data); 26 | 27 | bool escape_character; 28 | uint8_t * receive_frame_buffer; 29 | uint8_t frame_position; 30 | // 16bit CRC sum for _crc_ccitt_update 31 | uint16_t frame_checksum; 32 | uint16_t max_frame_length; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Qt_GUI/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | namespace Ui { 11 | class MainWindow; 12 | } 13 | 14 | class MainWindow : public QMainWindow 15 | { 16 | Q_OBJECT 17 | 18 | public: 19 | explicit MainWindow(QWidget *parent = 0); 20 | ~MainWindow(); 21 | 22 | public slots: 23 | void putChar(QByteArray data); 24 | void putChar(char data); 25 | void HDLC_CommandRouter(QByteArray buffer, quint16 bytes_received); 26 | void setStatusbarText(const QString& text); 27 | void setServoPosition(); 28 | void sendEcho(); 29 | void toggleLED(); 30 | 31 | signals: 32 | void sendDataFrame(QByteArray buffer, quint16 size); 33 | void dataReceived(QByteArray data); 34 | 35 | private: 36 | Ui::MainWindow *ui; 37 | void fillPortsInfo(); 38 | QSerialPort *serial; 39 | void getChipSignature(); 40 | void command_default(QByteArray buffer, quint16 bytes_received); 41 | void response_read_signature(QByteArray buffer, quint16 bytes_received); 42 | void response_echo_data(QByteArray buffer, quint16 bytes_received); 43 | void command_error(); 44 | 45 | 46 | private slots: 47 | void sendData(); 48 | void openSerialPort(); 49 | void closeSerialPort(); 50 | void writeData(const QByteArray &data); 51 | void readData(); 52 | void sendData(QByteArray data); 53 | void handleError(QSerialPort::SerialPortError error); 54 | void print_hex_value(QByteArray data); 55 | }; 56 | 57 | #endif // MAINWINDOW_H 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Screenshot](https://github.com/jarkko-hautakorpi/arduhdlc-qt-gui-example/blob/master/Screenshot.png) 2 | 3 | # Qt GUI to Arduino Arduhdlc Example Project 4 | 5 | Example project with Qt GUI Application controlling servo on Arduino UNO, over HDLC protocol. 6 | HDLC protocol is used to wrap all communication into [HDLC frames](https://en.wikipedia.org/wiki/High-Level_Data_Link_Control#Structure). On both ends, on the Qt GUI and on the Arduino, valid HDLC frames are passed to command router or dispatcher. 7 | 8 | ## Command router/dispatcher 9 | 10 | Application defined "protocol" or frame structure is, that the first byte is command byte. 11 | Dispatcher then extracts the first byte of the frame, which in this program example is used to hold the command, and calls the right command, passing the frame to it as a reference. Command function then extracts the rest of the data from the frame, if it requires it. 12 | 13 | ## Qt HDLC Singleton Class 14 | 15 | Arduhdlc library can not be conveniently used in Qt, so it is converted to a singleton class. All function pointers are replaced with Qt's signals and slots. Any actions can then easily be connected to HDLC Class by connecting/disconnecting signals. 16 | User must implement the same receive and valid frame handler functions, than on the Arduino side when using [Arduhdlc library](https://github.com/jarkko-hautakorpi/Arduhdlc). 17 | 18 | 19 | ### Links to related resources 20 | 21 | [Simple Servo Control tutorial](http://playground.arduino.cc/Learning/SingleServoExample) 22 | 23 | [HDLC Protocol in Wikipedia](https://en.wikipedia.org/wiki/High-Level_Data_Link_Control#Structure) 24 | 25 | [Open Source HDLC (OSHDLC)](https://github.com/dipman/OSHDLC) 26 | 27 | [Piconomic FW Library, with HDLC encapsulation layer](http://piconomic.co.za/fwlib/group___h_d_l_c.html) 28 | 29 | [Dust SmartMeshSDK C Library](https://github.com/dustcloud/sm_clib) 30 | 31 | [C++ Qt 03 - Intro to GUI programming](https://youtu.be/GxlB34Cn0zw) 32 | 33 | [Qt & Arduino - Making an RGB LED Controller - Intro](https://youtu.be/s3QuNvYce1o) 34 | -------------------------------------------------------------------------------- /Qt_GUI/hdlc_qt.h: -------------------------------------------------------------------------------- 1 | #ifndef HDLC_qt_H 2 | #define HDLC_qt_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /* HDLC Asynchronous framing */ 11 | /* The frame boundary octet is 01111110, (7E in hexadecimal notation) */ 12 | #define FRAME_BOUNDARY_OCTET 0x7E 13 | 14 | /* A "control escape octet", has the bit sequence '01111101', (7D hexadecimal) */ 15 | #define CONTROL_ESCAPE_OCTET 0x7D 16 | 17 | /* If either of these two octets appears in the transmitted data, an escape octet is sent, */ 18 | /* followed by the original data octet with bit 5 inverted */ 19 | #define INVERT_OCTET 0x20 20 | 21 | /* 16bit low and high bytes copier */ 22 | #define low(x) ((x) & 0xFF) 23 | #define high(x) (((x)>>8) & 0xFF) 24 | 25 | 26 | class HDLC_qt : public QObject 27 | { 28 | Q_OBJECT 29 | 30 | public: 31 | static HDLC_qt* instance() 32 | { 33 | static QMutex mutex; 34 | if (!m_Instance) 35 | { 36 | mutex.lock(); 37 | if (!m_Instance) 38 | m_Instance = new HDLC_qt; 39 | mutex.unlock(); 40 | } 41 | return m_Instance; 42 | } 43 | 44 | static void drop() 45 | { 46 | static QMutex mutex; 47 | mutex.lock(); 48 | delete m_Instance; 49 | m_Instance = 0; 50 | mutex.unlock(); 51 | } 52 | 53 | signals: 54 | /* Signals can never have return types (i.e. use void) */ 55 | void hdlcTransmitByte(QByteArray data); 56 | void hdlcTransmitByte(char data); 57 | void hdlcValidFrameReceived(QByteArray receive_frame_buffer, quint16 frame_size); 58 | 59 | public slots: 60 | void charReceiver(QByteArray dataArray); 61 | void frameDecode(QByteArray buffer, quint16 bytes_to_send); 62 | 63 | private: 64 | HDLC_qt(){} 65 | HDLC_qt(const HDLC_qt &); // hide copy constructor 66 | HDLC_qt& operator=(const HDLC_qt &);// hide assign op 67 | // we leave just the declarations, so the compiler will warn us 68 | // if we try to use those two functions by accident 69 | 70 | static HDLC_qt* m_Instance; 71 | QByteArray receive_frame_buffer; 72 | quint16 frame_position; 73 | quint16 frame_checksum; 74 | bool escape_character; 75 | bool hdlcFrameCRC_check(int frame_index); 76 | }; 77 | 78 | #endif // HDLC_qt_H 79 | -------------------------------------------------------------------------------- /Arduino_sketch/Arduino_sketch.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Arduhdlc.h" 4 | #include "defined_commands.h" 5 | 6 | /* How long (bytes) is the longest HDLC frame? */ 7 | #define MAX_HDLC_FRAME_LENGTH 32 8 | #define SERVO_PIN 14 9 | #define LED1_PIN 15 10 | #define VERSION_NUMBER 0.1 11 | 12 | 13 | /* Variable for LED toggle */ 14 | boolean toggle = false; 15 | 16 | /* Function to send out byte/char */ 17 | void send_character(uint8_t data); 18 | /* Function to handle a valid HDLC frame */ 19 | void hdlc_command_router(const uint8_t *framebuffer, uint16_t framelength); 20 | 21 | /* Command execute functions for each command */ 22 | void command_set_servo_position(const uint8_t *framebuffer, uint16_t framelength); 23 | void command_toggle_led(const uint8_t *framebuffer, uint16_t framelength); 24 | void command_read_signature(const uint8_t *framebuffer, uint16_t framelength); 25 | void command_echo_data(const uint8_t *framebuffer, uint16_t framelength); 26 | 27 | /* Initialize Servo library */ 28 | Servo servo1; 29 | 30 | /* Initialize Arduhdlc library with three parameters. 31 | 1. Character send function, to send out HDLC frame one byte at a time. 32 | 2. HDLC frame handler function for received frame. 33 | 3. Length of the longest frame used, to allocate buffer in memory */ 34 | Arduhdlc hdlc(&send_character, &hdlc_command_router, MAX_HDLC_FRAME_LENGTH); 35 | 36 | /* Function to send out one 8bit character */ 37 | void send_character(uint8_t data) { 38 | Serial.print((char)data); 39 | } 40 | 41 | /* 42 | * Command router/dispatcher 43 | * Calls the right command, passing data to it. 44 | * @TODO Make it better 45 | * https://doanduyhai.wordpress.com/2012/08/04/design-pattern-the-asynchronous-dispatcher/ 46 | */ 47 | void hdlc_command_router(const uint8_t *framebuffer, uint16_t framelength) { 48 | enum serial_commands command = static_cast(framebuffer[0]); 49 | switch(command) 50 | { 51 | case COMMAND_ERROR: command_error(); break; 52 | case COMMAND_SET_SERVO_POSITION: command_set_servo_position(framebuffer, framelength); break; 53 | case COMMAND_TOGGLE_LED: command_toggle_led(framebuffer, framelength); break; 54 | case COMMAND_READ_SIGNATURE: command_read_signature(framebuffer, framelength); break; 55 | case COMMAND_ECHO_DATA: command_echo_data(framebuffer, framelength); break; 56 | default: 57 | command_default(); 58 | break; 59 | } 60 | } 61 | 62 | void command_set_servo_position(const uint8_t *framebuffer, uint16_t framelength) { 63 | static int servo_position = 0; 64 | char num[5]; 65 | servo_position = framebuffer[1]; 66 | servo1.write(servo_position); 67 | } 68 | 69 | void command_toggle_led(const uint8_t *framebuffer, uint16_t framelengthd) { 70 | toggle = !toggle; 71 | digitalWrite(LED1_PIN, toggle); 72 | } 73 | 74 | void command_read_signature(const uint8_t *framebuffer, uint16_t framelengthd) { 75 | char signature[4]; 76 | signature[0] = RESPONSE_READ_SIGNATURE; 77 | signature[1] = boot_signature_byte_get(0); //Device Signature Byte 1 78 | signature[2] = boot_signature_byte_get(2); //Device Signature Byte 2 79 | signature[3] = boot_signature_byte_get(4); //Device Signature Byte 3 80 | signature[4] = boot_signature_byte_get(1); //RC Oscillator Calibration Byte 81 | hdlc.frameDecode(signature, 5); 82 | } 83 | 84 | // ECHO the data back, for testing CRC sum 85 | void command_echo_data(const uint8_t *framebuffer, uint16_t framelength) { 86 | hdlc.frameDecode((const char*)framebuffer, framelength); 87 | } 88 | 89 | void command_error(void) { 90 | char data[2] = {(uint8_t)RESPONSE_ERROR, 1}; 91 | //data[0] = RESPONSE_ERROR; 92 | hdlc.frameDecode(data, 2); 93 | } 94 | 95 | void command_default(void) { 96 | ; 97 | } 98 | 99 | void setup() { 100 | pinMode(1,OUTPUT); // Serial port TX to output 101 | servo1.attach(SERVO_PIN); // Servo on analog pin A0 102 | servo1.write(512); 103 | pinMode(LED1_PIN, OUTPUT); 104 | // initialize serial port to 9600 baud 105 | Serial.begin(9600); 106 | } 107 | 108 | void loop() { 109 | 110 | } 111 | 112 | /* 113 | SerialEvent occurs whenever a new data comes in the 114 | hardware serial RX. This routine is run between each 115 | time loop() runs, so using delay inside loop can delay 116 | response. Multiple bytes of data may be available. 117 | */ 118 | void serialEvent() { 119 | while (Serial.available()) { 120 | // get the new byte: 121 | char inChar = (char)Serial.read(); 122 | hdlc.charReceiver(inChar); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /Qt_GUI/hdlc_qt.cpp: -------------------------------------------------------------------------------- 1 | #include "hdlc_qt.h" 2 | 3 | HDLC_qt* HDLC_qt::m_Instance = 0; 4 | 5 | /** 6 | * All data is passed to HDLC receiver. 7 | * Here we check the packet consistency, and pass on the data only. 8 | * @brief HDLC_qt::charReceiver 9 | * @param dataArray Received chunck of data of some length. 10 | * @return emit hdlcValidFrameReceived(data, length) When packet arrived. 11 | */ 12 | void HDLC_qt::charReceiver(QByteArray dataArray) 13 | { 14 | quint8 data = 0; 15 | for(QByteArray::iterator it = dataArray.begin(); it != dataArray.end(); it++) { 16 | data = (*it); 17 | /* Start flag or end flag */ 18 | if(data == FRAME_BOUNDARY_OCTET) 19 | { 20 | if(escape_character == true) 21 | { 22 | escape_character = false; 23 | } 24 | /* Do CRC check if frame is valid */ 25 | else if( (frame_position >= 2) 26 | &&(this->hdlcFrameCRC_check(frame_position)) ) 27 | { 28 | /* Call user defined function to handle HDLC frame */ 29 | emit hdlcValidFrameReceived(receive_frame_buffer, (quint16)(frame_position-2)); 30 | } 31 | /* Reset all for next frame */ 32 | frame_position = 0; 33 | frame_checksum = 0; 34 | receive_frame_buffer.clear(); 35 | continue; 36 | } 37 | 38 | if(escape_character) 39 | { 40 | escape_character = false; 41 | data ^= INVERT_OCTET; 42 | } 43 | else if(data == CONTROL_ESCAPE_OCTET) 44 | { 45 | escape_character = true; 46 | continue; 47 | } 48 | receive_frame_buffer[frame_position] = data; 49 | 50 | /* In Qt we don't calculate CRC here iteratively. 51 | * In Arduino, we needed to save RAM, but here small 52 | * buffer is no problem, we have more RAM. 53 | */ 54 | 55 | frame_position++; 56 | 57 | /* If we don't ever receive valid frame, 58 | * buffer will keep growing bigger and bigger. 59 | * Hard coded max size limit and then reset 60 | */ 61 | if(frame_position >= 2048) 62 | { 63 | receive_frame_buffer.clear(); 64 | frame_position = 0; 65 | frame_checksum = 0; 66 | } 67 | } 68 | } 69 | 70 | /** 71 | * Wrap the data in HDLC frame 72 | * @brief HDLC_qt::frameDecode 73 | * @param buffer 74 | * @param bytes_to_send 75 | */ 76 | void HDLC_qt::frameDecode(QByteArray buffer, quint16 bytes_to_send) 77 | { 78 | char data; 79 | QByteArray packet; 80 | /* The frame check sequence (FCS) is a 16-bit CRC-CCITT */ 81 | quint16 fcs = 0; 82 | // Update checksum 83 | fcs = qChecksum((const char*)buffer.constData(), bytes_to_send); 84 | /* Start flag */ 85 | packet.append((char)FRAME_BOUNDARY_OCTET); 86 | int i = 0; 87 | while (i < bytes_to_send) 88 | { 89 | data = buffer[i]; 90 | if( (data == (char)CONTROL_ESCAPE_OCTET) || (data == (char)FRAME_BOUNDARY_OCTET) ) 91 | { 92 | packet.append((char)CONTROL_ESCAPE_OCTET); 93 | data ^= (char)INVERT_OCTET; 94 | } 95 | packet.append((char)data); 96 | i++; 97 | } 98 | 99 | /* Invert bits in checksum 100 | * For avrlibc crc_ccitt_update() compatibility 101 | */ 102 | fcs ^= 0xFFFF; 103 | 104 | /* Low byte of inverted FCS */ 105 | data = low(fcs); 106 | if((data == (char)CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) 107 | { 108 | packet.append((char)CONTROL_ESCAPE_OCTET); 109 | data ^= (char)INVERT_OCTET; 110 | } 111 | packet.append((char)data); 112 | 113 | /* High byte of inverted FCS */ 114 | data = high(fcs); 115 | if((data == (char)CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) 116 | { 117 | packet.append((char)CONTROL_ESCAPE_OCTET); 118 | data ^= (char)INVERT_OCTET; 119 | } 120 | packet.append((char)data); 121 | 122 | /* End flag */ 123 | packet.append((char)FRAME_BOUNDARY_OCTET); 124 | emit hdlcTransmitByte(packet); 125 | } 126 | 127 | 128 | bool HDLC_qt::hdlcFrameCRC_check(int frame_index) 129 | { 130 | /* frame = ...[CRC-LO] [CRC-HI] */ 131 | quint16 crc_received = 0; 132 | crc_received = receive_frame_buffer[frame_index-1]; // msb 133 | crc_received = crc_received << 8; 134 | crc_received |= receive_frame_buffer[frame_index-2]; // lsb 135 | quint16 crc_calculated = qChecksum((const char*)receive_frame_buffer.constData(), frame_index-2); 136 | crc_calculated = crc_calculated^0xFFFF; 137 | if(crc_received == crc_calculated) { 138 | return true; 139 | } else { 140 | return false; 141 | } 142 | } 143 | 144 | -------------------------------------------------------------------------------- /Arduino_sketch/Arduhdlc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Arduhdlc is Arduino HDLC library, which can be used over any interface. 3 | Copyright (C) 2015 Jarkko Hautakorpi 4 | 5 | This program is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU General Public License 7 | as published by the Free Software Foundation; either version 2 8 | of the License, or (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 | */ 19 | 20 | #include "Arduino.h" 21 | #include "Arduhdlc.h" 22 | 23 | /* HDLC Asynchronous framing */ 24 | /* The frame boundary octet is 01111110, (7E in hexadecimal notation) */ 25 | #define FRAME_BOUNDARY_OCTET 0x7E 26 | 27 | /* A "control escape octet", has the bit sequence '01111101', (7D hexadecimal) */ 28 | #define CONTROL_ESCAPE_OCTET 0x7D 29 | 30 | /* If either of these two octets appears in the transmitted data, an escape octet is sent, */ 31 | /* followed by the original data octet with bit 5 inverted */ 32 | #define INVERT_OCTET 0x20 33 | 34 | /* The frame check sequence (FCS) is a 16-bit CRC-CCITT */ 35 | /* AVR Libc CRC function is _crc_ccitt_update() */ 36 | /* Corresponding CRC function in Qt (www.qt.io) is qChecksum() */ 37 | #define CRC16_CCITT_INIT_VAL 0xFFFF 38 | 39 | /* 16bit low and high bytes copier */ 40 | #define low(x) ((x) & 0xFF) 41 | #define high(x) (((x)>>8) & 0xFF) 42 | 43 | Arduhdlc::Arduhdlc (sendchar_type put_char, frame_handler_type hdlc_command_router, uint16_t max_frame_length) : sendchar_function(put_char), frame_handler(hdlc_command_router) 44 | { 45 | this->frame_position = 0; 46 | this->max_frame_length = max_frame_length; 47 | this->receive_frame_buffer = (uint8_t *)malloc(max_frame_length+1); // char *ab = (char*)malloc(12); 48 | this->frame_checksum = CRC16_CCITT_INIT_VAL; 49 | this->escape_character = false; 50 | } 51 | 52 | /* Function to send a byte throug USART, I2C, SPI etc.*/ 53 | void Arduhdlc::sendchar(uint8_t data) 54 | { 55 | (*this->sendchar_function)(data); 56 | } 57 | 58 | /* Function to find valid HDLC frame from incoming data */ 59 | void Arduhdlc::charReceiver(uint8_t data) 60 | { 61 | /* FRAME FLAG */ 62 | if(data == FRAME_BOUNDARY_OCTET) 63 | { 64 | if(this->escape_character == true) 65 | { 66 | this->escape_character = false; 67 | } 68 | /* If a valid frame is detected */ 69 | else if( (this->frame_position >= 2) && 70 | ( this->frame_checksum == ((this->receive_frame_buffer[this->frame_position-1] << 8 ) | (this->receive_frame_buffer[this->frame_position-2] & 0xff)) ) ) // (msb << 8 ) | (lsb & 0xff) 71 | { 72 | /* Call the user defined function and pass frame to it */ 73 | (*frame_handler)(receive_frame_buffer,(uint8_t)(this->frame_position-2)); 74 | } 75 | this->frame_position = 0; 76 | this->frame_checksum = CRC16_CCITT_INIT_VAL; 77 | return; 78 | } 79 | 80 | if(this->escape_character) 81 | { 82 | this->escape_character = false; 83 | data ^= INVERT_OCTET; 84 | } 85 | else if(data == CONTROL_ESCAPE_OCTET) 86 | { 87 | this->escape_character = true; 88 | return; 89 | } 90 | 91 | receive_frame_buffer[this->frame_position] = data; 92 | 93 | if(this->frame_position-2 >= 0) { 94 | this->frame_checksum = _crc_ccitt_update(this->frame_checksum, receive_frame_buffer[this->frame_position-2]); 95 | } 96 | 97 | this->frame_position++; 98 | 99 | if(this->frame_position == this->max_frame_length) 100 | { 101 | this->frame_position = 0; 102 | this->frame_checksum = CRC16_CCITT_INIT_VAL; 103 | } 104 | } 105 | 106 | /* Wrap given data in HDLC frame and send it out byte at a time*/ 107 | void Arduhdlc::frameDecode(const char *framebuffer, uint8_t frame_length) 108 | { 109 | uint8_t data; 110 | uint16_t fcs = CRC16_CCITT_INIT_VAL; 111 | 112 | this->sendchar((uint8_t)FRAME_BOUNDARY_OCTET); 113 | 114 | while(frame_length) 115 | { 116 | data = *framebuffer++; 117 | fcs = _crc_ccitt_update(fcs, data); 118 | if((data == CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) 119 | { 120 | this->sendchar((uint8_t)CONTROL_ESCAPE_OCTET); 121 | data ^= INVERT_OCTET; 122 | } 123 | this->sendchar((uint8_t)data); 124 | frame_length--; 125 | } 126 | data = low(fcs); 127 | if((data == CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) 128 | { 129 | this->sendchar((uint8_t)CONTROL_ESCAPE_OCTET); 130 | data ^= (uint8_t)INVERT_OCTET; 131 | } 132 | this->sendchar((uint8_t)data); 133 | data = high(fcs); 134 | if((data == CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) 135 | { 136 | this->sendchar(CONTROL_ESCAPE_OCTET); 137 | data ^= INVERT_OCTET; 138 | } 139 | this->sendchar(data); 140 | this->sendchar(FRAME_BOUNDARY_OCTET); 141 | } 142 | 143 | 144 | -------------------------------------------------------------------------------- /Arduino_sketch/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Arduhdlc is Arduino HDLC library, which can be used over any interface. 2 | Copyright (C) 2015 Jarkko Hautakorpi 3 | 4 | This program is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU General Public License 6 | as published by the Free Software Foundation; either version 2 7 | of the License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 | 18 | 19 | 20 | 21 | Licese applies to work derived from Dust SmartMeshSDK C Library: 22 | ============================================================================= 23 | Copyright (c) 2012, Dust Networks 24 | All rights reserved. 25 | 26 | Redistribution and use in source and binary forms, with or without 27 | modification, are permitted provided that the following conditions are met: 28 | * Redistributions of source code must retain the above copyright 29 | notice, this list of conditions and the following disclaimer. 30 | * Redistributions in binary form must reproduce the above copyright 31 | notice, this list of conditions and the following disclaimer in the 32 | documentation and/or other materials provided with the distribution. 33 | * Neither the name of Dust Networks nor the 34 | names of its contributors may be used to endorse or promote products 35 | derived from this software without specific prior written permission. 36 | 37 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 38 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 39 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 | DISCLAIMED. IN NO EVENT SHALL DUST NETWORKS BE LIABLE FOR ANY 41 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 42 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 45 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 46 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 47 | 48 | 49 | 50 | 51 | Licese applies to work derived from Piconomic FW Library: 52 | ============================================================================= 53 | Copyright (c) 2006 Pieter Conradie [www.piconomic.co.za] 54 | All rights reserved. 55 | 56 | Redistribution and use in source and binary forms, with or without 57 | modification, are permitted provided that the following conditions are met: 58 | 59 | * Redistributions of source code must retain the above copyright 60 | notice, this list of conditions and the following disclaimer. 61 | 62 | * Redistributions in binary form must reproduce the above copyright 63 | notice, this list of conditions and the following disclaimer in 64 | the documentation and/or other materials provided with the 65 | distribution. 66 | 67 | * Neither the name of the copyright holders nor the names of 68 | contributors may be used to endorse or promote products derived 69 | from this software without specific prior written permission. 70 | 71 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 72 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 73 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 74 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 75 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 76 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 77 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 78 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 79 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 80 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 81 | POSSIBILITY OF SUCH DAMAGE. 82 | 83 | Title: HDLC encapsulation layer 84 | Author(s): Pieter Conradie 85 | Creation Date: 2007-03-31 86 | Revision Info: $Id: hdlc.h 117 2010-06-24 20:21:28Z pieterconradie $ 87 | 88 | 89 | 90 | 91 | Licese applies to work derived from HDLC byte stuffing package: 92 | ============================================================================= 93 | /***************************************************** 94 | * HDLC byte stuffing package * 95 | * * 96 | * Created on: Dec 2013 * 97 | * Author: jdn * 98 | * * 99 | ****************************************************** 100 | * * 101 | * * 102 | * (C) 2012,2013 * 103 | * * 104 | * Jens Dalsgaard Nielsen * 105 | * http://www.control.aau.dk/~jdn * 106 | * Studentspace/Satlab * 107 | * Section of Automation & Control * 108 | * Aalborg University, * 109 | * Denmark * 110 | * * 111 | * "THE BEER-WARE LICENSE" (frit efter PHK) * 112 | * wrote this file. As long as you * 113 | * retain this notice you can do whatever you want * 114 | * with this stuff. If we meet some day, and you think* 115 | * this stuff is worth it ... * 116 | * you can buy me a beer in return :-) * 117 | * or if you are real happy then ... * 118 | * single malt will be well received :-) * 119 | * * 120 | * Use it at your own risk - no warranty * 121 | *****************************************************/ 122 | -------------------------------------------------------------------------------- /Qt_GUI/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 775 10 | 728 11 | 12 | 13 | 14 | Qt to Arduino HDLC example 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Send echo 24 | 25 | 26 | 27 | 28 | 29 | 30 | Toggle LED 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 75 43 | true 44 | 45 | 46 | 47 | Device Signature: 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 75 56 | true 57 | 58 | 59 | 60 | 61 | 62 | 63 | Qt::PlainText 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 75 76 | true 77 | 78 | 79 | 80 | Oscillator calibration byte: 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 75 89 | true 90 | 91 | 92 | 93 | 94 | 95 | 96 | Qt::PlainText 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | Qt::Horizontal 108 | 109 | 110 | 111 | 40 112 | 20 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | Serial port: 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | Connect 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | Qt::ScrollBarAlwaysOn 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 71 150 | 0 151 | 152 | 153 | 154 | 155 | 50 156 | 16777215 157 | 158 | 159 | 160 | Qt::ScrollBarAlwaysOn 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 280 176 | 280 177 | 178 | 179 | 180 | 181 | 280 182 | 280 183 | 184 | 185 | 186 | 1 187 | 188 | 189 | 180 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | Qt::ScrollBarAlwaysOn 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | Send 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 1 227 | 228 | 229 | 180 230 | 231 | 232 | 24 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 0 242 | 0 243 | 775 244 | 21 245 | 246 | 247 | 248 | 249 | 250 | TopToolBarArea 251 | 252 | 253 | false 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | pushButton_Send 263 | clicked() 264 | MainWindow 265 | sendData() 266 | 267 | 268 | 647 269 | 450 270 | 271 | 272 | 340 273 | 271 274 | 275 | 276 | 277 | 278 | dial 279 | valueChanged(int) 280 | progressBar 281 | setValue(int) 282 | 283 | 284 | 133 285 | 350 286 | 287 | 288 | 165 289 | 474 290 | 291 | 292 | 293 | 294 | 295 | sendData() 296 | 297 | 298 | -------------------------------------------------------------------------------- /Qt_GUI/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include "hdlc_qt.h" 4 | #include "defined_commands.h" 5 | #include 6 | #include 7 | 8 | 9 | MainWindow::MainWindow(QWidget *parent) : 10 | QMainWindow(parent), 11 | ui(new Ui::MainWindow) 12 | { 13 | ui->setupUi(this); 14 | ui->statusBar->showMessage("Qt to Arduino HDLC command router example", 3000); 15 | serial = new QSerialPort(this); 16 | this->fillPortsInfo(); 17 | HDLC_qt* hdlc = HDLC_qt::instance(); 18 | 19 | QObject::connect(hdlc, SIGNAL(hdlcTransmitByte(QByteArray)), 20 | this, SLOT(putChar(QByteArray))); 21 | 22 | QObject::connect(hdlc, SIGNAL(hdlcTransmitByte(char)), 23 | this, SLOT(putChar(char))); 24 | 25 | QObject::connect(hdlc, SIGNAL(hdlcValidFrameReceived(QByteArray, quint16)), 26 | this, SLOT(HDLC_CommandRouter(QByteArray, quint16))); 27 | 28 | // All data coming from serial port (should go straight to HDLC validation) 29 | QObject::connect(this, SIGNAL(dataReceived(QByteArray)), 30 | hdlc, SLOT(charReceiver(QByteArray))); 31 | 32 | // Allso echo chars HEX value 33 | QObject::connect(this, SIGNAL(dataReceived(QByteArray)), 34 | this, SLOT(print_hex_value(QByteArray))); 35 | 36 | QObject::connect(this, SIGNAL(sendDataFrame(QByteArray, quint16)), 37 | hdlc, SLOT(frameDecode(QByteArray, quint16))); 38 | 39 | QObject::connect(ui->pushButton_connect, SIGNAL(clicked()), 40 | this, SLOT(openSerialPort())); 41 | 42 | QObject::connect(serial, SIGNAL(readyRead()), 43 | this, SLOT(readData())); 44 | 45 | QObject::connect(ui->dial, SIGNAL(valueChanged(int)), 46 | this, SLOT(setServoPosition()) ); 47 | 48 | QObject::connect(ui->pushButton_echo, SIGNAL(clicked()), 49 | this, SLOT(sendEcho()) ); 50 | 51 | QObject::connect(ui->pushButton_LED, SIGNAL(clicked()), 52 | this, SLOT(toggleLED()) ); 53 | } 54 | 55 | 56 | MainWindow::~MainWindow() 57 | { 58 | delete ui; 59 | } 60 | 61 | void MainWindow::putChar(QByteArray data) 62 | { 63 | ui->plainTextEdit_debug->appendPlainText(data.data()); 64 | this->writeData(data); 65 | } 66 | 67 | void MainWindow::putChar(char data) 68 | { 69 | QByteArray qdata(1, data); 70 | ui->plainTextEdit_debug->appendPlainText(qdata.data()); 71 | this->writeData(qdata); 72 | } 73 | 74 | void MainWindow::HDLC_CommandRouter(QByteArray buffer, quint16 bytes_received) 75 | { 76 | enum serial_responses command = static_cast(buffer.at(0)); 77 | switch(command) 78 | { 79 | case RESPONSE_ERROR: this->command_error(); break; 80 | case RESPONSE_VERSION: this->command_default(buffer, bytes_received); break; 81 | case RESPONSE_BUTTON_PRESS: this->command_default(buffer, bytes_received); break; 82 | case RESPONSE_READ_SIGNATURE: this->response_read_signature(buffer, bytes_received); break; 83 | case RESPONSE_ECHO_DATA: this->command_default(buffer, bytes_received); break; 84 | default: 85 | this->command_error(); 86 | break; 87 | } 88 | } 89 | 90 | void MainWindow::command_default(QByteArray buffer, quint16 bytes_received) { 91 | ui->statusBar->showMessage(tr("Received a valid HDLC packet/command"), 2000); 92 | } 93 | 94 | void MainWindow::command_error() { 95 | ui->statusBar->showMessage(tr("Error in HDLC packet/command"), 2000); 96 | } 97 | 98 | void MainWindow::response_read_signature(QByteArray buffer, quint16 bytes_received) { 99 | /* Device signature bytes */ 100 | QString device_signature = "0x"; 101 | QByteArray signature_bytes; 102 | signature_bytes 103 | .append(buffer.at(1)) 104 | .append(buffer.at(2)) 105 | .append(buffer.at(3)); 106 | device_signature.append(signature_bytes.toHex().toUpper()); 107 | ui->label_deviceSignature->setText(device_signature); 108 | 109 | /* Oscillator calibration byte */ 110 | QString oscillator_calibr = "0x"; 111 | QByteArray oscillator_byte; 112 | oscillator_byte.append(buffer.at(4)); 113 | oscillator_calibr.append(oscillator_byte.toHex().toUpper()); 114 | ui->label_oscillatorCalibrationByte->setText(oscillator_calibr); 115 | } 116 | 117 | void MainWindow::response_echo_data(QByteArray buffer, quint16 bytes_received) { 118 | QString bufferText(buffer); 119 | ui->statusBar->showMessage(bufferText, 2000); 120 | } 121 | 122 | 123 | void MainWindow::sendData() { 124 | QByteArray data; 125 | data.append((quint8) ui->lineEdit_Command->text().toInt()); 126 | data.append(ui->lineEdit_Data->text());; 127 | emit sendDataFrame(data, (quint16)data.length()); 128 | } 129 | 130 | void MainWindow::fillPortsInfo() 131 | { 132 | ui->serialPortDropdown->clear(); 133 | static const QString blankString = QObject::tr("N/A"); 134 | QString description; 135 | QString manufacturer; 136 | QString serialNumber; 137 | foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { 138 | QStringList list; 139 | description = info.description(); 140 | manufacturer = info.manufacturer(); 141 | serialNumber = info.serialNumber(); 142 | list << info.portName() 143 | << (!description.isEmpty() ? description : blankString) 144 | << (!manufacturer.isEmpty() ? manufacturer : blankString) 145 | << (!serialNumber.isEmpty() ? serialNumber : blankString) 146 | << info.systemLocation() 147 | << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString) 148 | << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString); 149 | 150 | ui->serialPortDropdown->addItem(list.first(), list); 151 | } 152 | } 153 | 154 | void MainWindow::openSerialPort() 155 | { 156 | serial->setPortName(ui->serialPortDropdown->currentText()); 157 | serial->setBaudRate(QSerialPort::Baud9600); 158 | serial->setDataBits(QSerialPort::Data8); 159 | serial->setParity(QSerialPort::NoParity); 160 | serial->setStopBits(QSerialPort::OneStop); 161 | serial->setFlowControl(QSerialPort::NoFlowControl); 162 | if (serial->open(QIODevice::ReadWrite)) { 163 | ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") 164 | .arg(ui->serialPortDropdown->currentText()) 165 | .arg(QSerialPort::Baud9600) 166 | .arg(QSerialPort::Data8) 167 | .arg(QSerialPort::NoParity) 168 | .arg(QSerialPort::OneStop) 169 | .arg(QSerialPort::NoFlowControl)); 170 | ui->pushButton_connect->setText("Disconnect"); 171 | disconnect( ui->pushButton_connect, SIGNAL(clicked()),0, 0); 172 | QObject::connect(ui->pushButton_connect, SIGNAL(clicked()), 173 | this, SLOT(closeSerialPort())); 174 | 175 | } else { 176 | QMessageBox::critical(this, tr("Error"), serial->errorString()); 177 | ui->statusBar->showMessage(tr("Open error")); 178 | } 179 | ui->plainTextEdit_input->appendPlainText("Getting chip signature:\n"); 180 | QByteArray data; 181 | data.append(COMMAND_READ_SIGNATURE); 182 | emit sendDataFrame(data, 1); 183 | } 184 | 185 | void MainWindow::closeSerialPort() 186 | { 187 | serial->close(); 188 | ui->statusBar->showMessage(tr("Disconnected")); 189 | disconnect( ui->pushButton_connect, SIGNAL(clicked()),0, 0); 190 | QObject::connect(ui->pushButton_connect, SIGNAL(clicked()), 191 | this, SLOT(openSerialPort())); 192 | ui->pushButton_connect->setText("Connect"); 193 | ui->label_deviceSignature->setText(" "); 194 | ui->label_oscillatorCalibrationByte->setText(" "); 195 | } 196 | 197 | void MainWindow::writeData(const QByteArray &data) 198 | { 199 | serial->write(data); 200 | } 201 | 202 | void MainWindow::readData() 203 | { 204 | QByteArray data = serial->readAll(); 205 | ui->plainTextEdit_input->appendPlainText(data); 206 | int dSize = data.size(); 207 | if (dSize > 0) { 208 | emit dataReceived(data); 209 | } 210 | } 211 | 212 | void MainWindow::handleError(QSerialPort::SerialPortError error) 213 | { 214 | if (error == QSerialPort::ResourceError) { 215 | QMessageBox::critical(this, tr("Critical Error"), serial->errorString()); 216 | closeSerialPort(); 217 | } 218 | } 219 | 220 | void MainWindow::sendData(QByteArray data) { 221 | quint16 length = data.length(); 222 | emit sendDataFrame(data, length); 223 | } 224 | 225 | void MainWindow::setStatusbarText(const QString& text) { 226 | ui->statusBar->showMessage(text); 227 | } 228 | 229 | void MainWindow::print_hex_value(QByteArray data) { 230 | QByteArray hex_console; 231 | QByteArray character; 232 | for (int i = 0; i < data.size(); ++i) { 233 | character.clear(); 234 | character.append(data.at(i)); 235 | //character = data.at(i); 236 | hex_console.append("0x"); 237 | hex_console.append(character.toHex().toUpper()); 238 | hex_console.append("\n"); 239 | } 240 | ui->plainTextEdit_input_hex->appendPlainText(QString(hex_console)); 241 | } 242 | 243 | /* Command sending functions */ 244 | 245 | void MainWindow::setServoPosition() { 246 | QByteArray data; 247 | quint16 length; 248 | quint16 position = ui->dial->value(); 249 | data.append(COMMAND_SET_SERVO_POSITION); 250 | //data.append(position >> 8); // high byte 251 | data.append(position & 0xff); // low byte 252 | length = (quint16) data.length(); 253 | emit sendDataFrame(data, length); 254 | } 255 | 256 | void MainWindow::sendEcho() { 257 | QByteArray data; 258 | data.append((quint8)COMMAND_ECHO_DATA); 259 | data.append("ABCD"); 260 | emit sendDataFrame(data, (quint16) data.length()); 261 | } 262 | 263 | void MainWindow::toggleLED() { 264 | QByteArray data; 265 | data.append((quint8)COMMAND_TOGGLE_LED); 266 | emit sendDataFrame(data, 1); 267 | } 268 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------