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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20