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(char* 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 |
mComport = CreateFile( mPortstr,
|
61 |
GENERIC_READ|GENERIC_WRITE,
|
62 |
0,
|
63 |
0,
|
64 |
OPEN_EXISTING,
|
65 |
FILE_ATTRIBUTE_NORMAL,
|
66 |
0);
|
67 |
|
68 |
if (mComport == INVALID_HANDLE_VALUE)
|
69 |
{
|
70 |
throw std::exception(writeLastError().c_str());
|
71 |
}
|
72 |
|
73 |
DCB dcb;
|
74 |
dcb.DCBlength = sizeof(DCB);
|
75 |
mDcbRestore.DCBlength = sizeof(DCB);
|
76 |
|
77 |
if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore))
|
78 |
{
|
79 |
std::string error = writeLastError();
|
80 |
CloseHandle(mComport);
|
81 |
throw std::exception(error.c_str());
|
82 |
}
|
83 |
|
84 |
dcb.BaudRate = mBitrate;
|
85 |
dcb.ByteSize = 8;
|
86 |
dcb.Parity = NOPARITY;
|
87 |
dcb.StopBits = ONESTOPBIT;
|
88 |
dcb.fDtrControl = DTR_CONTROL_DISABLE;
|
89 |
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
90 |
dcb.fParity = false;
|
91 |
dcb.fDsrSensitivity = false;
|
92 |
|
93 |
if (!SetCommState(mComport,&dcb))
|
94 |
{
|
95 |
std::string error = writeLastError();
|
96 |
CloseHandle(mComport);
|
97 |
throw std::exception(error.c_str());
|
98 |
}
|
99 |
|
100 |
mIsopen = true;
|
101 |
}
|
102 |
|
103 |
void CSerial::close()
|
104 |
{
|
105 |
if (mIsopen)
|
106 |
{
|
107 |
while (getComstat().cbOutQue >0)
|
108 |
Sleep(5);
|
109 |
SetCommState(mComport,&mDcbRestore);
|
110 |
CloseHandle(mComport);
|
111 |
mIsopen = false;
|
112 |
}
|
113 |
}
|
114 |
|
115 |
|
116 |
unsigned char CSerial::readByte()
|
117 |
{
|
118 |
unsigned char out;
|
119 |
unsigned long size;
|
120 |
|
121 |
if (!mIsopen)
|
122 |
throw std::exception("Port not opened");
|
123 |
|
124 |
ReadFile(mComport,&out,1,&size,0);
|
125 |
if (size != 1)
|
126 |
{
|
127 |
std::string error = writeLastError();
|
128 |
CloseHandle(mComport);
|
129 |
throw std::exception(error.c_str());
|
130 |
}
|
131 |
|
132 |
|
133 |
//printByte("Read", out);
|
134 |
return out;
|
135 |
}
|
136 |
|
137 |
|
138 |
void CSerial::writeByte(unsigned char out)
|
139 |
{
|
140 |
unsigned long size;
|
141 |
|
142 |
//printByte("Write", out);
|
143 |
if (!mIsopen)
|
144 |
throw std::exception("Port not opened");
|
145 |
|
146 |
while (getComstat().cbOutQue >0)
|
147 |
Sleep(2);
|
148 |
|
149 |
WriteFile(mComport,&out,1,&size, NULL);
|
150 |
if (size ==0)
|
151 |
{
|
152 |
std::string error = writeLastError();
|
153 |
CloseHandle(mComport);
|
154 |
throw std::exception(error.c_str());
|
155 |
}
|
156 |
}
|
157 |
|
158 |
COMSTAT CSerial::getComstat() const
|
159 |
{
|
160 |
if (!mIsopen)
|
161 |
throw std::exception("Port not opened");
|
162 |
|
163 |
COMSTAT stat;
|
164 |
DWORD x;
|
165 |
ClearCommError(mComport,&x,&stat);
|
166 |
return stat;
|
167 |
}
|
168 |
|
169 |
int CSerial::bytesReady() const
|
170 |
{
|
171 |
return getComstat().cbInQue;
|
172 |
}
|
173 |
|
174 |
int CSerial::outQueueSize() const
|
175 |
{
|
176 |
return getComstat().cbOutQue;
|
177 |
}
|
178 |
|
179 |
|
180 |
void CSerial::printByte(char* description, unsigned char byte)
|
181 |
{
|
182 |
std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
|
183 |
std::cout << std::dec;
|
184 |
}
|
185 |
|
186 |
/*
|
187 |
void CSerial::writeBytes(UCVector out)
|
188 |
{
|
189 |
unsigned long bytesWritten;
|
190 |
unsigned int size = out.size();
|
191 |
unsigned char *buf = new unsigned char[size];
|
192 |
|
193 |
for (int i=0; i<size; i++)
|
194 |
buf[i] = out[i];
|
195 |
|
196 |
WriteFile(mComport,buf,size,&bytesWritten,NULL);
|
197 |
if (bytesWritten != size)
|
198 |
{
|
199 |
std::string error = writeLastError();
|
200 |
CloseHandle(mComport);
|
201 |
throw std::exception(error.c_str());
|
202 |
}
|
203 |
delete[] buf;
|
204 |
}
|
205 |
*/
|
206 |
|
207 |
|
208 |
/*
|
209 |
UCVector CSerial::readBytes(int maxcount)
|
210 |
{
|
211 |
UCVector buf;
|
212 |
|
213 |
|
214 |
|
215 |
return buf;
|
216 |
}
|
217 |
*/
|