| 1 |
/******************************************************************************* |
| 2 |
Copyright(c) 2017 Jasem Mutlaq. All rights reserved. |
| 3 |
|
| 4 |
Connection Plugin Interface |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public |
| 8 |
License version 2 as published by the Free Software Foundation. |
| 9 |
|
| 10 |
This library 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 GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public License |
| 16 |
along with this library; see the file COPYING.LIB. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
Boston, MA 02110-1301, USA. |
| 19 |
*******************************************************************************/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include "connectioninterface.h" |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
namespace Connection |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @brief The Serial class manages connection with serial devices including Bluetooth. Serial communication is still the predominat |
| 32 |
* method to communicate with astronomical devices such as mounts, focusers, filter wheels..etc. The default connection |
| 33 |
* parameters are 9600 8N1 (9600 Baud Rate, 8 data bits, no parity, 1 stop bit). All the parameters can be updated and read via |
| 34 |
* the getters and setters of the class. |
| 35 |
* The default port is <i>/dev/ttyUSB0</i> under Linux and <i>/dev/cu.usbserial</i> under MacOS. After serial connection is established |
| 36 |
* successfully, |
| 37 |
*/ |
| 38 |
class Serial : public Interface |
| 39 |
{ |
| 40 |
public: |
| 41 |
/** |
| 42 |
* \typedef BaudRate |
| 43 |
* \brief Supported baud rates |
| 44 |
* \note: Default baud rate is 9600. To change default baud rate, use setDefaultBaudrate(..) function. |
| 45 |
*/ |
| 46 |
typedef enum { B_9600, B_19200, B_38400, B_57600, B_115200, B_230400 } BaudRate; |
| 47 |
|
| 48 |
Serial(INDI::DefaultDevice *dev); |
| 49 |
virtual ~Serial(); |
| 50 |
|
| 51 |
virtual bool Connect() override; |
| 52 |
|
| 53 |
virtual bool Disconnect() override; |
| 54 |
|
| 55 |
virtual void Activated() override; |
| 56 |
|
| 57 |
virtual void Deactivated() override; |
| 58 |
|
| 59 |
virtual std::string name() override |
| 60 |
{ |
| 61 |
return "CONNECTION_SERIAL"; |
| 62 |
} |
| 63 |
|
| 64 |
virtual std::string label() override |
| 65 |
{ |
| 66 |
return "Serial"; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @return Currently active device port |
| 71 |
*/ |
| 72 |
virtual const char *port() |
| 73 |
{ |
| 74 |
return PortT[0].text; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return Currently active baud rate raw value (e.g. 9600, 19200..etc) |
| 79 |
*/ |
| 80 |
virtual uint32_t baud(); |
| 81 |
|
| 82 |
/** |
| 83 |
* @brief setDefaultPort Set default port. Call this function in initProperties() of your driver |
| 84 |
* if you want to change default port. |
| 85 |
* @param port Name of desired default port |
| 86 |
*/ |
| 87 |
void setDefaultPort(const char *port); |
| 88 |
|
| 89 |
/** |
| 90 |
* @brief setDefaultBaudRate Set default baud rate. The default baud rate is 9600 unless |
| 91 |
* otherwise changed by this function. Call this function in initProperties() of your driver. |
| 92 |
* @param newRate Desired new rate |
| 93 |
*/ |
| 94 |
void setDefaultBaudRate(BaudRate newRate); |
| 95 |
|
| 96 |
/** |
| 97 |
* @return Return port file descriptor. If connection is successful, PortFD is a positive |
| 98 |
* integer otherwise it is set to -1 |
| 99 |
*/ |
| 100 |
int getPortFD() const |
| 101 |
{ |
| 102 |
return PortFD; |
| 103 |
} |
| 104 |
|
| 105 |
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override; |
| 106 |
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override; |
| 107 |
virtual bool saveConfigItems(FILE *fp) override; |
| 108 |
|
| 109 |
/** |
| 110 |
* Refresh the list of system ports |
| 111 |
*/ |
| 112 |
bool Refresh(bool silent = false); |
| 113 |
|
| 114 |
uint8_t getWordSize() const |
| 115 |
{ |
| 116 |
return wordSize; |
| 117 |
} |
| 118 |
/** |
| 119 |
* @brief setWordSize Set word size to be used in the serial connection. Default 8 |
| 120 |
*/ |
| 121 |
void setWordSize(const uint8_t &value) |
| 122 |
{ |
| 123 |
wordSize = value; |
| 124 |
} |
| 125 |
|
| 126 |
uint8_t getParity() const |
| 127 |
{ |
| 128 |
return parity ; |
| 129 |
} |
| 130 |
/** |
| 131 |
* @brief setParity Set parity to be used in the serial connection. Default 0 (NONE) |
| 132 |
* @param value 0 for NONE, 1 for EVEN, 2 for ODD |
| 133 |
*/ |
| 134 |
void setParity(const uint8_t &value) |
| 135 |
{ |
| 136 |
parity = value; |
| 137 |
} |
| 138 |
|
| 139 |
uint8_t getStopBits() const |
| 140 |
{ |
| 141 |
return stopBits; |
| 142 |
} |
| 143 |
/** |
| 144 |
* @brief setStopBits Set stop bits to be used in the serial connection. Default 0 |
| 145 |
*/ |
| 146 |
void setStopBits(const uint8_t &value) |
| 147 |
{ |
| 148 |
stopBits = value ; |
| 149 |
} |
| 150 |
|
| 151 |
protected: |
| 152 |
/** |
| 153 |
* \brief Connect to serial port device. Default parameters are 8 bits, 1 stop bit, no parity. |
| 154 |
* Override if different from default. |
| 155 |
* \param port Port to connect to. |
| 156 |
* \param baud Baud rate |
| 157 |
* \return True if connection is successful, false otherwise |
| 158 |
* \warning Do not call this function directly, it is called by Connection::Serial Connect() function. |
| 159 |
*/ |
| 160 |
virtual bool Connect(const char *port, uint32_t baud); |
| 161 |
|
| 162 |
virtual bool processHandshake(); |
| 163 |
|
| 164 |
enum |
| 165 |
{ |
| 166 |
SERIAL_DEV, |
| 167 |
USB_ID_DEV, |
| 168 |
BLUETOOTH_DEV |
| 169 |
}; |
| 170 |
|
| 171 |
// Device physical port |
| 172 |
ITextVectorProperty PortTP; |
| 173 |
IText PortT[1] {}; |
| 174 |
|
| 175 |
ISwitch BaudRateS[6]; |
| 176 |
ISwitchVectorProperty BaudRateSP; |
| 177 |
|
| 178 |
ISwitch AutoSearchS[2]; |
| 179 |
ISwitchVectorProperty AutoSearchSP; |
| 180 |
|
| 181 |
ISwitch *SystemPortS = nullptr; |
| 182 |
ISwitchVectorProperty SystemPortSP; |
| 183 |
|
| 184 |
ISwitch RefreshS[1]; |
| 185 |
ISwitchVectorProperty RefreshSP; |
| 186 |
|
| 187 |
int PortFD = -1; |
| 188 |
|
| 189 |
// Default 8N1 parameters |
| 190 |
uint8_t wordSize = 8; |
| 191 |
uint8_t parity = 0; |
| 192 |
uint8_t stopBits = 1; |
| 193 |
|
| 194 |
std::string m_ConfigPort; |
| 195 |
int m_ConfigBaudRate {-1}; |
| 196 |
std::vector<std::string> m_SystemPorts; |
| 197 |
}; |
| 198 |
} |