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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations) (download)
Sun Feb 4 21:22:44 2007 UTC (17 years, 3 months ago) by torben
File size: 4527 byte(s)
Clean the MFC heavy code, so it at least will compile with G++


1 #ifdef _WINDOWS
2 #include "StdAfx.h"
3 #else //linux
4 #include <errno.h>
5 #endif
6
7 #include "Serial.h"
8
9 #include <stdexcept>
10 #include <string>
11 #include <sstream>
12 #include <iomanip>
13
14 std::string writeLastError()
15 {
16 #ifdef _WINDOWS
17 LPVOID lpMsgBuf;
18 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
19 NULL,
20 GetLastError(),
21 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
22 (LPSTR) &lpMsgBuf,
23 0,
24 NULL);
25
26 std::ostringstream out;
27 out << "Error" << lpMsgBuf;
28 return out.str();
29 #else //linux
30 char message[256];
31 strerror_r(errno, message, 255);
32 return std::string(message);
33 #endif
34 }
35
36
37 CSerial::CSerial()
38 {
39 mIsopen = false;
40 }
41
42 CSerial::CSerial(char* port, int bitrate)
43 {
44 mPortstr = port;
45 mBitrate = bitrate;
46 mIsopen = false;
47
48 #ifdef _WINDOWS
49 openWindows();
50 #else
51 openLinux();
52 #endif
53 }
54
55 CSerial::~CSerial(void)
56 {
57 close();
58 }
59
60 void CSerial::open(char* port, int bitrate)
61 {
62 if (mIsopen)
63 throw std::runtime_error("Port already opened");
64
65 mPortstr = port;
66 mBitrate = bitrate;
67
68 #ifdef _WINDOWS
69 openWindows();
70 #else
71 openLinux();
72 #endif
73 }
74
75 #ifdef _WINDOWS
76 void CSerial::openWindows()
77 {
78 mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
79
80 if (mComport == INVALID_HANDLE_VALUE)
81 {
82 throw std::runtime_error(writeLastError().c_str());
83 }
84
85 DCB dcb;
86 dcb.DCBlength = sizeof(DCB);
87 mDcbRestore.DCBlength = sizeof(DCB);
88
89 if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore))
90 {
91 std::string error = writeLastError();
92 CloseHandle(mComport);
93 throw std::exception(error.c_str());
94 }
95
96 dcb.BaudRate = mBitrate;
97 dcb.ByteSize = 8;
98 dcb.Parity = NOPARITY;
99 dcb.StopBits = ONESTOPBIT;
100 dcb.fDtrControl = DTR_CONTROL_DISABLE;
101 dcb.fRtsControl = RTS_CONTROL_DISABLE;
102 dcb.fParity = false;
103 dcb.fDsrSensitivity = false;
104
105 if (!SetCommState(mComport,&dcb))
106 {
107 std::string error = writeLastError();
108 CloseHandle(mComport);
109 throw std::exception(error.c_str());
110 }
111
112 mIsopen = true;
113 }
114 #endif
115
116
117 #ifndef _WINDOWS
118 void CSerial::openLinux()
119 {
120 }
121 #endif
122
123 void CSerial::close()
124 {
125 if (mIsopen)
126 {
127 #ifdef _WINDOWS
128 while (getComstat().cbOutQue >0)
129 Sleep(5);
130 SetCommState(mComport,&mDcbRestore);
131 CloseHandle(mComport);
132 #else // linux close()
133 #endif
134 mIsopen = false;
135 }
136 }
137
138
139 unsigned char CSerial::readByte()
140 {
141 unsigned char out;
142 unsigned long size;
143
144 if (!mIsopen)
145 throw std::runtime_error("Port not opened");
146 #ifdef _WINDOWS
147 ReadFile(mComport,&out,1,&size,0);
148 if (size != 1)
149 {
150 std::string error = writeLastError();
151 CloseHandle(mComport);
152 throw std::exception(error.c_str());
153 }
154 #else //linux readByte()
155 #endif
156
157 //printByte("Read", out);
158 return out;
159 }
160
161
162 void CSerial::writeByte(unsigned char out)
163 {
164 unsigned long size;
165
166 //printByte("Write", out);
167 if (!mIsopen)
168 throw std::runtime_error("Port not opened");
169
170 #ifdef _WINDOWS
171 while (getComstat().cbOutQue >0)
172 Sleep(2);
173
174 WriteFile(mComport,&out,1,&size, NULL);
175 if (size ==0)
176 {
177 std::string error = writeLastError();
178 CloseHandle(mComport);
179 throw std::exception(error.c_str());
180 }
181 #else //linux writeByte()
182 #endif
183 }
184 #ifdef _WINDOWS
185 COMSTAT CSerial::getComstat() const
186 {
187 if (!mIsopen)
188 throw std::exception("Port not opened");
189
190 COMSTAT stat;
191 DWORD x;
192 ClearCommError(mComport,&x,&stat);
193 return stat;
194 }
195 #endif
196
197 int CSerial::bytesReady() const
198 {
199 #ifdef _WINDOWS
200 return getComstat().cbInQue;
201 #else
202 return 0;
203 #endif
204 }
205
206 int CSerial::outQueueSize() const
207 {
208 #ifdef _WINDOWS
209 return getComstat().cbOutQue;
210 #else
211 return 0;
212 #endif
213 }
214
215 /* Debug function
216 void CSerial::printByte(char* description, unsigned char byte)
217 {
218 std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
219 std::cout << std::dec;
220 }
221 */
222
223 /*
224 void CSerial::writeBytes(UCVector out)
225 {
226 unsigned long bytesWritten;
227 unsigned int size = out.size();
228 unsigned char *buf = new unsigned char[size];
229
230 for (int i=0; i<size; i++)
231 buf[i] = out[i];
232
233 WriteFile(mComport,buf,size,&bytesWritten,NULL);
234 if (bytesWritten != size)
235 {
236 std::string error = writeLastError();
237 CloseHandle(mComport);
238 throw std::exception(error.c_str());
239 }
240 delete[] buf;
241 }
242 */
243
244
245 /*
246 UCVector CSerial::readBytes(int maxcount)
247 {
248 UCVector buf;
249
250
251
252 return buf;
253 }
254 */

  ViewVC Help
Powered by ViewVC 1.1.20