]> git.tdb.fi Git - libs/core.git/blob - source/io/windows/serial.cpp
Deal with the activity being recreated during process lifetime
[libs/core.git] / source / io / windows / serial.cpp
1 #include <msp/core/systemerror.h>
2 #include <msp/strings/format.h>
3 #include "handle_private.h"
4 #include "serial.h"
5 #include "serial_private.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace IO {
11
12 void Serial::platform_init(const string &port)
13 {
14         string name = "\\\\.\\"+port;
15
16         *handle = CreateFile(name.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
17         if(!handle)
18                 throw system_error(format("CreateFile(%s)", port));
19         mode = M_READ|M_WRITE;
20
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);
28 }
29
30
31 void Serial::DeviceState::get_from(const Handle &h)
32 {
33         GetCommState(*h, &state);
34 }
35
36 void Serial::DeviceState::apply_to(const Handle &h)
37 {
38         if(SetCommState(*h, &state)==0)
39                 throw system_error("SetCommState");
40 }
41
42 void Serial::DeviceState::set_baud_rate(unsigned baud)
43 {
44         state.BaudRate = baud;
45 }
46
47 void Serial::DeviceState::set_data_bits(unsigned bits)
48 {
49         state.ByteSize = bits;
50 }
51
52 void Serial::DeviceState::set_parity(Serial::Parity par)
53 {
54         switch(par)
55         {
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("set_parity");
60         }
61 }
62
63 void Serial::DeviceState::set_stop_bits(unsigned bits)
64 {
65         switch(bits)
66         {
67         case 1: state.StopBits = ONESTOPBIT; break;
68         case 2: state.StopBits = TWOSTOPBITS; break;
69         default: throw invalid_argument("set_stop_bits");
70         }
71 }
72
73 } // namespace IO
74 } // namespace MSp