Scope
SerialConnection.cpp
1 #include "stdafx.h"
2 #include "SerialConnection.h"
3 #include "helpers\helpers.h"
4 
5 namespace scope {
6 
7 SerialConnection::SerialConnection(const std::wstring& _comstring, const DWORD& _baud, const BYTE& _bytesize, const BYTE& _parity, const BYTE& _stopbits)
8  : status(0)
9  , initialized(false) {
10  hCom = CreateFile(_comstring.c_str(),
11  GENERIC_READ | GENERIC_WRITE,
12  0, // must be opened with exclusive-access
13  NULL, // default security attributes
14  OPEN_EXISTING, // must use OPEN_EXISTING
15  0, // not overlapped I/O
16  NULL ); // hTemplate must be NULL for comm devices
17 
18  if (hCom == INVALID_HANDLE_VALUE)
19  throw std::exception("Initializing serial connection failed");
20 
21  // Initialize the DCB structure.
22  DCB dcb;
23  SecureZeroMemory(&dcb, sizeof(DCB));
24  dcb.DCBlength = sizeof(DCB);
25 
26  // Build on the current configuration by first retrieving all current
27  // settings.
28  if ( !GetCommState(hCom, &dcb) )
29  throw std::exception("GetCommState of serial connection failed");
30 
31  // Fill in some DCB values and set the com state:
32  // 9600 bps, 8 data bits, no parity, and 1 stop bit.
33  dcb.BaudRate = _baud; // baud rate
34  dcb.ByteSize = _bytesize; // data size, xmit and rcv
35  dcb.Parity = _parity; // parity bit
36  dcb.StopBits = _stopbits; // stop bit
37 
38  if ( !SetCommState(hCom, &dcb) )
39  throw std::exception("SetCommState of serial connection failed");
40 
41  // Get configuration again
42  if ( !GetCommState(hCom, &dcb) )
43  throw std::exception("GetCommState of serial connection failed");
44 
45  DBOUT(L"SerialConnection::Initialize configured: " << dcb.BaudRate << L"baud " << dcb.ByteSize << L"byte size" << dcb.Parity << L"parity " << dcb.StopBits << L"stop bits");
46 }
47 
49  CloseHandle(hCom);
50 }
51 
52 void SerialConnection::Send(const std::string& _string) {
53  //CW2A stra(_string);
54  DWORD written = 0;
55  if ( !WriteFile(hCom, _string.data(), static_cast<DWORD>(_string.length()), &written, NULL) )
56  throw std::exception("SerialConnection::Send failed");
57 }
58 
59 std::vector<BYTE> SerialConnection::Receive(const uint32_t& _toread, const uint32_t& _waitfor) {
60  DWORD errors = 0;
61  COMSTAT comstat;
62  if ( !ClearCommError(hCom, &errors, &comstat) )
63  throw std::exception("SerialConnection::Receive getting buffer info failed");
64 
65  if ( comstat.cbInQue < _toread ) {
66  std::this_thread::sleep_for(std::chrono::milliseconds(_waitfor));
67  if ( !ClearCommError(hCom, &errors, &comstat) )
68  throw std::exception("SerialConnection::Receive getting buffer info failed");
69  }
70 
71  if ( comstat.cbInQue < _toread )
72  throw std::exception("SerialConnection::Receive not all requested bytes in buffer");
73 
74  DWORD readbytes = 0;
75  std::vector<BYTE> read(_toread, 0);
76 
77  if ( !ReadFile(hCom, (void*)read.data(), comstat.cbInQue, &readbytes, NULL) )
78  throw std::exception("SerialConnection::Receive read from buffer failed");
79 
80  return read;
81 }
82 
83 }
void Send(const std::string &_string)
send command string over connection
HANDLE hCom
the connection handle
std::vector< BYTE > Receive(const uint32_t &_toread, const uint32_t &_waitfor=100)
Receive bytes from connection, does only wait 100ms for the bytes to be ready, otherwise throws...
This is the include file for standard system include files, or project specific include files that ar...
SerialConnection(const std::wstring &_comstring, const DWORD &_baud=CBR_9600, const BYTE &_bytesize=8, const BYTE &_parity=NOPARITY, const BYTE &_stopbits=ONESTOPBIT)
Initializes connection.
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
~SerialConnection(void)
Closes connection.
Various helper functions and classes for Scope.