Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Platforms/dotnet/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 <msclr/auto_gcroot.h> |
IanBenzMaxim | 3:f818ea5172ed | 34 | #include <msclr/marshal_cppstd.h> |
IanBenzMaxim | 3:f818ea5172ed | 35 | #include <MaximInterface/Utilities/Error.hpp> |
IanBenzMaxim | 3:f818ea5172ed | 36 | #include "ChangeSizeType.hpp" |
IanBenzMaxim | 3:f818ea5172ed | 37 | #include "Sleep.hpp" |
IanBenzMaxim | 3:f818ea5172ed | 38 | #include "SerialPort.hpp" |
IanBenzMaxim | 3:f818ea5172ed | 39 | |
IanBenzMaxim | 3:f818ea5172ed | 40 | using msclr::interop::marshal_as; |
IanBenzMaxim | 3:f818ea5172ed | 41 | using std::string; |
IanBenzMaxim | 3:f818ea5172ed | 42 | using namespace System; |
IanBenzMaxim | 3:f818ea5172ed | 43 | using System::Runtime::InteropServices::Marshal; |
IanBenzMaxim | 3:f818ea5172ed | 44 | |
IanBenzMaxim | 3:f818ea5172ed | 45 | namespace MaximInterface { |
IanBenzMaxim | 3:f818ea5172ed | 46 | namespace dotnet { |
IanBenzMaxim | 3:f818ea5172ed | 47 | |
IanBenzMaxim | 3:f818ea5172ed | 48 | template <typename Type, typename Input> static bool isType(Input in) { |
IanBenzMaxim | 3:f818ea5172ed | 49 | return dynamic_cast<Type>(in) != nullptr; |
IanBenzMaxim | 3:f818ea5172ed | 50 | } |
IanBenzMaxim | 3:f818ea5172ed | 51 | |
IanBenzMaxim | 3:f818ea5172ed | 52 | template <typename Func> |
IanBenzMaxim | 3:f818ea5172ed | 53 | static error_code executeTryCatchOperation(Func tryOperation) { |
IanBenzMaxim | 3:f818ea5172ed | 54 | return executeTryCatchOperation(tryOperation, [] {}); |
IanBenzMaxim | 3:f818ea5172ed | 55 | } |
IanBenzMaxim | 3:f818ea5172ed | 56 | |
IanBenzMaxim | 3:f818ea5172ed | 57 | template <typename TryFunc, typename CatchFunc> |
IanBenzMaxim | 3:f818ea5172ed | 58 | static error_code executeTryCatchOperation(TryFunc tryOperation, |
IanBenzMaxim | 3:f818ea5172ed | 59 | CatchFunc catchOperation) { |
IanBenzMaxim | 3:f818ea5172ed | 60 | error_code result; |
IanBenzMaxim | 3:f818ea5172ed | 61 | try { |
IanBenzMaxim | 3:f818ea5172ed | 62 | tryOperation(); |
IanBenzMaxim | 3:f818ea5172ed | 63 | } catch (Exception ^ e) { |
IanBenzMaxim | 3:f818ea5172ed | 64 | catchOperation(); |
IanBenzMaxim | 3:f818ea5172ed | 65 | if (isType<ArgumentException ^>(e)) { |
IanBenzMaxim | 3:f818ea5172ed | 66 | result = make_error_code(SerialPort::ArgumentError); |
IanBenzMaxim | 3:f818ea5172ed | 67 | } else if (isType<InvalidOperationException ^>(e)) { |
IanBenzMaxim | 3:f818ea5172ed | 68 | result = make_error_code(SerialPort::InvalidOperationError); |
IanBenzMaxim | 3:f818ea5172ed | 69 | } else if (isType<System::IO::IOException ^>(e)) { |
IanBenzMaxim | 3:f818ea5172ed | 70 | result = make_error_code(SerialPort::IOError); |
IanBenzMaxim | 3:f818ea5172ed | 71 | } else if (isType<UnauthorizedAccessException ^>(e)) { |
IanBenzMaxim | 3:f818ea5172ed | 72 | result = make_error_code(SerialPort::UnauthorizedAccessError); |
IanBenzMaxim | 3:f818ea5172ed | 73 | } else if (isType<TimeoutException ^>(e)) { |
IanBenzMaxim | 3:f818ea5172ed | 74 | result = make_error_code(SerialPort::TimeoutError); |
IanBenzMaxim | 3:f818ea5172ed | 75 | } else { |
IanBenzMaxim | 3:f818ea5172ed | 76 | throw; |
IanBenzMaxim | 3:f818ea5172ed | 77 | } |
IanBenzMaxim | 3:f818ea5172ed | 78 | } |
IanBenzMaxim | 3:f818ea5172ed | 79 | return result; |
IanBenzMaxim | 3:f818ea5172ed | 80 | } |
IanBenzMaxim | 3:f818ea5172ed | 81 | |
IanBenzMaxim | 3:f818ea5172ed | 82 | template <typename Func> |
IanBenzMaxim | 3:f818ea5172ed | 83 | static error_code executeIfConnected(const SerialPort & serial, |
IanBenzMaxim | 3:f818ea5172ed | 84 | Func operation) { |
IanBenzMaxim | 3:f818ea5172ed | 85 | return serial.connected() ? operation() |
IanBenzMaxim | 3:f818ea5172ed | 86 | : make_error_code(SerialPort::NotConnectedError); |
IanBenzMaxim | 3:f818ea5172ed | 87 | } |
IanBenzMaxim | 3:f818ea5172ed | 88 | |
IanBenzMaxim | 3:f818ea5172ed | 89 | struct SerialPort::Data { |
IanBenzMaxim | 3:f818ea5172ed | 90 | msclr::auto_gcroot<System::IO::Ports::SerialPort ^> port; |
IanBenzMaxim | 3:f818ea5172ed | 91 | }; |
IanBenzMaxim | 3:f818ea5172ed | 92 | |
IanBenzMaxim | 3:f818ea5172ed | 93 | SerialPort::SerialPort() : data(std::make_unique<Data>()) {} |
IanBenzMaxim | 3:f818ea5172ed | 94 | |
IanBenzMaxim | 3:f818ea5172ed | 95 | SerialPort::~SerialPort() = default; |
IanBenzMaxim | 3:f818ea5172ed | 96 | |
IanBenzMaxim | 3:f818ea5172ed | 97 | SerialPort::SerialPort(SerialPort &&) noexcept = default; |
IanBenzMaxim | 3:f818ea5172ed | 98 | |
IanBenzMaxim | 3:f818ea5172ed | 99 | SerialPort & SerialPort::operator=(SerialPort &&) noexcept = default; |
IanBenzMaxim | 3:f818ea5172ed | 100 | |
IanBenzMaxim | 3:f818ea5172ed | 101 | error_code SerialPort::connect(const string & portName) { |
IanBenzMaxim | 3:f818ea5172ed | 102 | data->port = gcnew System::IO::Ports::SerialPort; |
IanBenzMaxim | 3:f818ea5172ed | 103 | return executeTryCatchOperation( |
IanBenzMaxim | 3:f818ea5172ed | 104 | [this, &portName] { |
IanBenzMaxim | 3:f818ea5172ed | 105 | data->port->PortName = marshal_as<String ^>(portName); |
IanBenzMaxim | 3:f818ea5172ed | 106 | data->port->DtrEnable = true; |
IanBenzMaxim | 3:f818ea5172ed | 107 | data->port->ReadTimeout = 1000; |
IanBenzMaxim | 3:f818ea5172ed | 108 | data->port->WriteTimeout = 1000; |
IanBenzMaxim | 3:f818ea5172ed | 109 | data->port->Open(); |
IanBenzMaxim | 3:f818ea5172ed | 110 | }, |
IanBenzMaxim | 3:f818ea5172ed | 111 | [this] { data->port.reset(); }); |
IanBenzMaxim | 3:f818ea5172ed | 112 | } |
IanBenzMaxim | 3:f818ea5172ed | 113 | |
IanBenzMaxim | 3:f818ea5172ed | 114 | error_code SerialPort::disconnect() { |
IanBenzMaxim | 3:f818ea5172ed | 115 | data->port.reset(); |
IanBenzMaxim | 3:f818ea5172ed | 116 | return error_code(); |
IanBenzMaxim | 3:f818ea5172ed | 117 | } |
IanBenzMaxim | 3:f818ea5172ed | 118 | |
IanBenzMaxim | 3:f818ea5172ed | 119 | bool SerialPort::connected() const { return data->port.get() != nullptr; } |
IanBenzMaxim | 3:f818ea5172ed | 120 | |
IanBenzMaxim | 3:f818ea5172ed | 121 | string SerialPort::portName() const { |
IanBenzMaxim | 3:f818ea5172ed | 122 | return connected() ? marshal_as<string>(data->port->PortName) : ""; |
IanBenzMaxim | 3:f818ea5172ed | 123 | } |
IanBenzMaxim | 3:f818ea5172ed | 124 | |
IanBenzMaxim | 3:f818ea5172ed | 125 | error_code SerialPort::setBaudRate(int_least32_t baudRate) { |
IanBenzMaxim | 3:f818ea5172ed | 126 | return executeIfConnected(*this, [this, baudRate] { |
IanBenzMaxim | 3:f818ea5172ed | 127 | return executeTryCatchOperation( |
IanBenzMaxim | 3:f818ea5172ed | 128 | [this, baudRate] { data->port->BaudRate = baudRate; }); |
IanBenzMaxim | 3:f818ea5172ed | 129 | }); |
IanBenzMaxim | 3:f818ea5172ed | 130 | } |
IanBenzMaxim | 3:f818ea5172ed | 131 | |
IanBenzMaxim | 3:f818ea5172ed | 132 | error_code SerialPort::sendBreak() { |
IanBenzMaxim | 3:f818ea5172ed | 133 | return executeIfConnected(*this, [this] { |
IanBenzMaxim | 3:f818ea5172ed | 134 | return executeTryCatchOperation([this] { |
IanBenzMaxim | 3:f818ea5172ed | 135 | data->port->BreakState = true; |
IanBenzMaxim | 3:f818ea5172ed | 136 | sleep(1); |
IanBenzMaxim | 3:f818ea5172ed | 137 | data->port->BreakState = false; |
IanBenzMaxim | 3:f818ea5172ed | 138 | }); |
IanBenzMaxim | 3:f818ea5172ed | 139 | }); |
IanBenzMaxim | 3:f818ea5172ed | 140 | } |
IanBenzMaxim | 3:f818ea5172ed | 141 | |
IanBenzMaxim | 3:f818ea5172ed | 142 | error_code SerialPort::clearReadBuffer() { |
IanBenzMaxim | 3:f818ea5172ed | 143 | return executeIfConnected(*this, [this] { |
IanBenzMaxim | 3:f818ea5172ed | 144 | return executeTryCatchOperation([this] { data->port->ReadExisting(); }); |
IanBenzMaxim | 3:f818ea5172ed | 145 | }); |
IanBenzMaxim | 3:f818ea5172ed | 146 | } |
IanBenzMaxim | 3:f818ea5172ed | 147 | |
IanBenzMaxim | 3:f818ea5172ed | 148 | error_code SerialPort::writeByte(uint_least8_t data) { |
IanBenzMaxim | 3:f818ea5172ed | 149 | return writeBlock(&data, 1); |
IanBenzMaxim | 3:f818ea5172ed | 150 | } |
IanBenzMaxim | 3:f818ea5172ed | 151 | |
IanBenzMaxim | 3:f818ea5172ed | 152 | error_code SerialPort::writeBlock(const uint_least8_t * data, size_t dataLen) { |
IanBenzMaxim | 3:f818ea5172ed | 153 | return executeIfConnected(*this, [this, data, dataLen] { |
IanBenzMaxim | 3:f818ea5172ed | 154 | return executeTryCatchOperation([this, data, dataLen] { |
IanBenzMaxim | 3:f818ea5172ed | 155 | changeSizeType<int>( |
IanBenzMaxim | 3:f818ea5172ed | 156 | [this](const uint_least8_t * dataChunk, int dataChunkSize) { |
IanBenzMaxim | 3:f818ea5172ed | 157 | auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 158 | Marshal::Copy( |
IanBenzMaxim | 3:f818ea5172ed | 159 | static_cast<IntPtr>(const_cast<uint_least8_t *>(dataChunk)), |
IanBenzMaxim | 3:f818ea5172ed | 160 | dataManaged, 0, dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 161 | this->data->port->Write(dataManaged, 0, dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 162 | }, |
IanBenzMaxim | 3:f818ea5172ed | 163 | data, dataLen); |
IanBenzMaxim | 3:f818ea5172ed | 164 | }); |
IanBenzMaxim | 3:f818ea5172ed | 165 | }); |
IanBenzMaxim | 3:f818ea5172ed | 166 | } |
IanBenzMaxim | 3:f818ea5172ed | 167 | |
IanBenzMaxim | 3:f818ea5172ed | 168 | error_code SerialPort::readByte(uint_least8_t & data) { |
IanBenzMaxim | 3:f818ea5172ed | 169 | return executeIfConnected(*this, [this, &data] { |
IanBenzMaxim | 3:f818ea5172ed | 170 | return executeTryCatchOperation( |
IanBenzMaxim | 3:f818ea5172ed | 171 | [this, &data] { data = this->data->port->ReadByte(); }); |
IanBenzMaxim | 3:f818ea5172ed | 172 | }); |
IanBenzMaxim | 3:f818ea5172ed | 173 | } |
IanBenzMaxim | 3:f818ea5172ed | 174 | |
IanBenzMaxim | 3:f818ea5172ed | 175 | error_code SerialPort::readBlock(uint_least8_t * data, size_t dataLen) { |
IanBenzMaxim | 3:f818ea5172ed | 176 | return executeIfConnected(*this, [this, data, dataLen] { |
IanBenzMaxim | 3:f818ea5172ed | 177 | return executeTryCatchOperation([this, data, dataLen] { |
IanBenzMaxim | 3:f818ea5172ed | 178 | changeSizeType<int>( |
IanBenzMaxim | 3:f818ea5172ed | 179 | [this](uint_least8_t * dataChunk, int dataChunkSize) { |
IanBenzMaxim | 3:f818ea5172ed | 180 | auto dataManaged = gcnew array<uint_least8_t>(dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 181 | int bytesRead = 0; |
IanBenzMaxim | 3:f818ea5172ed | 182 | do { |
IanBenzMaxim | 3:f818ea5172ed | 183 | bytesRead += this->data->port->Read(dataManaged, bytesRead, |
IanBenzMaxim | 3:f818ea5172ed | 184 | dataChunkSize - bytesRead); |
IanBenzMaxim | 3:f818ea5172ed | 185 | } while (bytesRead < dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 186 | Marshal::Copy(dataManaged, 0, static_cast<IntPtr>(dataChunk), |
IanBenzMaxim | 3:f818ea5172ed | 187 | dataChunkSize); |
IanBenzMaxim | 3:f818ea5172ed | 188 | }, |
IanBenzMaxim | 3:f818ea5172ed | 189 | data, dataLen); |
IanBenzMaxim | 3:f818ea5172ed | 190 | }); |
IanBenzMaxim | 3:f818ea5172ed | 191 | }); |
IanBenzMaxim | 3:f818ea5172ed | 192 | } |
IanBenzMaxim | 3:f818ea5172ed | 193 | |
IanBenzMaxim | 3:f818ea5172ed | 194 | const error_category & SerialPort::errorCategory() { |
IanBenzMaxim | 3:f818ea5172ed | 195 | static class : public error_category { |
IanBenzMaxim | 3:f818ea5172ed | 196 | public: |
IanBenzMaxim | 3:f818ea5172ed | 197 | virtual const char * name() const override { return "dotnet SerialPort"; } |
IanBenzMaxim | 3:f818ea5172ed | 198 | |
IanBenzMaxim | 3:f818ea5172ed | 199 | virtual string message(int condition) const override { |
IanBenzMaxim | 3:f818ea5172ed | 200 | switch (condition) { |
IanBenzMaxim | 3:f818ea5172ed | 201 | case NotConnectedError: |
IanBenzMaxim | 3:f818ea5172ed | 202 | return "Not Connected Error"; |
IanBenzMaxim | 3:f818ea5172ed | 203 | |
IanBenzMaxim | 3:f818ea5172ed | 204 | case ArgumentError: |
IanBenzMaxim | 3:f818ea5172ed | 205 | return "Argument Error"; |
IanBenzMaxim | 3:f818ea5172ed | 206 | |
IanBenzMaxim | 3:f818ea5172ed | 207 | case InvalidOperationError: |
IanBenzMaxim | 3:f818ea5172ed | 208 | return "Invalid Operation Error"; |
IanBenzMaxim | 3:f818ea5172ed | 209 | |
IanBenzMaxim | 3:f818ea5172ed | 210 | case IOError: |
IanBenzMaxim | 3:f818ea5172ed | 211 | return "IO Error"; |
IanBenzMaxim | 3:f818ea5172ed | 212 | |
IanBenzMaxim | 3:f818ea5172ed | 213 | case UnauthorizedAccessError: |
IanBenzMaxim | 3:f818ea5172ed | 214 | return "Unauthorized Access Error"; |
IanBenzMaxim | 3:f818ea5172ed | 215 | |
IanBenzMaxim | 3:f818ea5172ed | 216 | default: |
IanBenzMaxim | 3:f818ea5172ed | 217 | return defaultErrorMessage(condition); |
IanBenzMaxim | 3:f818ea5172ed | 218 | } |
IanBenzMaxim | 3:f818ea5172ed | 219 | } |
IanBenzMaxim | 3:f818ea5172ed | 220 | } instance; |
IanBenzMaxim | 3:f818ea5172ed | 221 | return instance; |
IanBenzMaxim | 3:f818ea5172ed | 222 | } |
IanBenzMaxim | 3:f818ea5172ed | 223 | |
IanBenzMaxim | 3:f818ea5172ed | 224 | } // namespace dotnet |
IanBenzMaxim | 3:f818ea5172ed | 225 | } // namespace MaximInterface |