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_TOGGLE;
|
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 |
{
|
115 |
Sleep(5);
|
116 |
}
|
117 |
SetCommState(mComport,&mDcbRestore);
|
118 |
CloseHandle(mComport);
|
119 |
mIsopen = false;
|
120 |
}
|
121 |
}
|
122 |
|
123 |
|
124 |
unsigned char CSerial::readByte()
|
125 |
{
|
126 |
unsigned char out;
|
127 |
unsigned long size;
|
128 |
|
129 |
if (!mIsopen)
|
130 |
{
|
131 |
throw std::exception("Port not opened");
|
132 |
}
|
133 |
|
134 |
ReadFile(mComport,&out,1,&size,0);
|
135 |
// Error check.
|
136 |
if (size != 1)
|
137 |
{
|
138 |
std::string error = writeLastError();
|
139 |
CloseHandle(mComport);
|
140 |
throw std::exception(error.c_str());
|
141 |
}
|
142 |
|
143 |
|
144 |
//printByte("Read", out);
|
145 |
return out;
|
146 |
}
|
147 |
|
148 |
|
149 |
void CSerial::writeByte(unsigned char out)
|
150 |
{
|
151 |
unsigned long size;
|
152 |
|
153 |
//printByte("Write", out);
|
154 |
if (!mIsopen)
|
155 |
throw std::exception("Port not opened");
|
156 |
|
157 |
while (getComstat().cbOutQue >0)
|
158 |
Sleep(2);
|
159 |
|
160 |
WriteFile(mComport,&out,1,&size, NULL);
|
161 |
if (size ==0)
|
162 |
{
|
163 |
std::string error = writeLastError();
|
164 |
CloseHandle(mComport);
|
165 |
throw std::exception(error.c_str());
|
166 |
}
|
167 |
}
|
168 |
|
169 |
COMSTAT CSerial::getComstat() const
|
170 |
{
|
171 |
if (!mIsopen)
|
172 |
throw std::exception("Port not opened");
|
173 |
|
174 |
COMSTAT stat;
|
175 |
DWORD x;
|
176 |
ClearCommError(mComport,&x,&stat);
|
177 |
return stat;
|
178 |
}
|
179 |
|
180 |
int CSerial::bytesReady() const
|
181 |
{
|
182 |
return getComstat().cbInQue;
|
183 |
}
|
184 |
|
185 |
int CSerial::outQueueSize() const
|
186 |
{
|
187 |
return getComstat().cbOutQue;
|
188 |
}
|
189 |
|
190 |
|
191 |
void CSerial::printByte(char* description, unsigned char byte)
|
192 |
{
|
193 |
std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
|
194 |
std::cout << std::dec;
|
195 |
}
|