1 /// Definitions of using types
2 module serialport.types;
3 
4 ///
5 enum Parity
6 {
7     none, ///
8     odd,  ///
9     even  ///
10 }
11 
12 ///
13 enum DataBits : uint
14 {
15    data8 = 8, ///
16    data7 = 7, ///
17    data6 = 6, ///
18    data5 = 5, ///
19 }
20 
21 ///
22 enum StopBits
23 {
24    one, ///
25    onePointFive, ///
26    two ///
27 }
28 
29 package
30 {
31     version(Posix)
32     {
33         import core.sys.posix.unistd;
34         version(linux)
35             import core.sys.linux.termios;
36         else
37             import core.sys.posix.termios;
38 
39         import core.sys.posix.fcntl;
40         import core.sys.posix.sys.select;
41         import core.sys.posix.sys.ioctl;
42         import core.stdc.errno;
43         import std.algorithm;
44         import std.file;
45 
46         alias posixRead = core.sys.posix.unistd.read;
47         alias posixWrite = core.sys.posix.unistd.write;
48         alias posixClose = core.sys.posix.unistd.close;
49 
50         alias closeHandle = posixClose;
51         /// Posix handle is int
52         alias SPHandle = int;
53         enum initHandle = -1;
54     }
55     version(Windows)
56     {
57         import core.sys.windows.windows;
58         import std.bitmanip;
59 
60         enum NOPARITY    = 0x0;
61         enum EVENPARITY  = 0x2;
62         enum MARKPARITY  = 0x3;
63         enum ODDPARITY   = 0x1;
64         enum SPACEPARITY = 0x4;
65 
66         enum ONESTOPBIT   = 0x0;
67         enum ONE5STOPBITS = 0x1;
68         enum TWOSTOPBITS  = 0x2;
69 
70         struct DCB
71         {
72             DWORD DCBlength;
73             DWORD BaudRate;
74 
75             mixin(bitfields!(
76             DWORD, "fBinary",           1,
77             DWORD, "fParity",           1,
78             DWORD, "fOutxCtsFlow",      1,
79             DWORD, "fOutxDsrFlow",      1,
80             DWORD, "fDtrControl",       2,
81             DWORD, "fDsrSensitivity",   1,
82             DWORD, "fTXContinueOnXoff", 1,
83             DWORD, "fOutX",             1,
84             DWORD, "fInX",              1,
85             DWORD, "fErrorChar",        1,
86             DWORD, "fNull",             1,
87             DWORD, "fRtsControl",       2,
88             DWORD, "fAbortOnError",     1,
89             DWORD, "fDummy2",           17));
90 
91             WORD  wReserved;
92             WORD  XonLim;
93             WORD  XoffLim;
94             BYTE  ByteSize;
95             BYTE  Parity;
96             BYTE  StopBits;
97             ubyte  XonChar;
98             ubyte  XoffChar;
99             ubyte  ErrorChar;
100             ubyte  EofChar;
101             ubyte  EvtChar;
102             WORD  wReserved1;
103         }
104 
105         struct COMMTIMEOUTS
106         {
107             DWORD ReadIntervalTimeout;
108             DWORD ReadTotalTimeoutMultiplier;
109             DWORD ReadTotalTimeoutConstant;
110             DWORD WriteTotalTimeoutMultiplier;
111             DWORD WriteTotalTimeoutConstant;
112         }
113 
114         extern(Windows) @nogc
115         {
116             bool GetCommState(HANDLE hFile, DCB* lpDCB);
117             bool SetCommState(HANDLE hFile, DCB* lpDCB);
118             bool SetCommTimeouts(HANDLE hFile, COMMTIMEOUTS* lpCommTimeouts);
119         }
120 
121         alias closeHandle = CloseHandle;
122         /// Windows is HANDLE
123         alias SPHandle = HANDLE;
124         enum initHandle = null;
125     }
126 }
127 
128 ///
129 static immutable string modeSplitChar=":";
130 
131 version (Posix)
132 {
133     import serialport.util;
134 
135     version (OSX)
136     {
137         /+
138         38400 is max for OSX
139         http://www.manpages.info/macosx/cfsetospeed.3.html
140          +/
141         static immutable unixBaudList = pairList(
142             pair(    0, B0),
143             pair(   50, B50),
144             pair(   75, B75),
145             pair(  110, B110),
146             pair(  134, B134),
147             pair(  150, B150),   
148             pair(  200, B200),  
149             pair(  300, B300),
150             pair(  600, B600),
151             pair( 1200, B1200), 
152             pair( 1800, B1800), 
153             pair( 2400, B2400),
154             pair( 4800, B4800),
155             pair( 9600, B9600),
156             pair(19200, B19200),
157             pair(38400, B38400)
158         );
159     }
160     else
161     {
162         static immutable unixBaudList = pairList(
163             pair(     0, B0),
164             pair(    50, B50), 
165             pair(    75, B75),
166             pair(   110, B110),
167             pair(   134, B134),
168             pair(   150, B150),   
169             pair(   200, B200),  
170             pair(   300, B300),
171             pair(   600, B600),
172             pair(  1200, B1200), 
173             pair(  1800, B1800), 
174             pair(  2400, B2400),
175             pair(  4800, B4800),
176             pair(  9600, B9600),
177             pair( 19200, B19200),
178             pair( 38400, B38400),
179             pair( 57600, B57600),
180             pair(115200, B115200),
181             pair(230400, B230400) 
182         );
183     }
184 
185     static assert(unixBaudList.isUniqA, "not uniq A vals in unix baud list");
186     static assert(unixBaudList.isUniqB, "not uniq B vals in unix baud list");
187 }