/OpenNETSerial/DCB.cs

https://bitbucket.org/x893/sirflive · C# · 253 lines · 247 code · 6 blank · 0 comment · 0 complexity · 7083e8a4e0ffac41ba567988651d64ea MD5 · raw file

  1. namespace OpenNETCF.IO.Serial
  2. {
  3. using System;
  4. using System.Collections.Specialized;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. [StructLayout(LayoutKind.Sequential)]
  8. internal class DCB
  9. {
  10. private uint DCBlength;
  11. public uint BaudRate;
  12. private BitVector32 Control;
  13. internal ushort wReserved;
  14. public ushort XonLim;
  15. public ushort XoffLim;
  16. public byte ByteSize;
  17. public byte Parity;
  18. public byte StopBits;
  19. public sbyte XonChar;
  20. public sbyte XoffChar;
  21. public sbyte ErrorChar;
  22. public sbyte EofChar;
  23. public sbyte EvtChar;
  24. internal ushort wReserved1;
  25. private readonly BitVector32.Section sect1;
  26. private readonly BitVector32.Section DTRsect;
  27. private readonly BitVector32.Section sect2;
  28. private readonly BitVector32.Section RTSsect;
  29. public DCB()
  30. {
  31. this.DCBlength = (uint) Marshal.SizeOf(this);
  32. this.Control = new BitVector32(0);
  33. this.sect1 = BitVector32.CreateSection(15);
  34. this.DTRsect = BitVector32.CreateSection(3, this.sect1);
  35. this.sect2 = BitVector32.CreateSection(0x3f, this.DTRsect);
  36. this.RTSsect = BitVector32.CreateSection(3, this.sect2);
  37. }
  38. internal void _SuppressCompilerWarnings()
  39. {
  40. this.wReserved = this.wReserved;
  41. this.wReserved1 = this.wReserved1;
  42. }
  43. public bool fBinary
  44. {
  45. get
  46. {
  47. return this.Control[1];
  48. }
  49. set
  50. {
  51. this.Control[1] = value;
  52. }
  53. }
  54. public bool fParity
  55. {
  56. get
  57. {
  58. return this.Control[2];
  59. }
  60. set
  61. {
  62. this.Control[2] = value;
  63. }
  64. }
  65. public bool fOutxCtsFlow
  66. {
  67. get
  68. {
  69. return this.Control[4];
  70. }
  71. set
  72. {
  73. this.Control[4] = value;
  74. }
  75. }
  76. public bool fOutxDsrFlow
  77. {
  78. get
  79. {
  80. return this.Control[8];
  81. }
  82. set
  83. {
  84. this.Control[8] = value;
  85. }
  86. }
  87. public byte fDtrControl
  88. {
  89. get
  90. {
  91. return (byte) this.Control[this.DTRsect];
  92. }
  93. set
  94. {
  95. this.Control[this.DTRsect] = value;
  96. }
  97. }
  98. public bool fDsrSensitivity
  99. {
  100. get
  101. {
  102. return this.Control[0x40];
  103. }
  104. set
  105. {
  106. this.Control[0x40] = value;
  107. }
  108. }
  109. public bool fTXContinueOnXoff
  110. {
  111. get
  112. {
  113. return this.Control[0x80];
  114. }
  115. set
  116. {
  117. this.Control[0x80] = value;
  118. }
  119. }
  120. public bool fOutX
  121. {
  122. get
  123. {
  124. return this.Control[0x100];
  125. }
  126. set
  127. {
  128. this.Control[0x100] = value;
  129. }
  130. }
  131. public bool fInX
  132. {
  133. get
  134. {
  135. return this.Control[0x200];
  136. }
  137. set
  138. {
  139. this.Control[0x200] = value;
  140. }
  141. }
  142. public bool fErrorChar
  143. {
  144. get
  145. {
  146. return this.Control[0x400];
  147. }
  148. set
  149. {
  150. this.Control[0x400] = value;
  151. }
  152. }
  153. public bool fNull
  154. {
  155. get
  156. {
  157. return this.Control[0x800];
  158. }
  159. set
  160. {
  161. this.Control[0x800] = value;
  162. }
  163. }
  164. public byte fRtsControl
  165. {
  166. get
  167. {
  168. return (byte) this.Control[this.RTSsect];
  169. }
  170. set
  171. {
  172. this.Control[this.RTSsect] = value;
  173. }
  174. }
  175. public bool fAbortOnError
  176. {
  177. get
  178. {
  179. return this.Control[0x4000];
  180. }
  181. set
  182. {
  183. this.Control[0x4000] = value;
  184. }
  185. }
  186. public override string ToString()
  187. {
  188. StringBuilder builder = new StringBuilder();
  189. builder.Append("DCB:\r\n");
  190. builder.AppendFormat(null, " BaudRate: {0}\r\n", new object[] { this.BaudRate });
  191. builder.AppendFormat(null, " Control: 0x{0:x}\r\n", new object[] { this.Control.Data });
  192. builder.AppendFormat(null, " fBinary: {0}\r\n", new object[] { this.fBinary });
  193. builder.AppendFormat(null, " fParity: {0}\r\n", new object[] { this.fParity });
  194. builder.AppendFormat(null, " fOutxCtsFlow: {0}\r\n", new object[] { this.fOutxCtsFlow });
  195. builder.AppendFormat(null, " fOutxDsrFlow: {0}\r\n", new object[] { this.fOutxDsrFlow });
  196. builder.AppendFormat(null, " fDtrControl: {0}\r\n", new object[] { this.fDtrControl });
  197. builder.AppendFormat(null, " fDsrSensitivity: {0}\r\n", new object[] { this.fDsrSensitivity });
  198. builder.AppendFormat(null, " fTXContinueOnXoff: {0}\r\n", new object[] { this.fTXContinueOnXoff });
  199. builder.AppendFormat(null, " fOutX: {0}\r\n", new object[] { this.fOutX });
  200. builder.AppendFormat(null, " fInX: {0}\r\n", new object[] { this.fInX });
  201. builder.AppendFormat(null, " fNull: {0}\r\n", new object[] { this.fNull });
  202. builder.AppendFormat(null, " fRtsControl: {0}\r\n", new object[] { this.fRtsControl });
  203. builder.AppendFormat(null, " fAbortOnError: {0}\r\n", new object[] { this.fAbortOnError });
  204. builder.AppendFormat(null, " XonLim: {0}\r\n", new object[] { this.XonLim });
  205. builder.AppendFormat(null, " XoffLim: {0}\r\n", new object[] { this.XoffLim });
  206. builder.AppendFormat(null, " ByteSize: {0}\r\n", new object[] { this.ByteSize });
  207. builder.AppendFormat(null, " Parity: {0}\r\n", new object[] { this.Parity });
  208. builder.AppendFormat(null, " StopBits: {0}\r\n", new object[] { this.StopBits });
  209. builder.AppendFormat(null, " XonChar: {0}\r\n", new object[] { this.XonChar });
  210. builder.AppendFormat(null, " XoffChar: {0}\r\n", new object[] { this.XoffChar });
  211. builder.AppendFormat(null, " ErrorChar: {0}\r\n", new object[] { this.ErrorChar });
  212. builder.AppendFormat(null, " EofChar: {0}\r\n", new object[] { this.EofChar });
  213. builder.AppendFormat(null, " EvtChar: {0}\r\n", new object[] { this.EvtChar });
  214. return builder.ToString();
  215. }
  216. [Flags]
  217. private enum ctrlBit
  218. {
  219. fAbortOnErrorMask = 0x4000,
  220. fBinaryMask = 1,
  221. fDsrSensitivityMask = 0x40,
  222. fDtrControlMask = 0x30,
  223. fErrorCharMask = 0x400,
  224. fInXMask = 0x200,
  225. fNullMask = 0x800,
  226. fOutxCtsFlowMask = 4,
  227. fOutxDsrFlowMask = 8,
  228. fOutXMask = 0x100,
  229. fParityMask = 2,
  230. fRtsControlMask = 0x3000,
  231. fTXContinueOnXoffMask = 0x80
  232. }
  233. public enum DtrControlFlags
  234. {
  235. Disable,
  236. Enable,
  237. Handshake
  238. }
  239. public enum RtsControlFlags
  240. {
  241. Disable,
  242. Enable,
  243. Handshake,
  244. Toggle
  245. }
  246. }
  247. }