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 |
#include <string>
|
11 |
#include <stdexcept>
|
12 |
typedef std::vector<unsigned char> UCVector;
|
13 |
|
14 |
#ifndef _MSC_VER //
|
15 |
inline void Sleep(int x) { usleep(x*1000);}
|
16 |
#include <termios.h>
|
17 |
#endif
|
18 |
|
19 |
|
20 |
class CSerial
|
21 |
{
|
22 |
#ifdef _MSC_VER
|
23 |
HANDLE mComport;
|
24 |
DCB mDcbRestore;
|
25 |
#else
|
26 |
int mFiledescriptor;
|
27 |
termios mOldtio;
|
28 |
#endif
|
29 |
|
30 |
char* mPortstr;
|
31 |
int mBitrate;
|
32 |
bool mIsopen;
|
33 |
|
34 |
public:
|
35 |
CSerial();
|
36 |
CSerial(char* port, int bitrate);
|
37 |
virtual ~CSerial(void);
|
38 |
|
39 |
void close();
|
40 |
void open(char* port, int bitrate);
|
41 |
|
42 |
int readByte();
|
43 |
void writeByte(unsigned char out);
|
44 |
|
45 |
#ifdef _MSC_VER
|
46 |
COMSTAT getComstat() const;
|
47 |
#endif
|
48 |
int bytesReady() const;
|
49 |
int outQueueSize() const;
|
50 |
char* getPort() const {return mPortstr;}
|
51 |
int getBitrate() const {return mBitrate;}
|
52 |
|
53 |
bool isOpen() { return mIsopen; }
|
54 |
|
55 |
//void writeBytes(UCVector out);
|
56 |
//UCVector readBytes(int maxcount);
|
57 |
protected:
|
58 |
|
59 |
void printByte(char* description, unsigned char byte);
|
60 |
#ifdef _MSC_VER
|
61 |
void openWindows();
|
62 |
#else
|
63 |
void openLinux();
|
64 |
#endif
|
65 |
};
|