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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (show annotations) (download)
Wed Jun 4 13:08:30 2008 UTC (15 years, 10 months ago) by torben
File MIME type: text/plain
File size: 1746 byte(s)
It's nicer to have the private data at the end

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

  ViewVC Help
Powered by ViewVC 1.1.20