1 |
/*
|
2 |
* Serial.h & Serial.cpp - (C) 2007 by Torben H. Nielsen
|
3 |
*
|
4 |
* A wrapper class for communicating with a serial port
|
5 |
*/
|
6 |
|
7 |
#pragma once
|
8 |
|
9 |
#include <vector>
|
10 |
typedef std::vector<unsigned char> UCVector;
|
11 |
|
12 |
#ifndef _WINDOWS //
|
13 |
inline void Sleep(int x) { usleep(x*1000);}
|
14 |
#endif
|
15 |
|
16 |
|
17 |
class CSerial
|
18 |
{
|
19 |
#ifdef _WINDOWS
|
20 |
HANDLE mComport;
|
21 |
DCB mDcbRestore;
|
22 |
#else
|
23 |
int mFiledescriptor;
|
24 |
#endif
|
25 |
|
26 |
char* mPortstr;
|
27 |
int mBitrate;
|
28 |
bool mIsopen;
|
29 |
|
30 |
//void printByte(char* description, unsigned char byte);
|
31 |
public:
|
32 |
CSerial();
|
33 |
CSerial(char* port, int bitrate);
|
34 |
~CSerial(void);
|
35 |
|
36 |
void close();
|
37 |
void open(char* port, int bitrate);
|
38 |
|
39 |
unsigned char readByte();
|
40 |
void writeByte(unsigned char out);
|
41 |
|
42 |
#ifdef _WINDOWS
|
43 |
COMSTAT getComstat() const;
|
44 |
#endif
|
45 |
int bytesReady() const;
|
46 |
int outQueueSize() const;
|
47 |
char* getPort() const {return mPortstr;}
|
48 |
int getBitrate() const {return mBitrate;}
|
49 |
|
50 |
bool isOpen() { return mIsopen; }
|
51 |
|
52 |
//void writeBytes(UCVector out);
|
53 |
//UCVector readBytes(int maxcount);
|
54 |
protected:
|
55 |
|
56 |
#ifdef _WINDOWS
|
57 |
void openWindows();
|
58 |
#else
|
59 |
void openLinux();
|
60 |
#endif
|
61 |
};
|