/[H9]/trunk/FlisServer/Serial.cpp
ViewVC logotype

Annotation of /trunk/FlisServer/Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 296 - (hide annotations) (download)
Fri Dec 14 09:50:41 2007 UTC (16 years, 5 months ago) by torben
File size: 3614 byte(s)
Enabled the use of com10+
1 kevin 70 #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 torben 296 CString comport = "\\\\.\\";
62     comport += mPortstr;
63 kevin 70 // Åbner Serial porten.
64 torben 296 mComport = CreateFile( comport,
65 kevin 70 GENERIC_READ|GENERIC_WRITE,
66     0,
67     0,
68     OPEN_EXISTING,
69     FILE_ATTRIBUTE_NORMAL,
70     0);
71    
72     // Error check.
73     if (mComport == INVALID_HANDLE_VALUE)
74     {
75     throw std::exception(writeLastError().c_str());
76     }
77    
78     DCB dcb;
79     dcb.DCBlength = sizeof(DCB);
80     mDcbRestore.DCBlength = sizeof(DCB);
81    
82     // Error check.
83     if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore))
84     {
85     std::string error = writeLastError();
86     CloseHandle(mComport);
87     throw std::exception(error.c_str());
88     }
89    
90     // Definerer Serial Conf.
91     dcb.BaudRate = mBitrate;
92     dcb.ByteSize = 8;
93     dcb.Parity = NOPARITY;
94     dcb.StopBits = ONESTOPBIT;
95     dcb.fDtrControl = DTR_CONTROL_DISABLE;
96     dcb.fRtsControl = RTS_CONTROL_TOGGLE;
97     dcb.fParity = false;
98     dcb.fDsrSensitivity = false;
99    
100     // Error check.
101     if (!SetCommState(mComport,&dcb))
102     {
103     std::string error = writeLastError();
104     CloseHandle(mComport);
105     throw std::exception(error.c_str());
106     }
107    
108     mIsopen = true;
109     }
110    
111     void CSerial::close()
112     {
113     if (mIsopen)
114     {
115     while (getComstat().cbOutQue >0)
116 kevin 113 {
117 kevin 70 Sleep(5);
118 kevin 113 }
119 kevin 70 SetCommState(mComport,&mDcbRestore);
120     CloseHandle(mComport);
121     mIsopen = false;
122     }
123     }
124    
125    
126     unsigned char CSerial::readByte()
127     {
128     unsigned char out;
129     unsigned long size;
130    
131     if (!mIsopen)
132 kevin 113 {
133 kevin 70 throw std::exception("Port not opened");
134 kevin 113 }
135 kevin 70
136     ReadFile(mComport,&out,1,&size,0);
137     // Error check.
138     if (size != 1)
139     {
140     std::string error = writeLastError();
141     CloseHandle(mComport);
142     throw std::exception(error.c_str());
143     }
144 kevin 113
145 kevin 70
146     //printByte("Read", out);
147     return out;
148     }
149    
150    
151     void CSerial::writeByte(unsigned char out)
152     {
153     unsigned long size;
154    
155     //printByte("Write", out);
156     if (!mIsopen)
157     throw std::exception("Port not opened");
158    
159     while (getComstat().cbOutQue >0)
160     Sleep(2);
161    
162     WriteFile(mComport,&out,1,&size, NULL);
163     if (size ==0)
164     {
165     std::string error = writeLastError();
166     CloseHandle(mComport);
167     throw std::exception(error.c_str());
168     }
169     }
170    
171     COMSTAT CSerial::getComstat() const
172     {
173     if (!mIsopen)
174     throw std::exception("Port not opened");
175    
176     COMSTAT stat;
177     DWORD x;
178     ClearCommError(mComport,&x,&stat);
179     return stat;
180     }
181    
182     int CSerial::bytesReady() const
183     {
184     return getComstat().cbInQue;
185     }
186    
187     int CSerial::outQueueSize() const
188     {
189     return getComstat().cbOutQue;
190     }
191    
192    
193     void CSerial::printByte(char* description, unsigned char byte)
194     {
195     std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
196     std::cout << std::dec;
197     }

  ViewVC Help
Powered by ViewVC 1.1.20