44 #ifndef COMMONCPP_SERIAL_H_
45 #define COMMONCPP_SERIAL_H_
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
51 #ifndef COMMONCPP_THREAD_H_
55 #ifndef COMMMONCPP_EXCEPTION_H_
110 typedef enum Error Error;
118 typedef enum Flow Flow;
125 typedef enum Parity Parity;
132 typedef enum Pending Pending;
149 void initSerial(
void);
160 void open(
const char *fname);
175 virtual int aRead(
char * Data,
const int Length);
183 virtual int aWrite(
const char * Data,
const int Length);
192 Error error(Error error,
char *errstr = NULL);
200 inline void error(
char *err) {
201 error(errExtended, err);
211 inline void setError(
bool enable) {
212 flags.thrown = !enable;
225 int setPacketInput(
int size, uint8_t btimer = 0);
236 int setLineInput(
char newline = 13,
char nl1 = 0);
246 void flushInput(
void);
251 void flushOutput(
void);
256 void waitOutput(
void);
262 void endSerial(
void);
269 void initConfig(
void);
285 Serial(
const char *name);
302 Serial &operator=(
const Serial &from);
310 Error setSpeed(
unsigned long speed);
318 Error setCharBits(
int bits);
326 Error setParity(Parity parity);
334 Error setStopBits(
int bits);
342 Error setFlowControl(Flow flow);
349 void toggleDTR(timeout_t millisec);
354 void sendBreak(
void);
362 inline Error getErrorNumber(
void)
const {
372 inline char *getErrorString(
void)
const {
383 inline int getBufferSize(
void)
const {
396 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
420 class __EXPORT TTYStream :
protected std::streambuf,
public Serial,
public std::iostream
425 friend TTYStream& crlf(TTYStream&);
426 friend TTYStream& lfcr(TTYStream&);
428 __DELETE_COPY(TTYStream);
450 void endStream(
void);
458 int underflow(
void) __OVERRIDE;
468 int uflow(
void) __OVERRIDE;
477 int overflow(
int ch) __OVERRIDE;
486 TTYStream(
const char *filename, timeout_t to = 0);
491 virtual ~TTYStream();
498 inline void setTimeout(timeout_t to) {
509 void interactive(
bool flag);
530 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF) __OVERRIDE;
542 class __EXPORT ttystream :
public TTYStream
545 __DELETE_COPY(ttystream);
560 ttystream(
const char *name);
567 void open(
const char *name);
577 inline bool operator!() {
592 class __EXPORT TTYSession :
public Thread,
public TTYStream
595 __DELETE_COPY(TTYSession);
605 TTYSession(
const char *name,
int pri = 0,
int stack = 0);
607 virtual ~TTYSession();
638 class __EXPORT SerialPort:
public Serial,
public TimerPort
641 SerialPort *next, *prev;
642 SerialService *service;
648 bool detect_disconnect;
650 friend class SerialService;
652 __DELETE_COPY(SerialPort);
661 SerialPort(SerialService *svc,
const char *name);
667 virtual ~SerialPort();
673 void setDetectPending(
bool );
678 inline bool getDetectPending(
void )
const {
679 return detect_pending;
686 void setDetectOutput(
bool );
691 inline bool getDetectOutput(
void )
const {
692 return detect_output;
699 virtual void expired(
void);
706 virtual void pending(
void);
712 virtual void disconnect(
void);
723 inline int output(
void *buf,
int len) {
724 return aWrite((
char *)buf, len);
730 virtual void output(
void);
741 inline int input(
void *buf,
int len) {
742 return aRead((
char *)buf, len);
753 void setTimer(timeout_t timeout = 0);
760 void incTimer(timeout_t timeout);
785 class __EXPORT SerialService :
public Thread,
private Mutex
792 SerialPort *first, *last;
794 __DELETE_COPY(SerialService);
801 void attach(SerialPort *port);
808 void detach(SerialPort *port);
813 void run(
void) __OVERRIDE;
815 friend class SerialPort;
824 virtual void onUpdate(uint8_t flag);
830 virtual void onEvent(
void);
838 virtual void onCallback(SerialPort *port);
850 void update(uint8_t flag = 0xff);
860 SerialService(
int pri = 0,
size_t stack = 0,
const char *
id = NULL);
865 virtual ~SerialService();
873 inline int getCount(
void)
const {
880 #ifdef CCXX_EXCEPTIONS
881 class __EXPORT SerException :
public IOException
884 SerException(
const String &str) : IOException(str) {}