COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
serialclass.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 
30 #ifndef SERIALCLASS_H
31 #define SERIALCLASS_H
32 
33 #include "support/configCosmos.h"
34 #include "support/sliplib.h"
35 #include "support/cosmos-errno.h"
36 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
37 #include <termios.h>
38 #include <sys/select.h>
39 #endif
40 
41 
42 namespace Cosmos {
43  class Serial
44  {
45  public:
46  Serial(string dname, size_t dbaud=9600, size_t dbits=8, size_t dparity=0, size_t dstop=1);
47  ~Serial();
48  int32_t open_device();
49  int32_t close_device();
50  int32_t set_restoreonclose(bool argument);
51  int32_t set_params(size_t dbaud, size_t dbits, size_t dparity, size_t dstop);
52  int32_t set_flowcontrol(bool rtscts, bool xonxoff);
53 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
54  int32_t set_timeout(int minchar, double timeout);
55 #else // Windows
56  int32_t set_timeout(int, double timeout);
57 #endif
58  int32_t set_timeout(double timeout);
59  int32_t set_dtr(bool state);
60  int32_t set_rts(bool state);
61  bool get_cts();
62  int32_t put_char(uint8_t c);
63  int32_t put_string(string data);
64  int32_t put_data(vector <uint8_t> data);
65  int32_t put_data(const uint8_t *data, size_t size);
66  int32_t put_slip(vector <uint8_t> data);
67  int32_t put_nmea(vector <uint8_t> data);
68  int32_t drain();
69  int32_t poll_char();
70  int32_t get_char();
71  int32_t get_char(uint8_t &buffer);
72  int32_t get_data(vector <uint8_t> &data, size_t size=SIZE_MAX);
73  int32_t get_string(string &data, size_t size=SIZE_MAX);
74  int32_t get_string(string &data, char endc=0);
75  int32_t get_data(uint8_t *data, size_t size);
76  int32_t get_slip(vector <uint8_t> &data, size_t size=SIZE_MAX);
77  int32_t get_nmea(vector <uint8_t> &data, size_t size);
78  int32_t get_xmodem(vector <uint8_t> &data, size_t size);
79  int32_t get_error();
80  bool get_open();
81 
82 
83  int32_t SendByte(uint8_t byte);
84  int32_t ReceiveBuffer(uint8_t *buf, int size);
85  int32_t ReceiveByte(uint8_t &buf);
86  int32_t SendBuffer(uint8_t *buffer, int size);
87  private:
88  int fd = -1; /* tty file descriptor */
89  int32_t error;
90  vector <uint32_t> baud_speed[2] = {
91  {0, 50, 75, 110, 134, 150, 200, 300, 500, 1200, 1800, 2400, 4800, 9600, 19200, 38400},
92  {57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000}
93  };
94  // timeout in sec 2 ms
95  double ictimeout = .002;
96  bool restoreonclose = true;
97 
98 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
99  struct termios tio; /* termios structure for the port */
100  struct termios oldtio; /* old termios structure */
101 #else // windows
102  DCB dcb; // port settings
103  HANDLE handle;
104 #endif
105 
106  string name;
107  size_t baud;
108  size_t bits;
109  size_t parity;
110  size_t stop;
111 
112 #define XMODEM_SOH 0x01
113 #define XMODEM_EOT 0x04
114 #define XMODEM_ACK 0x06
115 #define XMODEM_NAK 0x15
116 #define XMODEM_CAN 0x18
117 
118  };
119 
120 
121 } // end of namepsace Cosmos
122 #endif // SERIALCLASS_H
Definition: agentclass.cpp:54
int32_t get_xmodem(vector< uint8_t > &data, size_t size)
Read Xmodem frame.
Definition: serialclass.cpp:1268
Serial(string dname, size_t dbaud=9600, size_t dbits=8, size_t dparity=0, size_t dstop=1)
Definition: serialclass.cpp:49
int32_t put_data(vector< uint8_t > data)
Definition: serialclass.cpp:707
COSMOS SLIP support include file.
int32_t close_device()
Definition: serialclass.cpp:126
int32_t put_string(string data)
Definition: serialclass.cpp:686
vector< uint32_t > baud_speed[2]
Definition: serialclass.h:90
string name
Definition: serialclass.h:106
Definition: serialclass.h:43
int32_t set_params(size_t dbaud, size_t dbits, size_t dparity, size_t dstop)
Definition: serialclass.cpp:181
int32_t put_nmea(vector< uint8_t > data)
Definition: serialclass.cpp:825
int32_t get_char()
Definition: serialclass.cpp:987
bool restoreonclose
Definition: serialclass.h:96
int32_t put_slip(vector< uint8_t > data)
Definition: serialclass.cpp:763
int32_t put_char(uint8_t c)
Definition: serialclass.cpp:609
size_t parity
Definition: serialclass.h:109
int32_t set_restoreonclose(bool argument)
Definition: serialclass.cpp:175
static char buffer[255]
Definition: propagator_simple.cpp:60
int32_t drain()
Definition: serialclass.cpp:927
Headers and definitions common to all COSMOS.
size_t baud
Definition: serialclass.h:107
int32_t set_dtr(bool state)
Definition: serialclass.cpp:447
~Serial()
Definition: serialclass.cpp:153
int fd
Definition: serialclass.h:88
COSMOS Error Codes.
size_t bits
Definition: serialclass.h:108
int32_t set_rts(bool state)
Definition: serialclass.cpp:474
int32_t poll_char()
Definition: serialclass.cpp:1072
int32_t open_device()
Definition: serialclass.cpp:60
bool get_open()
Definition: serialclass.cpp:158
bool get_cts()
Definition: serialclass.cpp:501
int32_t ReceiveByte(uint8_t &buf)
Definition: serialclass.cpp:963
int32_t get_nmea(vector< uint8_t > &data, size_t size)
Read NMEA response.
Definition: serialclass.cpp:1400
int32_t set_timeout(int, double timeout)
Definition: serialclass.cpp:515
int32_t get_string(string &data, size_t size=SIZE_MAX)
Definition: serialclass.cpp:1141
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
char buf[128]
Definition: rw_test.cpp:40
int32_t set_flowcontrol(bool rtscts, bool xonxoff)
Definition: serialclass.cpp:386
int32_t get_data(vector< uint8_t > &data, size_t size=SIZE_MAX)
Definition: serialclass.cpp:1202
int32_t error
Definition: serialclass.h:89
int32_t SendByte(uint8_t byte)
Definition: serialclass.cpp:547
size_t stop
Definition: serialclass.h:110
double ictimeout
Definition: serialclass.h:95
int32_t SendBuffer(uint8_t *buffer, int size)
Definition: serialclass.cpp:578
int32_t get_error()
Definition: serialclass.cpp:170
int32_t get_slip(vector< uint8_t > &data, size_t size=SIZE_MAX)
Read SLIP frame.
Definition: serialclass.cpp:1322
int32_t ReceiveBuffer(uint8_t *buf, int size)
Definition: serialclass.cpp:974