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

Contents of /trunk/H7 Server/Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20