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