PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/OpenNETSerial/Port.cs

https://bitbucket.org/x893/sirflive
C# | 813 lines | 763 code | 50 blank | 0 comment | 107 complexity | 6c2d47912e3f005b9d39326645ca22b4 MD5 | raw file
  1. namespace OpenNETCF.IO.Serial
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. public class Port : IDisposable
  10. {
  11. private int brk;
  12. public readonly CommCapabilities Capabilities;
  13. private IntPtr closeEvent;
  14. private string closeEventName;
  15. private DCB dcb;
  16. private int dtr;
  17. private bool dtravail;
  18. private Thread eventThread;
  19. private IntPtr hPort;
  20. private int inputLength;
  21. private bool isOpen;
  22. private CommAPI m_CommAPI;
  23. private string portName;
  24. private DetailedPortSettings portSettings;
  25. private int ptxBuffer;
  26. private int rthreshold;
  27. private int rts;
  28. private bool rtsavail;
  29. private Mutex rxBufferBusy;
  30. private int rxBufferSize;
  31. private Queue rxFIFO;
  32. private IntPtr rxOverlapped;
  33. private int setir;
  34. private int sthreshold;
  35. private ManualResetEvent threadStarted;
  36. private byte[] txBuffer;
  37. private int txBufferSize;
  38. private IntPtr txOverlapped;
  39. public event CommChangeEvent CTSChange;
  40. public event CommEvent DataReceived;
  41. public event CommChangeEvent DSRChange;
  42. public event CommEvent FlagCharReceived;
  43. public event CommEvent HighWater;
  44. public event CommErrorEvent OnError;
  45. public event CommEvent PowerEvent;
  46. public event CommChangeEvent RingChange;
  47. public event CommChangeEvent RLSDChange;
  48. public event CommEvent TxDone;
  49. public Port(string PortName)
  50. {
  51. this.hPort = (IntPtr) (-1);
  52. this.rxBufferSize = 0x400000;
  53. this.rthreshold = 1;
  54. this.txBufferSize = 0x400000;
  55. this.sthreshold = 1;
  56. this.rxBufferBusy = new Mutex();
  57. this.dcb = new DCB();
  58. this.threadStarted = new ManualResetEvent(false);
  59. this.closeEventName = "CloseEvent";
  60. this.txOverlapped = IntPtr.Zero;
  61. this.rxOverlapped = IntPtr.Zero;
  62. this.Capabilities = new CommCapabilities();
  63. this.PortName = PortName;
  64. this.Init();
  65. }
  66. public Port(string PortName, BasicPortSettings InitialSettings)
  67. {
  68. this.hPort = (IntPtr) (-1);
  69. this.rxBufferSize = 0x400000;
  70. this.rthreshold = 1;
  71. this.txBufferSize = 0x400000;
  72. this.sthreshold = 1;
  73. this.rxBufferBusy = new Mutex();
  74. this.dcb = new DCB();
  75. this.threadStarted = new ManualResetEvent(false);
  76. this.closeEventName = "CloseEvent";
  77. this.txOverlapped = IntPtr.Zero;
  78. this.rxOverlapped = IntPtr.Zero;
  79. this.Capabilities = new CommCapabilities();
  80. this.PortName = PortName;
  81. this.Init();
  82. this.portSettings.BasicSettings = InitialSettings;
  83. }
  84. public Port(string PortName, DetailedPortSettings InitialSettings)
  85. {
  86. this.hPort = (IntPtr) (-1);
  87. this.rxBufferSize = 0x400000;
  88. this.rthreshold = 1;
  89. this.txBufferSize = 0x400000;
  90. this.sthreshold = 1;
  91. this.rxBufferBusy = new Mutex();
  92. this.dcb = new DCB();
  93. this.threadStarted = new ManualResetEvent(false);
  94. this.closeEventName = "CloseEvent";
  95. this.txOverlapped = IntPtr.Zero;
  96. this.rxOverlapped = IntPtr.Zero;
  97. this.Capabilities = new CommCapabilities();
  98. this.PortName = PortName;
  99. this.Init();
  100. this.portSettings = InitialSettings;
  101. }
  102. public Port(string PortName, int RxBufferSize, int TxBufferSize)
  103. {
  104. this.hPort = (IntPtr) (-1);
  105. this.rxBufferSize = 0x400000;
  106. this.rthreshold = 1;
  107. this.txBufferSize = 0x400000;
  108. this.sthreshold = 1;
  109. this.rxBufferBusy = new Mutex();
  110. this.dcb = new DCB();
  111. this.threadStarted = new ManualResetEvent(false);
  112. this.closeEventName = "CloseEvent";
  113. this.txOverlapped = IntPtr.Zero;
  114. this.rxOverlapped = IntPtr.Zero;
  115. this.Capabilities = new CommCapabilities();
  116. this.rxBufferSize = RxBufferSize;
  117. this.txBufferSize = TxBufferSize;
  118. this.PortName = PortName;
  119. this.Init();
  120. }
  121. public Port(string PortName, BasicPortSettings InitialSettings, int RxBufferSize, int TxBufferSize)
  122. {
  123. this.hPort = (IntPtr) (-1);
  124. this.rxBufferSize = 0x400000;
  125. this.rthreshold = 1;
  126. this.txBufferSize = 0x400000;
  127. this.sthreshold = 1;
  128. this.rxBufferBusy = new Mutex();
  129. this.dcb = new DCB();
  130. this.threadStarted = new ManualResetEvent(false);
  131. this.closeEventName = "CloseEvent";
  132. this.txOverlapped = IntPtr.Zero;
  133. this.rxOverlapped = IntPtr.Zero;
  134. this.Capabilities = new CommCapabilities();
  135. this.rxBufferSize = RxBufferSize;
  136. this.txBufferSize = TxBufferSize;
  137. this.PortName = PortName;
  138. this.Init();
  139. this.portSettings.BasicSettings = InitialSettings;
  140. }
  141. public Port(string PortName, DetailedPortSettings InitialSettings, int RxBufferSize, int TxBufferSize)
  142. {
  143. this.hPort = (IntPtr) (-1);
  144. this.rxBufferSize = 0x400000;
  145. this.rthreshold = 1;
  146. this.txBufferSize = 0x400000;
  147. this.sthreshold = 1;
  148. this.rxBufferBusy = new Mutex();
  149. this.dcb = new DCB();
  150. this.threadStarted = new ManualResetEvent(false);
  151. this.closeEventName = "CloseEvent";
  152. this.txOverlapped = IntPtr.Zero;
  153. this.rxOverlapped = IntPtr.Zero;
  154. this.Capabilities = new CommCapabilities();
  155. this.rxBufferSize = RxBufferSize;
  156. this.txBufferSize = TxBufferSize;
  157. this.PortName = PortName;
  158. this.Init();
  159. this.portSettings = InitialSettings;
  160. }
  161. public bool Close()
  162. {
  163. CommErrorFlags flags;
  164. if (this.txOverlapped != IntPtr.Zero)
  165. {
  166. LocalFree(this.txOverlapped);
  167. this.txOverlapped = IntPtr.Zero;
  168. }
  169. if (this.rxOverlapped != IntPtr.Zero)
  170. {
  171. LocalFree(this.rxOverlapped);
  172. this.rxOverlapped = IntPtr.Zero;
  173. }
  174. if (this.hPort == IntPtr.Zero)
  175. {
  176. return false;
  177. }
  178. int num = 0;
  179. Label_0068:
  180. flags = 0;
  181. CommStat stat = new CommStat();
  182. if (!this.m_CommAPI.ClearCommError(this.hPort, ref flags, stat) || (stat.cbOutQue != 0))
  183. {
  184. Thread.Sleep(1);
  185. if (num < 3)
  186. {
  187. num++;
  188. Thread.Sleep(10);
  189. goto Label_0068;
  190. }
  191. }
  192. this.m_CommAPI.PurgeComm(this.hPort, 0);
  193. this.m_CommAPI.CloseHandle(this.hPort);
  194. this.m_CommAPI.SetEvent(this.closeEvent);
  195. this.isOpen = false;
  196. this.hPort = (IntPtr) (-1);
  197. return true;
  198. }
  199. private void CommEventThread()
  200. {
  201. byte[] buffer = new byte[this.rxBufferSize];
  202. int cbRead = 0;
  203. new AutoResetEvent(false);
  204. if (CommAPI.FullFramework)
  205. {
  206. this.m_CommAPI.SetCommMask(this.hPort, CommEventFlags.ALLPC);
  207. }
  208. else
  209. {
  210. this.m_CommAPI.SetCommMask(this.hPort, CommEventFlags.ALLCE);
  211. }
  212. this.threadStarted.Set();
  213. StringBuilder builder = new StringBuilder("", 80);
  214. while (this.hPort != ((IntPtr) (-1)))
  215. {
  216. try
  217. {
  218. CommErrorFlags flags = 0;
  219. CommStat stat = new CommStat();
  220. if (!this.m_CommAPI.ClearCommError(this.hPort, ref flags, stat))
  221. {
  222. Thread.Sleep(20);
  223. }
  224. else if (stat.cbInQue == 0)
  225. {
  226. Thread.Sleep(20);
  227. }
  228. else
  229. {
  230. builder.Append("UART Error: ");
  231. if ((flags & CommErrorFlags.FRAME) != 0)
  232. {
  233. builder = builder.Append("Framing,");
  234. }
  235. if ((flags & CommErrorFlags.IOE) != 0)
  236. {
  237. builder = builder.Append("IO,");
  238. }
  239. if ((flags & CommErrorFlags.OVERRUN) != 0)
  240. {
  241. builder = builder.Append("Overrun,");
  242. }
  243. if ((flags & CommErrorFlags.RXOVER) != 0)
  244. {
  245. builder = builder.Append("Receive Overflow,");
  246. }
  247. if ((flags & CommErrorFlags.RXPARITY) != 0)
  248. {
  249. builder = builder.Append("Parity,");
  250. }
  251. if ((flags & CommErrorFlags.TXFULL) != 0)
  252. {
  253. builder = builder.Append("Transmit Overflow,");
  254. }
  255. if ((flags & CommErrorFlags.BREAK) != 0)
  256. {
  257. builder = builder.Append("Break,");
  258. }
  259. if (builder.Length == 12)
  260. {
  261. builder = builder.Append("Unknown");
  262. }
  263. if ((this.OnError != null) && (flags != 0))
  264. {
  265. this.OnError(builder.ToString());
  266. }
  267. if (stat.cbInQue >= this.rxBufferSize)
  268. {
  269. this.m_CommAPI.ReadFile(this.hPort, buffer, this.rxBufferSize, ref cbRead, IntPtr.Zero);
  270. }
  271. else
  272. {
  273. this.m_CommAPI.ReadFile(this.hPort, buffer, (int) stat.cbInQue, ref cbRead, IntPtr.Zero);
  274. }
  275. if (cbRead >= 1)
  276. {
  277. this.rxBufferBusy.WaitOne();
  278. for (int i = 0; i < cbRead; i++)
  279. {
  280. this.rxFIFO.Enqueue(buffer[i]);
  281. }
  282. int count = this.rxFIFO.Count;
  283. this.rxBufferBusy.ReleaseMutex();
  284. if (((this.DataReceived != null) && (this.rthreshold != 0)) && (count >= this.rthreshold))
  285. {
  286. this.DataReceived();
  287. }
  288. }
  289. builder.Remove(0, builder.Length);
  290. Thread.Sleep(1);
  291. }
  292. continue;
  293. }
  294. catch (Exception exception)
  295. {
  296. if (this.rxOverlapped != IntPtr.Zero)
  297. {
  298. LocalFree(this.rxOverlapped);
  299. }
  300. if (this.OnError != null)
  301. {
  302. this.OnError(exception.Message);
  303. }
  304. continue;
  305. }
  306. }
  307. }
  308. public void Dispose()
  309. {
  310. if (this.isOpen)
  311. {
  312. this.Close();
  313. }
  314. }
  315. ~Port()
  316. {
  317. if (this.isOpen)
  318. {
  319. this.Close();
  320. }
  321. }
  322. private bool GetPortProperties()
  323. {
  324. return this.m_CommAPI.GetCommProperties(this.hPort, this.Capabilities);
  325. }
  326. private void Init()
  327. {
  328. if (Environment.OSVersion.Platform != PlatformID.WinCE)
  329. {
  330. this.m_CommAPI = new WinCommAPI();
  331. }
  332. else
  333. {
  334. this.m_CommAPI = new CECommAPI();
  335. }
  336. this.closeEvent = this.m_CommAPI.CreateEvent(true, false, this.closeEventName);
  337. this.rxFIFO = new Queue(this.rxBufferSize);
  338. this.txBuffer = new byte[this.txBufferSize];
  339. this.portSettings = new DetailedPortSettings();
  340. }
  341. [DllImport("kernel32", SetLastError=true)]
  342. internal static extern IntPtr LocalAlloc(int uFlags, int uBytes);
  343. [DllImport("kernel32", SetLastError=true)]
  344. internal static extern IntPtr LocalFree(IntPtr hMem);
  345. public bool Open()
  346. {
  347. if (this.isOpen)
  348. {
  349. return false;
  350. }
  351. bool fullFramework = CommAPI.FullFramework;
  352. string fileName = string.Format(@"\\.\{0}", this.portName);
  353. this.hPort = this.m_CommAPI.CreateFile(fileName);
  354. if (this.hPort == ((IntPtr) (-1)))
  355. {
  356. int num = Marshal.GetLastWin32Error();
  357. throw new CommPortException(string.Format("Error open port: {0}", (APIErrors) num));
  358. }
  359. this.m_CommAPI.PurgeComm(this.hPort, 0);
  360. this.m_CommAPI.GetCommState(this.hPort, this.dcb);
  361. this.dcb.BaudRate = (uint) this.portSettings.BasicSettings.BaudRate;
  362. this.dcb.ByteSize = this.portSettings.BasicSettings.ByteSize;
  363. this.dcb.fOutxCtsFlow = this.portSettings.OutCTS;
  364. this.dcb.fParity = true;
  365. this.dcb.fBinary = true;
  366. this.dcb.fRtsControl = (byte) this.portSettings.RTSControl;
  367. this.dcb.Parity = (byte) this.portSettings.BasicSettings.Parity;
  368. this.dcb.StopBits = (byte) this.portSettings.BasicSettings.StopBits;
  369. this.brk = 0;
  370. this.dtr = (this.dcb.fDtrControl == 1) ? 1 : 0;
  371. this.rts = (this.dcb.fRtsControl == 1) ? 1 : 0;
  372. CommTimeouts timeouts = new CommTimeouts();
  373. timeouts.ReadIntervalTimeout = uint.MaxValue;
  374. timeouts.ReadTotalTimeoutMultiplier = 0;
  375. timeouts.ReadTotalTimeoutConstant = 0x3e8;
  376. timeouts.WriteTotalTimeoutConstant = 0;
  377. timeouts.WriteTotalTimeoutMultiplier = 0x4b00 / this.dcb.BaudRate;
  378. this.m_CommAPI.SetCommState(this.hPort, this.dcb);
  379. this.m_CommAPI.SetCommTimeouts(this.hPort, timeouts);
  380. this.m_CommAPI.SetupComm(this.hPort, 0x1000, 0x1000);
  381. this.m_CommAPI.SetupComm(this.hPort, this.rxBufferSize, this.txBufferSize);
  382. this.GetPortProperties();
  383. Thread.Sleep(100);
  384. this.isOpen = true;
  385. this.eventThread = new Thread(new ThreadStart(this.CommEventThread));
  386. this.eventThread.IsBackground = true;
  387. this.eventThread.Priority = ThreadPriority.Highest;
  388. this.eventThread.Start();
  389. this.threadStarted.WaitOne();
  390. return true;
  391. }
  392. public bool Query()
  393. {
  394. if (this.isOpen)
  395. {
  396. return false;
  397. }
  398. this.hPort = this.m_CommAPI.QueryFile(this.portName);
  399. if (this.hPort == ((IntPtr) (-1)))
  400. {
  401. int num = Marshal.GetLastWin32Error();
  402. if (num != 5)
  403. {
  404. throw new CommPortException(string.Format("CreateFile Failed: {0}", num));
  405. }
  406. return false;
  407. }
  408. this.GetPortProperties();
  409. return true;
  410. }
  411. public bool ToggleRTS()
  412. {
  413. this.m_CommAPI.PurgeComm(this.hPort, 0);
  414. bool commState = this.m_CommAPI.GetCommState(this.hPort, this.dcb);
  415. byte fRtsControl = this.dcb.fRtsControl;
  416. byte num2 = (fRtsControl == 1) ? ((byte) 0) : ((byte) 1);
  417. this.dcb.fRtsControl = num2;
  418. commState = this.m_CommAPI.SetCommState(this.hPort, this.dcb);
  419. Thread.Sleep(10);
  420. this.dcb.fRtsControl = fRtsControl;
  421. commState = this.m_CommAPI.SetCommState(this.hPort, this.dcb);
  422. this.m_CommAPI.PurgeComm(this.hPort, 0);
  423. return commState;
  424. }
  425. public bool UpdateBaud(uint baud)
  426. {
  427. this.m_CommAPI.PurgeComm(this.hPort, 0);
  428. bool commState = this.m_CommAPI.GetCommState(this.hPort, this.dcb);
  429. this.dcb.BaudRate = baud;
  430. commState = this.m_CommAPI.SetCommState(this.hPort, this.dcb);
  431. this.m_CommAPI.PurgeComm(this.hPort, 0);
  432. this.rxBufferBusy.WaitOne();
  433. this.rxFIFO.Clear();
  434. this.rxBufferBusy.ReleaseMutex();
  435. return commState;
  436. }
  437. public void UpdatePortSettings(DetailedPortSettings mySettings)
  438. {
  439. this.portSettings = mySettings;
  440. }
  441. public bool UpdateSettings()
  442. {
  443. if (!this.isOpen)
  444. {
  445. return false;
  446. }
  447. this.m_CommAPI.PurgeComm(this.hPort, 0);
  448. bool commState = this.m_CommAPI.GetCommState(this.hPort, this.dcb);
  449. this.dcb.BaudRate = (uint) this.portSettings.BasicSettings.BaudRate;
  450. this.dcb.ByteSize = this.portSettings.BasicSettings.ByteSize;
  451. this.dcb.fBinary = true;
  452. this.dcb.fOutxCtsFlow = this.portSettings.OutCTS;
  453. this.dcb.fParity = true;
  454. this.dcb.fRtsControl = (byte) this.portSettings.RTSControl;
  455. this.dcb.Parity = (byte) this.portSettings.BasicSettings.Parity;
  456. this.dcb.StopBits = (byte) this.portSettings.BasicSettings.StopBits;
  457. commState = this.m_CommAPI.SetCommState(this.hPort, this.dcb);
  458. this.m_CommAPI.PurgeComm(this.hPort, 0);
  459. this.rxBufferBusy.WaitOne();
  460. this.rxFIFO.Clear();
  461. this.rxBufferBusy.ReleaseMutex();
  462. return commState;
  463. }
  464. public bool Break
  465. {
  466. get
  467. {
  468. if (!this.isOpen)
  469. {
  470. return false;
  471. }
  472. return (this.brk == 1);
  473. }
  474. set
  475. {
  476. if ((this.isOpen && (this.brk >= 0)) && (this.hPort != ((IntPtr) (-1))))
  477. {
  478. if (value)
  479. {
  480. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.SETBREAK))
  481. {
  482. throw new CommPortException("Failed to set break!");
  483. }
  484. this.brk = 1;
  485. }
  486. else
  487. {
  488. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.CLRBREAK))
  489. {
  490. throw new CommPortException("Failed to clear break!");
  491. }
  492. this.brk = 0;
  493. }
  494. }
  495. }
  496. }
  497. public DetailedPortSettings DetailedSettings
  498. {
  499. get
  500. {
  501. return this.portSettings;
  502. }
  503. set
  504. {
  505. this.portSettings = value;
  506. this.UpdateSettings();
  507. }
  508. }
  509. public bool DTRAvailable
  510. {
  511. get
  512. {
  513. return this.dtravail;
  514. }
  515. }
  516. public bool DTREnable
  517. {
  518. get
  519. {
  520. return (this.dtr == 1);
  521. }
  522. set
  523. {
  524. if ((this.dtr >= 0) && (this.hPort != ((IntPtr) (-1))))
  525. {
  526. if (value)
  527. {
  528. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.SETDTR))
  529. {
  530. throw new CommPortException("Failed to set DTR!");
  531. }
  532. this.dtr = 1;
  533. }
  534. else
  535. {
  536. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.CLRDTR))
  537. {
  538. throw new CommPortException("Failed to clear DTR!");
  539. }
  540. this.dtr = 0;
  541. }
  542. }
  543. }
  544. }
  545. public int InBufferCount
  546. {
  547. get
  548. {
  549. if (!this.isOpen)
  550. {
  551. return 0;
  552. }
  553. return this.rxFIFO.Count;
  554. }
  555. }
  556. public byte[] Input
  557. {
  558. get
  559. {
  560. if (!this.isOpen)
  561. {
  562. return null;
  563. }
  564. int count = 0;
  565. this.rxBufferBusy.WaitOne();
  566. if (this.inputLength == 0)
  567. {
  568. count = this.rxFIFO.Count;
  569. }
  570. else
  571. {
  572. count = (this.inputLength < this.rxFIFO.Count) ? this.inputLength : this.rxFIFO.Count;
  573. }
  574. byte[] buffer = new byte[count];
  575. for (int i = 0; i < count; i++)
  576. {
  577. buffer[i] = (byte) this.rxFIFO.Dequeue();
  578. }
  579. this.rxBufferBusy.ReleaseMutex();
  580. return buffer;
  581. }
  582. }
  583. public int InputLen
  584. {
  585. get
  586. {
  587. return this.inputLength;
  588. }
  589. set
  590. {
  591. this.inputLength = value;
  592. }
  593. }
  594. public bool IREnable
  595. {
  596. get
  597. {
  598. return (this.setir == 1);
  599. }
  600. set
  601. {
  602. if ((this.setir >= 0) && (this.hPort != ((IntPtr) (-1))))
  603. {
  604. if (value)
  605. {
  606. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.SETIR))
  607. {
  608. throw new CommPortException("Failed to set IR!");
  609. }
  610. this.setir = 1;
  611. }
  612. else
  613. {
  614. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.CLRIR))
  615. {
  616. throw new CommPortException("Failed to clear IR!");
  617. }
  618. this.setir = 0;
  619. }
  620. }
  621. }
  622. }
  623. public bool IsOpen
  624. {
  625. get
  626. {
  627. return this.isOpen;
  628. }
  629. }
  630. public int OutBufferCount
  631. {
  632. get
  633. {
  634. if (!this.isOpen)
  635. {
  636. return 0;
  637. }
  638. return this.ptxBuffer;
  639. }
  640. }
  641. public byte[] Output
  642. {
  643. set
  644. {
  645. if (!this.isOpen)
  646. {
  647. throw new CommPortException("Port not open");
  648. }
  649. int cbWritten = 0;
  650. if (value.GetLength(0) > this.sthreshold)
  651. {
  652. if (this.ptxBuffer > 0)
  653. {
  654. this.m_CommAPI.WriteFile(this.hPort, this.txBuffer, this.ptxBuffer, ref cbWritten, this.txOverlapped);
  655. this.ptxBuffer = 0;
  656. }
  657. this.m_CommAPI.WriteFile(this.hPort, value, value.GetLength(0), ref cbWritten, this.txOverlapped);
  658. }
  659. else
  660. {
  661. value.CopyTo(this.txBuffer, this.ptxBuffer);
  662. this.ptxBuffer += value.Length;
  663. if (this.ptxBuffer >= this.sthreshold)
  664. {
  665. this.m_CommAPI.WriteFile(this.hPort, this.txBuffer, this.ptxBuffer, ref cbWritten, this.txOverlapped);
  666. this.ptxBuffer = 0;
  667. }
  668. }
  669. }
  670. }
  671. public string PortName
  672. {
  673. get
  674. {
  675. return this.portName;
  676. }
  677. set
  678. {
  679. if (!CommAPI.FullFramework && !value.EndsWith(":"))
  680. {
  681. this.portName = value + ":";
  682. }
  683. else
  684. {
  685. this.portName = value;
  686. }
  687. }
  688. }
  689. public int RThreshold
  690. {
  691. get
  692. {
  693. return this.rthreshold;
  694. }
  695. set
  696. {
  697. this.rthreshold = value;
  698. }
  699. }
  700. public bool RTSAvailable
  701. {
  702. get
  703. {
  704. return this.rtsavail;
  705. }
  706. }
  707. public bool RTSEnable
  708. {
  709. get
  710. {
  711. return (this.rts == 1);
  712. }
  713. set
  714. {
  715. if ((this.rts >= 0) && (this.hPort != ((IntPtr) (-1))))
  716. {
  717. if (value)
  718. {
  719. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.SETRTS))
  720. {
  721. throw new CommPortException("Failed to set RTS!");
  722. }
  723. this.rts = 1;
  724. }
  725. else
  726. {
  727. if (!this.m_CommAPI.EscapeCommFunction(this.hPort, CommEscapes.CLRRTS))
  728. {
  729. throw new CommPortException("Failed to clear RTS!");
  730. }
  731. this.rts = 0;
  732. }
  733. }
  734. }
  735. }
  736. public BasicPortSettings Settings
  737. {
  738. get
  739. {
  740. return this.portSettings.BasicSettings;
  741. }
  742. set
  743. {
  744. this.portSettings.BasicSettings = value;
  745. this.UpdateSettings();
  746. }
  747. }
  748. public int SThreshold
  749. {
  750. get
  751. {
  752. return this.sthreshold;
  753. }
  754. set
  755. {
  756. this.sthreshold = value;
  757. }
  758. }
  759. public delegate void CommChangeEvent(bool NewState);
  760. public delegate void CommErrorEvent(string Description);
  761. public delegate void CommEvent();
  762. }
  763. }