charinterface.h
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #ifndef CHARINTERFACE_H_
15 #define CHARINTERFACE_H_
16 
17 #include "interface.h"
18 
19 //#include <stdarg.h>
20 
21 class DECLSPEC_SHARED XCustomCharInterface : public XInterface {
22 public:
23  XCustomCharInterface(const char *name, bool runtime, const shared_ptr<XDriver> &driver);
24  virtual ~XCustomCharInterface() = default;
25 
26  //! Buffer is Thread-Local-Strage.
27  //! Therefore, be careful when you access multi-interfaces in one thread.
28  //! \sa XThreadLocal
29  const std::vector<char> &buffer() const {return *s_tlBuffer;}
30  //! error-check is user's responsibility.
31  int scanf(const char *format, ...) const
32 #if defined __GNUC__ || defined __clang__
33  __attribute__ ((format(scanf,2,3)))
34 #endif
35  ;
36  double toDouble() const throw (XConvError &);
37  int toInt() const throw (XConvError &);
38  unsigned int toUInt() const throw (XConvError &);
39  XString toStr() const;
40  XString toStrSimplified() const; //!< returns string white-spaces stripped.
41 
42  //! format version of send()
43  //! \sa printf()
44  void sendf(const char *format, ...) throw (XInterfaceError &)
45 #if defined __GNUC__ || defined __clang__
46  __attribute__ ((format(printf,2,3)))
47 #endif
48  ;
49  //! format version of query()
50  //! \sa printf()
51  void queryf(const char *format, ...) throw (XInterfaceError &)
52 #if defined __GNUC__ || defined __clang__
53  __attribute__ ((format(printf,2,3)))
54 #endif
55  ;
56 
57  void setEOS(const char *str);
58  const XString &eos() const {return m_eos;}
59 
60  void query(const XString &str) throw (XCommError &);
61  virtual void query(const char *str) throw (XCommError &);
62  virtual void send(const char *str) throw (XCommError &) = 0;
63  virtual void receive() throw (XCommError &) = 0;
64 
65  virtual bool isOpened() const = 0;
66 
67  //! only for XPort and internal use.
68  static std::vector<char> &buffer_receive() {return *s_tlBuffer;}
69 protected:
70  virtual void open() throw (XInterfaceError &) = 0;
71  //! This can be called even if has already closed.
72  virtual void close() throw (XInterfaceError &) = 0;
73 
74 private:
75  static XThreadLocal<std::vector<char> > s_tlBuffer;
76  XString m_eos;
77 };
78 
79 class XPort;
80 //! Standard interface for character devices. e.g. GPIB, serial port, TCP/IP...
81 class DECLSPEC_SHARED XCharInterface : public XCustomCharInterface {
82 public:
83  XCharInterface(const char *name, bool runtime, const shared_ptr<XDriver> &driver);
84  virtual ~XCharInterface() = default;
85 
86  void setGPIBUseSerialPollOnWrite(bool x) {m_bGPIBUseSerialPollOnWrite = x;}
87  void setGPIBUseSerialPollOnRead(bool x) {m_bGPIBUseSerialPollOnRead = x;}
88  void setGPIBWaitBeforeWrite(int msec) {m_gpibWaitBeforeWrite = msec;}
89  void setGPIBWaitBeforeRead(int msec) {m_gpibWaitBeforeRead = msec;}
90  void setGPIBWaitBeforeSPoll(int msec) {m_gpibWaitBeforeSPoll = msec;}
91  void setGPIBMAVbit(unsigned char x) {m_gpibMAVbit = x;}
92 
93  bool gpibUseSerialPollOnWrite() const {return m_bGPIBUseSerialPollOnWrite;}
94  bool gpibUseSerialPollOnRead() const {return m_bGPIBUseSerialPollOnRead;}
95  int gpibWaitBeforeWrite() const {return m_gpibWaitBeforeWrite;}
96  int gpibWaitBeforeRead() const {return m_gpibWaitBeforeRead;}
97  int gpibWaitBeforeSPoll() const {return m_gpibWaitBeforeSPoll;}
98  unsigned char gpibMAVbit() const {return m_gpibMAVbit;}
99 
100  //! These properties should be set before open().
101  void setSerialBaudRate(unsigned int rate) {m_serialBaudRate = rate;}
102  void setSerialStopBits(unsigned int bits) {m_serialStopBits = bits;}
103  enum {PARITY_NONE = 0, PARITY_ODD = 1, PARITY_EVEN = 2};
104  void setSerialParity(unsigned int parity) {m_serialParity = parity;}
105  void setSerial7Bits(bool enable) {m_serial7Bits = enable;}
106  void setSerialFlushBeforeWrite(bool x) {m_serialFlushBeforeWrite = x;}
107  void setSerialEOS(const char *str) {m_serialEOS = str;} //!< be overridden by \a setEOS().
108  void setSerialHasEchoBack(bool x) {m_serialHasEchoBack = x;}
109 
110  unsigned int serialBaudRate() const {return m_serialBaudRate;}
111  unsigned int serialStopBits() const {return m_serialStopBits;}
112  unsigned int serialParity() const {return m_serialParity;}
113  bool serial7Bits() const {return m_serial7Bits;}
114  bool serialFlushBeforeWrite() const {return m_serialFlushBeforeWrite;}
115  const XString &serialEOS() const {return m_serialEOS;}
116  bool serialHasEchoBack() const {return m_serialHasEchoBack;}
117 
118  virtual void send(const XString &str) throw (XCommError &);
119  virtual void send(const char *str) throw (XCommError &);
120  virtual void write(const char *sendbuf, int size) throw (XCommError &);
121  virtual void receive() throw (XCommError &);
122  virtual void receive(unsigned int length) throw (XCommError &);
123  virtual bool isOpened() const {return !!m_xport;}
124 protected:
125  virtual void open() throw (XInterfaceError &);
126  //! This can be called even if has already closed.
127  virtual void close() throw (XInterfaceError &);
128 
129  shared_ptr<XPort> openedPort() const {return m_xport;}
130 private:
131  XString m_serialEOS;
132  bool m_bGPIBUseSerialPollOnWrite;
133  bool m_bGPIBUseSerialPollOnRead;
134  int m_gpibWaitBeforeWrite;
135  int m_gpibWaitBeforeRead;
136  int m_gpibWaitBeforeSPoll;
137  unsigned char m_gpibMAVbit; //! don't check if zero
138 
139  unsigned int m_serialBaudRate;
140  unsigned int m_serialStopBits;
141  unsigned int m_serialParity;
142  bool m_serial7Bits;
143  bool m_serialFlushBeforeWrite;
144  bool m_serialHasEchoBack;
145 
146  shared_ptr<XPort> m_xport;
147 
148  //! for scripting
149  shared_ptr<XStringNode> m_script_send;
150  shared_ptr<XStringNode> m_script_query;
151  shared_ptr<XListener> m_lsnOnSendRequested;
152  shared_ptr<XListener> m_lsnOnQueryRequested;
153  void onSendRequested(const Snapshot &shot, XValueNodeBase *);
154  void onQueryRequested(const Snapshot &shot, XValueNodeBase *);
155 };
156 
157 class XPort {
158 public:
159  XPort(XCharInterface *interface) {m_portString = ***interface->port();}
160  virtual ~XPort() = default;
161  virtual void open(const XCharInterface *pInterface) throw (XInterface::XCommError &) = 0;
162  virtual void send(const char *str) throw (XInterface::XCommError &) = 0;
163  virtual void write(const char *sendbuf, int size) throw (XInterface::XCommError &) = 0;
164  virtual void receive() throw (XInterface::XCommError &) = 0;
165  virtual void receive(unsigned int length) throw (XInterface::XCommError &) = 0;
166  //! Thread-Local-Storage Buffer.
167  //! \sa XThreadLocal
168  std::vector<char>& buffer() {return XCharInterface::buffer_receive();}
169  const XString &portString() const {return m_portString;}
170  const XString &eos() const {return m_eos;}
171  void setEOS(const char *str) {m_eos = str;}
172 protected:
173 private:
174  XString m_portString, m_eos;
175 };
176 
177 #endif /*CHARINTERFACE_H_*/

Generated for KAME4 by  doxygen 1.8.3