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