1 | //============================================================================ |
---|
2 | // Name : LxSerial.cpp |
---|
3 | // Author : Eelko van Breda,www.dbl.tudelft.nl |
---|
4 | // Version : 0.1 |
---|
5 | // Copyright : Copyright (c) 2008 LGPL |
---|
6 | // Description : serial communicatin class linux |
---|
7 | //============================================================================ |
---|
8 | |
---|
9 | #ifndef LXSERIAL_H_ |
---|
10 | #define LXSERIAL_H_ |
---|
11 | //#define __DBG__ |
---|
12 | |
---|
13 | #include <fcntl.h> /* fileio */ |
---|
14 | #include <termios.h> /* terminal i/o system, talks to /dev/tty* ports */ |
---|
15 | #include <unistd.h> /* Read function */ |
---|
16 | #include <sys/ioctl.h> /* ioctl function */ |
---|
17 | #include <iostream> |
---|
18 | #include <assert.h> |
---|
19 | #include <stdint.h> |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | #define INVALID_DEVICE_HANDLE -1 |
---|
23 | |
---|
24 | class LxSerial |
---|
25 | { |
---|
26 | public: |
---|
27 | enum PortType { RS232, // Normal RS232 |
---|
28 | RS485_EXAR, // EXAR XR16C2850 |
---|
29 | RS485_FTDI, // FTDI FT232RL in 485 mode |
---|
30 | RS485_SMSC, // SMSC SCH311X RS-485 mode (Versalogic Sidewinder board) |
---|
31 | TCP |
---|
32 | }; |
---|
33 | enum PortSpeed { S50 = B50, // Baudrate to use for the port --> see termios.h |
---|
34 | S75 = B75, |
---|
35 | S110 = B110, |
---|
36 | S134 = B134, |
---|
37 | S150 = B150, |
---|
38 | S200 = B200, |
---|
39 | S300 = B300, |
---|
40 | S600 = B600, |
---|
41 | S1200 = B1200, |
---|
42 | S1800 = B1800, |
---|
43 | S2400 = B2400, |
---|
44 | S4800 = B4800, |
---|
45 | S9600 = B9600, |
---|
46 | S19200 = B19200, |
---|
47 | S38400 = B38400, |
---|
48 | S57600 = B57600, |
---|
49 | S115200 = B115200, |
---|
50 | S230400 = B230400, |
---|
51 | S460800 = B460800, |
---|
52 | S500000 = B500000, |
---|
53 | S576000 = B576000, |
---|
54 | S921600 = B921600, |
---|
55 | S1000000 = B1000000, |
---|
56 | S1152000 = B1152000, |
---|
57 | S1500000 = B1500000, |
---|
58 | S2000000 = B2000000, |
---|
59 | S2500000 = B2500000, |
---|
60 | S3000000 = B3000000, |
---|
61 | S3500000 = B3500000, |
---|
62 | S4000000 = B4000000 |
---|
63 | }; |
---|
64 | /*return values*/ |
---|
65 | static const int READ_ERROR = -1; |
---|
66 | static const int COLLISION_DETECT_ERROR = -2; |
---|
67 | static const int ECHO_TIMEOUT_ERROR = -3; |
---|
68 | |
---|
69 | /*magic numbers*/ |
---|
70 | static const int COLLISION_WAIT_TIME_USEC = 10000; // microseconds |
---|
71 | static const int ECHO_WAIT_TIME_SEC = 1; // seconds |
---|
72 | static const int ECHO_WAIT_TIME_USEC = 0; // microseconds |
---|
73 | static const int WAIT_FOR_DATA_DSEC = 5; // |
---|
74 | |
---|
75 | protected: |
---|
76 | int hPort; // file handle to the port |
---|
77 | std::string s_port_name; // name of the port that was opened |
---|
78 | // bool b_initialised; // |
---|
79 | bool b_clear_echo; // read sended characters from Rx when true |
---|
80 | bool b_rts; // this boolean must be set to enforce setting the RTS signal without hardware contol enabled |
---|
81 | bool b_hw_flow_control; // |
---|
82 | bool b_socket; |
---|
83 | termios options, old_options; // |
---|
84 | bool wait_for_input(int *seconds, int *microseconds); // private member function to wait for port. the time variables are modified after return to reflect the time not slept |
---|
85 | void set_port_type(LxSerial::PortType port_type); |
---|
86 | |
---|
87 | public: |
---|
88 | LxSerial(); |
---|
89 | virtual ~LxSerial(); |
---|
90 | virtual bool port_open(const std::string& portname, LxSerial::PortType port_type); // open serial port. If overridden, make sure you set s_port_name!! |
---|
91 | virtual bool is_port_open(); |
---|
92 | std::string& get_port_name(); |
---|
93 | virtual bool set_speed(LxSerial::PortSpeed baudrate ); // enumerated |
---|
94 | virtual bool set_speed_int(const int baudrate); // Set speed by integer value directly - UNPROTECTED! |
---|
95 | void set_clear_echo(bool clear); // clear echoed charackters from input and detect collisions on write |
---|
96 | virtual bool port_close(); |
---|
97 | virtual int port_read(unsigned char* buffer, int numBytes) const; |
---|
98 | virtual int port_read(unsigned char* buffer, int numBytes, int seconds, int microseconds); |
---|
99 | virtual int port_write(unsigned char* buffer, int numBytes); |
---|
100 | virtual void flush_buffer(); // flush input and output buffers |
---|
101 | |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | #endif /*LXSERIAL_H_*/ |
---|