1 #include <msp/core/systemerror.h>
2 #include <msp/strings/format.h>
3 #include "handle_private.h"
5 #include "serial_private.h"
12 void Serial::platform_init(const string &port)
14 string name = "\\\\.\\"+port;
16 *handle = CreateFile(name.c_str(), GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, nullptr);
18 throw system_error(format("CreateFile(%s)", port));
19 mode = M_READ|M_WRITE;
21 COMMTIMEOUTS timeouts;
22 timeouts.ReadIntervalTimeout = MAXDWORD;
23 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
24 timeouts.ReadTotalTimeoutConstant = MAXDWORD-1;
25 timeouts.WriteTotalTimeoutMultiplier = 0;
26 timeouts.WriteTotalTimeoutConstant = 0;
27 SetCommTimeouts(*handle, &timeouts);
31 void Serial::DeviceState::get_from(const Handle &h)
33 GetCommState(*h, &state);
36 void Serial::DeviceState::apply_to(const Handle &h)
38 if(SetCommState(*h, &state)==0)
39 throw system_error("SetCommState");
42 void Serial::DeviceState::set_baud_rate(unsigned baud)
44 state.BaudRate = baud;
47 void Serial::DeviceState::set_data_bits(unsigned bits)
49 state.ByteSize = bits;
52 void Serial::DeviceState::set_parity(Serial::Parity par)
56 case Serial::NONE: state.Parity = NOPARITY; break;
57 case Serial::EVEN: state.Parity = EVENPARITY; break;
58 case Serial::ODD: state.Parity = ODDPARITY; break;
59 default: throw invalid_argument("Serial::set_parity");
63 void Serial::DeviceState::set_stop_bits(unsigned bits)
67 case 1: state.StopBits = ONESTOPBIT; break;
68 case 2: state.StopBits = TWOSTOPBITS; break;
69 default: throw invalid_argument("Serial::set_stop_bits");