Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Platforms/Qt/SerialPort.cpp@3:f818ea5172ed, 2018-01-11 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Thu Jan 11 13:50:39 2018 -0600
- Revision:
- 3:f818ea5172ed
Updated to version 1.1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 3:f818ea5172ed | 1 | /******************************************************************************* |
IanBenzMaxim | 3:f818ea5172ed | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 3:f818ea5172ed | 3 | * |
IanBenzMaxim | 3:f818ea5172ed | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 3:f818ea5172ed | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 3:f818ea5172ed | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 3:f818ea5172ed | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 3:f818ea5172ed | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 3:f818ea5172ed | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 3:f818ea5172ed | 10 | * |
IanBenzMaxim | 3:f818ea5172ed | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 3:f818ea5172ed | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 3:f818ea5172ed | 13 | * |
IanBenzMaxim | 3:f818ea5172ed | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 3:f818ea5172ed | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 3:f818ea5172ed | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 3:f818ea5172ed | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 3:f818ea5172ed | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 3:f818ea5172ed | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 3:f818ea5172ed | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 3:f818ea5172ed | 21 | * |
IanBenzMaxim | 3:f818ea5172ed | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 3:f818ea5172ed | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 3:f818ea5172ed | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 3:f818ea5172ed | 25 | * |
IanBenzMaxim | 3:f818ea5172ed | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 3:f818ea5172ed | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 3:f818ea5172ed | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 3:f818ea5172ed | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 3:f818ea5172ed | 30 | * ownership rights. |
IanBenzMaxim | 3:f818ea5172ed | 31 | *******************************************************************************/ |
IanBenzMaxim | 3:f818ea5172ed | 32 | |
IanBenzMaxim | 3:f818ea5172ed | 33 | #include <QSerialPortInfo> |
IanBenzMaxim | 3:f818ea5172ed | 34 | #include <MaximInterface/Utilities/Error.hpp> |
IanBenzMaxim | 3:f818ea5172ed | 35 | #include "SerialPort.hpp" |
IanBenzMaxim | 3:f818ea5172ed | 36 | #include "Sleep.hpp" |
IanBenzMaxim | 3:f818ea5172ed | 37 | |
IanBenzMaxim | 3:f818ea5172ed | 38 | using std::string; |
IanBenzMaxim | 3:f818ea5172ed | 39 | |
IanBenzMaxim | 3:f818ea5172ed | 40 | namespace MaximInterface { |
IanBenzMaxim | 3:f818ea5172ed | 41 | namespace Qt { |
IanBenzMaxim | 3:f818ea5172ed | 42 | |
IanBenzMaxim | 3:f818ea5172ed | 43 | static_assert(QSerialPort::NoError == 0, "Remapping required for error_code."); |
IanBenzMaxim | 3:f818ea5172ed | 44 | |
IanBenzMaxim | 3:f818ea5172ed | 45 | error_code SerialPort::connect(const string & portName) { |
IanBenzMaxim | 3:f818ea5172ed | 46 | port.setPort(QSerialPortInfo(QString::fromStdString(portName))); |
IanBenzMaxim | 3:f818ea5172ed | 47 | auto result = port.open(QSerialPort::ReadWrite) |
IanBenzMaxim | 3:f818ea5172ed | 48 | ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 49 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 50 | if (result) { |
IanBenzMaxim | 3:f818ea5172ed | 51 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 52 | } |
IanBenzMaxim | 3:f818ea5172ed | 53 | result = port.setDataTerminalReady(true) ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 54 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 55 | if (result) { |
IanBenzMaxim | 3:f818ea5172ed | 56 | port.close(); |
IanBenzMaxim | 3:f818ea5172ed | 57 | } |
IanBenzMaxim | 3:f818ea5172ed | 58 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 59 | } |
IanBenzMaxim | 3:f818ea5172ed | 60 | |
IanBenzMaxim | 3:f818ea5172ed | 61 | error_code SerialPort::disconnect() { |
IanBenzMaxim | 3:f818ea5172ed | 62 | port.close(); |
IanBenzMaxim | 3:f818ea5172ed | 63 | return error_code(); |
IanBenzMaxim | 3:f818ea5172ed | 64 | } |
IanBenzMaxim | 3:f818ea5172ed | 65 | |
IanBenzMaxim | 3:f818ea5172ed | 66 | bool SerialPort::connected() const { return port.isOpen(); } |
IanBenzMaxim | 3:f818ea5172ed | 67 | |
IanBenzMaxim | 3:f818ea5172ed | 68 | string SerialPort::portName() const { return port.portName().toStdString(); } |
IanBenzMaxim | 3:f818ea5172ed | 69 | |
IanBenzMaxim | 3:f818ea5172ed | 70 | error_code SerialPort::setBaudRate(int_least32_t baudRate) { |
IanBenzMaxim | 3:f818ea5172ed | 71 | return port.setBaudRate(baudRate) ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 72 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 73 | } |
IanBenzMaxim | 3:f818ea5172ed | 74 | |
IanBenzMaxim | 3:f818ea5172ed | 75 | error_code SerialPort::sendBreak() { |
IanBenzMaxim | 3:f818ea5172ed | 76 | auto result = |
IanBenzMaxim | 3:f818ea5172ed | 77 | port.setBreakEnabled(true) ? error_code() : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 78 | if (!result) { |
IanBenzMaxim | 3:f818ea5172ed | 79 | sleep(1); |
IanBenzMaxim | 3:f818ea5172ed | 80 | result = port.setBreakEnabled(false) ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 81 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 82 | } |
IanBenzMaxim | 3:f818ea5172ed | 83 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 84 | } |
IanBenzMaxim | 3:f818ea5172ed | 85 | |
IanBenzMaxim | 3:f818ea5172ed | 86 | error_code SerialPort::clearReadBuffer() { |
IanBenzMaxim | 3:f818ea5172ed | 87 | port.readAll(); |
IanBenzMaxim | 3:f818ea5172ed | 88 | return error_code(); |
IanBenzMaxim | 3:f818ea5172ed | 89 | } |
IanBenzMaxim | 3:f818ea5172ed | 90 | |
IanBenzMaxim | 3:f818ea5172ed | 91 | error_code SerialPort::writeByte(uint_least8_t data) { |
IanBenzMaxim | 3:f818ea5172ed | 92 | auto result = |
IanBenzMaxim | 3:f818ea5172ed | 93 | port.putChar(data) ? error_code() : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 94 | if (!result) { |
IanBenzMaxim | 3:f818ea5172ed | 95 | result = port.waitForBytesWritten(1000) ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 96 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 97 | } |
IanBenzMaxim | 3:f818ea5172ed | 98 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 99 | } |
IanBenzMaxim | 3:f818ea5172ed | 100 | |
IanBenzMaxim | 3:f818ea5172ed | 101 | error_code SerialPort::readByte(uint_least8_t & data) { |
IanBenzMaxim | 3:f818ea5172ed | 102 | if (port.atEnd()) { |
IanBenzMaxim | 3:f818ea5172ed | 103 | const auto result = port.waitForReadyRead(1000) |
IanBenzMaxim | 3:f818ea5172ed | 104 | ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 105 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 106 | if (result) { |
IanBenzMaxim | 3:f818ea5172ed | 107 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 108 | } |
IanBenzMaxim | 3:f818ea5172ed | 109 | } |
IanBenzMaxim | 3:f818ea5172ed | 110 | return port.getChar(reinterpret_cast<char *>(&data)) |
IanBenzMaxim | 3:f818ea5172ed | 111 | ? error_code() |
IanBenzMaxim | 3:f818ea5172ed | 112 | : make_error_code(port.error()); |
IanBenzMaxim | 3:f818ea5172ed | 113 | } |
IanBenzMaxim | 3:f818ea5172ed | 114 | |
IanBenzMaxim | 3:f818ea5172ed | 115 | const error_category & SerialPort::errorCategory() { |
IanBenzMaxim | 3:f818ea5172ed | 116 | static class : public error_category { |
IanBenzMaxim | 3:f818ea5172ed | 117 | public: |
IanBenzMaxim | 3:f818ea5172ed | 118 | virtual const char * name() const override { return "Qt SerialPort"; } |
IanBenzMaxim | 3:f818ea5172ed | 119 | |
IanBenzMaxim | 3:f818ea5172ed | 120 | virtual string message(int condition) const override { |
IanBenzMaxim | 3:f818ea5172ed | 121 | switch (condition) { |
IanBenzMaxim | 3:f818ea5172ed | 122 | case QSerialPort::DeviceNotFoundError: |
IanBenzMaxim | 3:f818ea5172ed | 123 | return "Device Not Found"; |
IanBenzMaxim | 3:f818ea5172ed | 124 | |
IanBenzMaxim | 3:f818ea5172ed | 125 | case QSerialPort::PermissionError: |
IanBenzMaxim | 3:f818ea5172ed | 126 | return "Permission Error"; |
IanBenzMaxim | 3:f818ea5172ed | 127 | |
IanBenzMaxim | 3:f818ea5172ed | 128 | case QSerialPort::OpenError: |
IanBenzMaxim | 3:f818ea5172ed | 129 | return "Open Error"; |
IanBenzMaxim | 3:f818ea5172ed | 130 | |
IanBenzMaxim | 3:f818ea5172ed | 131 | case QSerialPort::NotOpenError: |
IanBenzMaxim | 3:f818ea5172ed | 132 | return "Not Open Error"; |
IanBenzMaxim | 3:f818ea5172ed | 133 | |
IanBenzMaxim | 3:f818ea5172ed | 134 | case QSerialPort::WriteError: |
IanBenzMaxim | 3:f818ea5172ed | 135 | return "Write Error"; |
IanBenzMaxim | 3:f818ea5172ed | 136 | |
IanBenzMaxim | 3:f818ea5172ed | 137 | case QSerialPort::ReadError: |
IanBenzMaxim | 3:f818ea5172ed | 138 | return "Read Error"; |
IanBenzMaxim | 3:f818ea5172ed | 139 | |
IanBenzMaxim | 3:f818ea5172ed | 140 | case QSerialPort::ResourceError: |
IanBenzMaxim | 3:f818ea5172ed | 141 | return "Resource Error"; |
IanBenzMaxim | 3:f818ea5172ed | 142 | |
IanBenzMaxim | 3:f818ea5172ed | 143 | case QSerialPort::UnsupportedOperationError: |
IanBenzMaxim | 3:f818ea5172ed | 144 | return "Unsupported Operation Error"; |
IanBenzMaxim | 3:f818ea5172ed | 145 | |
IanBenzMaxim | 3:f818ea5172ed | 146 | case QSerialPort::TimeoutError: |
IanBenzMaxim | 3:f818ea5172ed | 147 | return "Timeout Error"; |
IanBenzMaxim | 3:f818ea5172ed | 148 | |
IanBenzMaxim | 3:f818ea5172ed | 149 | default: |
IanBenzMaxim | 3:f818ea5172ed | 150 | return defaultErrorMessage(condition); |
IanBenzMaxim | 3:f818ea5172ed | 151 | } |
IanBenzMaxim | 3:f818ea5172ed | 152 | } |
IanBenzMaxim | 3:f818ea5172ed | 153 | |
IanBenzMaxim | 3:f818ea5172ed | 154 | // Make QSerialPort::TimeoutError equivalent to Uart::TimeoutError. |
IanBenzMaxim | 3:f818ea5172ed | 155 | virtual bool equivalent(int code, |
IanBenzMaxim | 3:f818ea5172ed | 156 | const error_condition & condition) const override { |
IanBenzMaxim | 3:f818ea5172ed | 157 | return (code == QSerialPort::TimeoutError) |
IanBenzMaxim | 3:f818ea5172ed | 158 | ? (condition == make_error_condition(Uart::TimeoutError)) |
IanBenzMaxim | 3:f818ea5172ed | 159 | : error_category::equivalent(code, condition); |
IanBenzMaxim | 3:f818ea5172ed | 160 | } |
IanBenzMaxim | 3:f818ea5172ed | 161 | } instance; |
IanBenzMaxim | 3:f818ea5172ed | 162 | return instance; |
IanBenzMaxim | 3:f818ea5172ed | 163 | } |
IanBenzMaxim | 3:f818ea5172ed | 164 | |
IanBenzMaxim | 3:f818ea5172ed | 165 | } // namespace Qt |
IanBenzMaxim | 3:f818ea5172ed | 166 | } // namespace MaximInterface |