7 |
#pragma once |
#pragma once |
8 |
|
|
9 |
#include <vector> |
#include <vector> |
10 |
|
#include <string> |
11 |
|
#include <stdexcept> |
12 |
typedef std::vector<unsigned char> UCVector; |
typedef std::vector<unsigned char> UCVector; |
13 |
|
|
14 |
#ifndef _WINDOWS // |
#ifndef _MSC_VER // |
15 |
inline void Sleep(int x) { usleep(x*1000);} |
inline void Sleep(int x) { usleep(x*1000);} |
16 |
#include <termios.h> |
#include <termios.h> |
17 |
#endif |
#endif |
19 |
|
|
20 |
class CSerial |
class CSerial |
21 |
{ |
{ |
22 |
#ifdef _WINDOWS |
|
23 |
|
public: //public enumerations |
24 |
|
enum Baudrate |
25 |
|
{ |
26 |
|
Baud300, |
27 |
|
Baud600, |
28 |
|
Baud1200, |
29 |
|
Baud2400, |
30 |
|
Baud4800, |
31 |
|
Baud9600, |
32 |
|
Baud19200, |
33 |
|
Baud38400, |
34 |
|
Baud57600, |
35 |
|
Baud115200 |
36 |
|
}; |
37 |
|
private: // member vars |
38 |
|
#ifdef _MSC_VER |
39 |
HANDLE mComport; |
HANDLE mComport; |
40 |
DCB mDcbRestore; |
DCB mDcbRestore; |
41 |
#else |
#else |
44 |
#endif |
#endif |
45 |
|
|
46 |
char* mPortstr; |
char* mPortstr; |
47 |
int mBitrate; |
Baudrate mBitrate; |
48 |
bool mIsopen; |
bool mIsopen; |
49 |
|
|
50 |
public: |
public: //public methods |
51 |
CSerial(); |
CSerial(); |
52 |
CSerial(char* port, int bitrate); |
CSerial(char* port, Baudrate bitrate); |
53 |
~CSerial(void); |
virtual ~CSerial(void); |
54 |
|
|
55 |
void close(); |
void close(); |
56 |
void open(char* port, int bitrate); |
void open(char* port, Baudrate bitrate); |
57 |
|
|
58 |
unsigned char readByte(); |
int readByte(); |
59 |
void writeByte(unsigned char out); |
void writeByte(unsigned char out); |
60 |
|
|
61 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
62 |
COMSTAT getComstat() const; |
COMSTAT getComstat() const; |
63 |
#endif |
#endif |
64 |
int bytesReady() const; |
int bytesReady() const; |
65 |
int outQueueSize() const; |
int outQueueSize() const; |
66 |
char* getPort() const {return mPortstr;} |
char* getPort() const {return mPortstr;} |
67 |
int getBitrate() const {return mBitrate;} |
Baudrate getBitrate() const {return mBitrate;} |
68 |
|
|
69 |
bool isOpen() { return mIsopen; } |
bool isOpen() { return mIsopen; } |
70 |
|
|
73 |
protected: |
protected: |
74 |
|
|
75 |
void printByte(char* description, unsigned char byte); |
void printByte(char* description, unsigned char byte); |
76 |
#ifdef _WINDOWS |
int convertBaudrate(Baudrate rate); |
77 |
|
#ifdef _MSC_VER |
78 |
void openWindows(); |
void openWindows(); |
79 |
#else |
#else |
80 |
void openLinux(); |
void openLinux(); |