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