/[H7]/trunk/H7 Server/Serial.cpp
ViewVC logotype

Contents of /trunk/H7 Server/Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (show annotations) (download)
Thu Feb 1 13:49:01 2007 UTC (17 years, 3 months ago) by hedin
File size: 3521 byte(s)
Found a "feature" more, about the COM port...
Made some comments in the code
1 #include "StdAfx.h"
2 #include <iostream>
3
4 #include "Serial.h"
5
6 #include <exception>
7 #include <string>
8 #include <sstream>
9 #include <iomanip>
10
11 std::string writeLastError()
12 {
13 LPVOID lpMsgBuf;
14 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
15 NULL,
16 GetLastError(),
17 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
18 (LPSTR) &lpMsgBuf,
19 0,
20 NULL);
21
22 std::ostringstream out;
23 out << "Error" << lpMsgBuf;
24 return out.str();
25 }
26
27
28 CSerial::CSerial()
29 {
30 mIsopen = false;
31 }
32
33 CSerial::CSerial(char* port, int bitrate)
34 {
35 mPortstr = port;
36 mBitrate = bitrate;
37 mIsopen = false;
38
39 open();
40 }
41
42 CSerial::~CSerial(void)
43 {
44 close();
45 }
46
47 void CSerial::open(CString port, int bitrate)
48 {
49 if (mIsopen)
50 throw std::exception("Port already opened");
51
52 mPortstr = port;
53 mBitrate = bitrate;
54
55 open();
56 }
57
58 void CSerial::open()
59 {
60 // Åbner Serial porten.
61 mComport = CreateFile( mPortstr,
62 GENERIC_READ|GENERIC_WRITE,
63 0,
64 0,
65 OPEN_EXISTING,
66 FILE_ATTRIBUTE_NORMAL,
67 0);
68
69 // Error check.
70 if (mComport == INVALID_HANDLE_VALUE)
71 {
72 throw std::exception(writeLastError().c_str());
73 }
74
75 DCB dcb;
76 dcb.DCBlength = sizeof(DCB);
77 mDcbRestore.DCBlength = sizeof(DCB);
78
79 // Error check.
80 if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore))
81 {
82 std::string error = writeLastError();
83 CloseHandle(mComport);
84 throw std::exception(error.c_str());
85 }
86
87 // Definerer Serial Conf.
88 dcb.BaudRate = mBitrate;
89 dcb.ByteSize = 8;
90 dcb.Parity = NOPARITY;
91 dcb.StopBits = ONESTOPBIT;
92 dcb.fDtrControl = DTR_CONTROL_DISABLE;
93 dcb.fRtsControl = RTS_CONTROL_DISABLE;
94 dcb.fParity = false;
95 dcb.fDsrSensitivity = false;
96
97 // Error check.
98 if (!SetCommState(mComport,&dcb))
99 {
100 std::string error = writeLastError();
101 CloseHandle(mComport);
102 throw std::exception(error.c_str());
103 }
104
105 mIsopen = true;
106 }
107
108 void CSerial::close()
109 {
110 if (mIsopen)
111 {
112 while (getComstat().cbOutQue >0)
113 Sleep(5);
114 SetCommState(mComport,&mDcbRestore);
115 CloseHandle(mComport);
116 mIsopen = false;
117 }
118 }
119
120
121 unsigned char CSerial::readByte()
122 {
123 unsigned char out;
124 unsigned long size;
125
126 if (!mIsopen)
127 throw std::exception("Port not opened");
128
129 ReadFile(mComport,&out,1,&size,0);
130 // Error check.
131 if (size != 1)
132 {
133 std::string error = writeLastError();
134 CloseHandle(mComport);
135 throw std::exception(error.c_str());
136 }
137
138
139 //printByte("Read", out);
140 return out;
141 }
142
143
144 void CSerial::writeByte(unsigned char out)
145 {
146 unsigned long size;
147
148 //printByte("Write", out);
149 if (!mIsopen)
150 throw std::exception("Port not opened");
151
152 while (getComstat().cbOutQue >0)
153 Sleep(2);
154
155 WriteFile(mComport,&out,1,&size, NULL);
156 if (size ==0)
157 {
158 std::string error = writeLastError();
159 CloseHandle(mComport);
160 throw std::exception(error.c_str());
161 }
162 }
163
164 COMSTAT CSerial::getComstat() const
165 {
166 if (!mIsopen)
167 throw std::exception("Port not opened");
168
169 COMSTAT stat;
170 DWORD x;
171 ClearCommError(mComport,&x,&stat);
172 return stat;
173 }
174
175 int CSerial::bytesReady() const
176 {
177 return getComstat().cbInQue;
178 }
179
180 int CSerial::outQueueSize() const
181 {
182 return getComstat().cbOutQue;
183 }
184
185
186 void CSerial::printByte(char* description, unsigned char byte)
187 {
188 std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
189 std::cout << std::dec;
190 }

  ViewVC Help
Powered by ViewVC 1.1.20