/[H7]/branches/linux-serial/Serial.h
ViewVC logotype

Contents of /branches/linux-serial/Serial.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations) (download)
Mon Feb 5 10:10:18 2007 UTC (17 years, 3 months ago) by torben
File MIME type: text/plain
File size: 1489 byte(s)
Added a cross-platform specification of desired baud-rate.

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
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;
40 DCB mDcbRestore;
41 #else
42 int mFiledescriptor;
43 termios mOldtio;
44 #endif
45
46 char* mPortstr;
47 Baudrate mBitrate;
48 bool mIsopen;
49
50 public: //public methods
51 CSerial();
52 CSerial(char* port, Baudrate bitrate);
53 virtual ~CSerial(void);
54
55 void close();
56 void open(char* port, Baudrate bitrate);
57
58 int readByte();
59 void writeByte(unsigned char out);
60
61 #ifdef _MSC_VER
62 COMSTAT getComstat() const;
63 #endif
64 int bytesReady() const;
65 int outQueueSize() const;
66 char* getPort() const {return mPortstr;}
67 Baudrate getBitrate() const {return mBitrate;}
68
69 bool isOpen() { return mIsopen; }
70
71 //void writeBytes(UCVector out);
72 //UCVector readBytes(int maxcount);
73 protected:
74
75 void printByte(char* description, unsigned char byte);
76 int convertBaudrate(Baudrate rate);
77 #ifdef _MSC_VER
78 void openWindows();
79 #else
80 void openLinux();
81 #endif
82 };

  ViewVC Help
Powered by ViewVC 1.1.20