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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (hide annotations) (download)
Wed Jun 4 13:06:09 2008 UTC (15 years, 11 months ago) by torben
File MIME type: text/plain
File size: 1742 byte(s)
Cleaned up the code a bit


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
13 torben 49 #ifndef _MSC_VER //
14 torben 44 inline void Sleep(int x) { usleep(x*1000);}
15 torben 46 #include <termios.h>
16 torben 44 #endif
17    
18    
19 torben 38 class CSerial
20     {
21 torben 54
22     public: //public enumerations
23     enum Baudrate
24     {
25     Baud300,
26     Baud600,
27     Baud1200,
28     Baud2400,
29     Baud4800,
30     Baud9600,
31     Baud19200,
32     Baud38400,
33     Baud57600,
34     Baud115200
35     };
36     private: // member vars
37 torben 49 #ifdef _MSC_VER
38 torben 38 HANDLE mComport;
39     DCB mDcbRestore;
40 torben 44 #else
41     int mFiledescriptor;
42 torben 46 termios mOldtio;
43 torben 44 #endif
44    
45     char* mPortstr;
46 torben 54 Baudrate mBitrate;
47 torben 38 bool mIsopen;
48    
49 torben 54 public: //public methods
50 torben 38 CSerial();
51 torben 54 CSerial(char* port, Baudrate bitrate);
52 torben 48 virtual ~CSerial(void);
53 torben 38
54     void close();
55 torben 54 void open(char* port, Baudrate bitrate);
56 torben 38
57 torben 47 int readByte();
58 torben 38 void writeByte(unsigned char out);
59    
60 torben 76 void writeBytes(unsigned char* buf, unsigned int len);
61     int readBytes(unsigned char* buf, unsigned int maxLen);
62    
63 torben 49 #ifdef _MSC_VER
64 torben 38 COMSTAT getComstat() const;
65 torben 44 #endif
66 torben 38 int bytesReady() const;
67     int outQueueSize() const;
68 torben 44 char* getPort() const {return mPortstr;}
69 torben 54 Baudrate getBitrate() const {return mBitrate;}
70 torben 38
71     bool isOpen() { return mIsopen; }
72    
73     protected:
74 torben 44
75 torben 46 void printByte(char* description, unsigned char byte);
76 torben 54 int convertBaudrate(Baudrate rate);
77 torben 76
78 torben 49 #ifdef _MSC_VER
79 torben 44 void openWindows();
80 torben 76 int readBytesWindows(unsigned char*buf, unsigned int maxLen);
81     void writeBytesWindows(unsigned char* buf, unsigned int len);
82 torben 44 #else
83     void openLinux();
84 torben 76 int readBytesLinux(unsigned char* buf, unsigned int maxLen);
85     void writeBytesLinux(unsigned char* buf, unsigned int len);
86 torben 44 #endif
87 torben 38 };

  ViewVC Help
Powered by ViewVC 1.1.20