You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1289 lines
36 KiB

1 year ago
  1. //=================================================================
  2. // SIOListener.cs
  3. //=================================================================
  4. // Copyright (C) 2005 Bob Tracy
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // You may contact the author via email at: k5kdn@arrl.net
  21. //=================================================================
  22. #define DBG_PRINT
  23. using System;
  24. using System.IO;
  25. using System.Text;
  26. using System.Collections;
  27. using System.Threading;
  28. using System.Windows.Forms; // needed for MessageBox (wjt)
  29. using System.Text.RegularExpressions;
  30. using System.Diagnostics;
  31. namespace PowerSDR
  32. {
  33. public class SIOListenerII
  34. {
  35. #region Constructor
  36. public SIOListenerII(Console c)
  37. {
  38. console = c;
  39. console.Activated += new EventHandler(console_Activated);
  40. console.Closing += new System.ComponentModel.CancelEventHandler(console_Closing);
  41. parser = new CATParser(console);
  42. //event handler for Serial RX Events
  43. SDRSerialPort.serial_rx_event += new SerialRXEventHandler(SerialRXEventHandler);
  44. if ( console.CATEnabled ) // if CAT is on, fire it up
  45. {
  46. try
  47. {
  48. enableCAT();
  49. }
  50. catch ( Exception ex )
  51. {
  52. // fixme??? how cool is to to pop a msg box from an exception handler in a constructor ??
  53. // seems ugly to me (wjt)
  54. console.CATEnabled = false;
  55. if ( console.setupForm != null )
  56. {
  57. console.setupForm.copyCATPropsToDialogVars(); // need to make sure the props on the setup page get reset
  58. }
  59. MessageBox.Show("Could not initialize CAT control. Exception was:\n\n " + ex.Message +
  60. "\n\nCAT control has been disabled.", "Error Initializing CAT control",
  61. MessageBoxButtons.OK, MessageBoxIcon.Error);
  62. }
  63. }
  64. // SIOMonitor = new System.Timers.Timer();
  65. // SIOMonitor.Elapsed+=new
  66. // System.Timers.ElapsedEventHandler(SIOMonitor_Elapsed);
  67. //
  68. // SIOMonitor.Interval = 60000;
  69. // SIOMonitor.Start();
  70. //
  71. // if(!File.Exists("SIOTestLog.txt"))
  72. // File.Create("SIOTestLog.txt");
  73. }
  74. // private void SIOMonitor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  75. // {
  76. // if(!console.MOX)
  77. // {
  78. // SIOMonitorCount++; // increments the counter when in receive
  79. // Debug.WriteLine("SIOMonitorCount: "+SIOMonitorCount.ToString());
  80. // if(SIOMonitorCount < 12) // if the counter is less than 12 (60 seconds),reinitialize the serial port
  81. // {
  82. // Debug.WriteLine("The SIO Timer has elapsed");
  83. // if(!SIO.PortIsOpen)
  84. // {
  85. // Debug.WriteLine("The port is closed");
  86. // SIO.OpenPort = true;
  87. // StreamWriter sw = new StreamWriter("SIOTestLog.txt", true);
  88. // sw.WriteLine("The serial port was forced open");
  89. // sw.WriteLine("Time: "+DateTime.Now.ToString());
  90. // sw.Flush();
  91. // sw.Close();
  92. // SIOMonitor.Enabled = true;
  93. // SIOMonitorCount = 0;
  94. // Fpass = true;
  95. // disableCAT();
  96. // enableCAT();
  97. // }
  98. // //Initialize();
  99. // }
  100. // else // consider the remote program on the serial port as being shut down
  101. // {
  102. // if(SIO.PortIsOpen)
  103. // {
  104. // port_status = "open";
  105. // }
  106. // else
  107. // {
  108. //
  109. // port_status = "closed";
  110. // }
  111. //
  112. //
  113. // SIOMonitorCount = 0;
  114. // disableCAT();
  115. // enableCAT();
  116. // // SIOMonitor.Stop();
  117. // }
  118. // }
  119. // else
  120. // SIOMonitorCount = 0;
  121. // }
  122. public void enableCAT()
  123. {
  124. lock ( this )
  125. {
  126. if ( cat_enabled ) return; // nothing to do already enabled
  127. cat_enabled = true;
  128. }
  129. Debug.WriteLine("==============CAT PORT OPEN");
  130. int port_num = console.CATPort;
  131. SIO = new SDRSerialPort(port_num);
  132. SIO.setCommParms(console.CATBaudRate,
  133. console.CATParity,
  134. console.CATDataBits,
  135. console.CATStopBits,
  136. console.CATHandshake);
  137. Initialize();
  138. }
  139. public bool UseForKeyPTT
  140. {
  141. set
  142. {
  143. if(SIO != null)
  144. SIO.UseForKeyPTT = value;
  145. }
  146. }
  147. public bool UseForPaddles
  148. {
  149. set
  150. {
  151. if (SIO != null)
  152. SIO.UseForPaddles = value;
  153. }
  154. }
  155. public bool PTTOnDTR
  156. {
  157. set
  158. {
  159. if (SIO != null)
  160. SIO.PTTOnDTR = value;
  161. }
  162. }
  163. public bool PTTOnRTS
  164. {
  165. set
  166. {
  167. if (SIO != null)
  168. SIO.PTTOnRTS = value;
  169. }
  170. }
  171. public bool KeyOnDTR
  172. {
  173. set
  174. {
  175. if (SIO != null)
  176. SIO.KeyOnDTR = value;
  177. }
  178. }
  179. public bool KeyOnRTS
  180. {
  181. set
  182. {
  183. if (SIO != null)
  184. SIO.KeyOnRTS = value;
  185. }
  186. }
  187. // typically called when the end user has disabled CAT control through a UI element ... this
  188. // closes the serial port and neutralized the listeners we have in place
  189. public void disableCAT()
  190. {
  191. lock ( this )
  192. {
  193. if ( !cat_enabled ) return; /* nothing to do already disabled */
  194. cat_enabled = false;
  195. }
  196. Debug.WriteLine("==============CAT PORT CLOSED");
  197. if ( SIO != null )
  198. {
  199. SIO.Destroy();
  200. SIO = null;
  201. }
  202. Fpass = true; // reset init flag
  203. return;
  204. }
  205. #endregion Constructor
  206. #region Variables
  207. //HiPerfTimer testTimer1 = new HiPerfTimer();
  208. //HiPerfTimer testTimer2 = new HiPerfTimer();
  209. public SDRSerialPort SIO;
  210. Console console;
  211. ASCIIEncoding AE = new ASCIIEncoding();
  212. private bool Fpass = true;
  213. private bool cat_enabled = false; // is cat currently enabled by user?
  214. // private System.Timers.Timer SIOMonitor;
  215. CATParser parser;
  216. // private int SIOMonitorCount = 0;
  217. #endregion variables
  218. #region Methods
  219. private static void dbgWriteLine(string s)
  220. {
  221. #if(!DBG_PRINT)
  222. Console.dbgWriteLine("SIOListener: " + s);
  223. #endif
  224. }
  225. // Called when the console is activated for the first time.
  226. private void Initialize()
  227. {
  228. if(Fpass)
  229. {
  230. SIO.Create();
  231. Fpass = false;
  232. }
  233. }
  234. #if UseParser
  235. private char[] ParseLeftover = null;
  236. // segment incoming string into CAT commands ... handle leftovers from when we read a parial
  237. //
  238. private void ParseString(byte[] rxdata, uint count)
  239. {
  240. if ( count == 0 ) return; // nothing to do
  241. int cmd_char_count = 0;
  242. int left_over_char_count = ( ParseLeftover == null ? 0 : ParseLeftover.Length );
  243. char[] cmd_chars = new char[count + left_over_char_count];
  244. if ( ParseLeftover != null ) // seed with leftovers from last read
  245. {
  246. for ( int j = 0; j < left_over_char_count; j++ ) // wjt fixme ... use C# equiv of System.arraycopy
  247. {
  248. cmd_chars[cmd_char_count] = ParseLeftover[j];
  249. ++cmd_char_count;
  250. }
  251. ParseLeftover = null;
  252. }
  253. for ( int j = 0; j < count; j++ ) // while we have chars to play with
  254. {
  255. cmd_chars[cmd_char_count] = (char)rxdata[j];
  256. ++cmd_char_count;
  257. if ( rxdata[j] == ';' ) // end of cmd -- parse it and execute it
  258. {
  259. string cmdword = new String(cmd_chars, 0, cmd_char_count);
  260. dbgWriteLine("cmdword: >" + cmdword + "<");
  261. // BT 06/08
  262. string answer = parser.Get(cmdword);
  263. byte[] out_string = AE.GetBytes(answer);
  264. uint result = SIO.put(out_string, (uint) out_string.Length);
  265. cmd_char_count = 0; // reset word counter
  266. }
  267. }
  268. // when we get here have processed all of the incoming buffer, if there's anyting
  269. // in cmd_chars we need to save it as we've not pulled a full command so we stuff
  270. // it in leftover for the next time we come through
  271. if ( cmd_char_count != 0 )
  272. {
  273. ParseLeftover = new char[cmd_char_count];
  274. for ( int j = 0; j < cmd_char_count; j++ ) // wjt fixme ... C# equiv of Sytsem.arraycopy
  275. {
  276. ParseLeftover[j] = cmd_chars[j];
  277. }
  278. }
  279. #if DBG_PRINT
  280. if ( ParseLeftover != null)
  281. {
  282. dbgWriteLine("Leftover >" + new String(ParseLeftover) + "<");
  283. }
  284. #endif
  285. return;
  286. }
  287. #endif
  288. #endregion Methods
  289. #region Events
  290. private void console_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  291. {
  292. if ( SIO != null )
  293. {
  294. SIO.Destroy();
  295. }
  296. }
  297. private void console_Activated(object sender, EventArgs e)
  298. {
  299. if ( console.CATEnabled )
  300. {
  301. // Initialize(); // wjt enable CAT calls Initialize
  302. enableCAT();
  303. }
  304. }
  305. StringBuilder CommBuffer = new StringBuilder();//""; //holds incoming serial data from the port
  306. private void SerialRXEventHandler(object source, SerialRXEvent e)
  307. {
  308. // SIOMonitor.Interval = 5000; // set the timer for 5 seconds
  309. // SIOMonitor.Enabled = true; // start or restart the timer
  310. //double T0 = 0.00;
  311. //double T1 = 0.00;
  312. //int bufferLen = 0;
  313. CommBuffer.Append(e.buffer); // put the data in the string
  314. if(parser != null) // is the parser instantiated
  315. {
  316. //bufferLen = CommBuffer.Length;
  317. try
  318. {
  319. Regex rex = new Regex(".*?;"); //accept any string ending in ;
  320. string answer;
  321. uint result;
  322. for(Match m = rex.Match(CommBuffer.ToString()); m.Success; m = m.NextMatch()) //loop thru the buffer and find matches
  323. {
  324. //testTimer1.Start();
  325. answer = parser.Get(m.Value); //send the match to the parser
  326. //testTimer1.Stop();
  327. //T0 = testTimer1.DurationMsec;
  328. //testTimer2.Start();
  329. if(answer.Length > 0)
  330. result = SIO.put(answer); //send the answer to the serial port
  331. //testTimer2.Stop();
  332. //T1 = testTimer2.DurationMsec;
  333. CommBuffer = CommBuffer.Replace(m.Value, "", 0, m.Length); //remove the match from the buffer
  334. //Debug.WriteLine("Parser decode time for "+m.Value.ToString()+": "+T0.ToString()+ "ms");
  335. //Debug.WriteLine("SIO send answer time: " + T1.ToString() + "ms");
  336. //Debug.WriteLine("CommBuffer Length: " + bufferLen.ToString());
  337. //if (bufferLen > 100)
  338. //Debug.WriteLine("Buffer contents: "+CommBuffer.ToString());
  339. }
  340. }
  341. catch(Exception)
  342. {
  343. //Add ex name to exception above to enable
  344. //Debug.WriteLine("RX Event: "+ex.Message);
  345. //Debug.WriteLine("RX Event: "+ex.StackTrace);
  346. }
  347. }
  348. }
  349. #endregion Events
  350. }
  351. //CAT2
  352. public class SIO2ListenerII
  353. {
  354. #region Constructor
  355. public SIO2ListenerII(Console c)
  356. {
  357. console = c;
  358. console.Activated += new EventHandler(console_Activated);
  359. console.Closing += new System.ComponentModel.CancelEventHandler(console_Closing);
  360. parser = new CATParser(console);
  361. //event handler for Serial RX Events
  362. SDRSerialPort2.serial_rx_event += new SerialRXEventHandler(SerialRX2EventHandler);
  363. if ( console.CAT2Enabled ) // if CAT is on, fire it up
  364. {
  365. try
  366. {
  367. enableCAT2();
  368. }
  369. catch ( Exception ex )
  370. {
  371. // fixme??? how cool is to to pop a msg box from an exception handler in a constructor ??
  372. // seems ugly to me (wjt)
  373. console.CAT2Enabled = false;
  374. if ( console.setupForm != null )
  375. {
  376. console.setupForm.copyCAT2PropsToDialogVars(); // need to make sure the props on the setup page get reset
  377. }
  378. MessageBox.Show("Could not initialize CAT control. Exception was:\n\n " + ex.Message +
  379. "\n\nCAT2 control has been disabled.", "Error Initializing CAT control",
  380. MessageBoxButtons.OK, MessageBoxIcon.Error);
  381. }
  382. }
  383. }
  384. public void enableCAT2()
  385. {
  386. lock ( this )
  387. {
  388. if ( cat2_enabled ) return; // nothing to do already enabled
  389. cat2_enabled = true;
  390. }
  391. Debug.WriteLine("==============CAT PORT OPEN");
  392. int port_num = console.CAT2Port;
  393. SIO2 = new SDRSerialPort2(port_num);
  394. SIO2.setCommParms(console.CAT2BaudRate,
  395. console.CAT2Parity,
  396. console.CAT2DataBits,
  397. console.CAT2StopBits,
  398. console.CAT2Handshake);
  399. Initialize();
  400. }
  401. public bool UseForKeyPTT
  402. {
  403. set
  404. {
  405. if(SIO2 != null)
  406. SIO2.UseForKeyPTT = value;
  407. }
  408. }
  409. public bool UseForPaddles
  410. {
  411. set
  412. {
  413. if (SIO2 != null)
  414. SIO2.UseForPaddles = value;
  415. }
  416. }
  417. public bool PTTOnDTR
  418. {
  419. set
  420. {
  421. if (SIO2 != null)
  422. SIO2.PTTOnDTR = value;
  423. }
  424. }
  425. public bool PTTOnRTS
  426. {
  427. set
  428. {
  429. if (SIO2 != null)
  430. SIO2.PTTOnRTS = value;
  431. }
  432. }
  433. public bool KeyOnDTR
  434. {
  435. set
  436. {
  437. if (SIO2 != null)
  438. SIO2.KeyOnDTR = value;
  439. }
  440. }
  441. public bool KeyOnRTS
  442. {
  443. set
  444. {
  445. if (SIO2 != null)
  446. SIO2.KeyOnRTS = value;
  447. }
  448. }
  449. // typically called when the end user has disabled CAT control through a UI element ... this
  450. // closes the serial port and neutralized the listeners we have in place
  451. public void disableCAT2()
  452. {
  453. lock ( this )
  454. {
  455. if ( !cat2_enabled ) return; /* nothing to do already disabled */
  456. cat2_enabled = false;
  457. }
  458. Debug.WriteLine("==============CAT PORT CLOSED");
  459. if ( SIO2 != null )
  460. {
  461. SIO2.Destroy();
  462. SIO2 = null;
  463. }
  464. Fpass = true; // reset init flag
  465. return;
  466. }
  467. #endregion Constructor
  468. #region Variables
  469. //HiPerfTimer testTimer1 = new HiPerfTimer();
  470. //HiPerfTimer testTimer2 = new HiPerfTimer();
  471. public SDRSerialPort2 SIO2;
  472. Console console;
  473. ASCIIEncoding AE = new ASCIIEncoding();
  474. private bool Fpass = true;
  475. private bool cat2_enabled = false; // is cat currently enabled by user?
  476. // private System.Timers.Timer SIOMonitor;
  477. CATParser parser;
  478. // private int SIOMonitorCount = 0;
  479. #endregion variables
  480. #region Methods
  481. private static void dbgWriteLine(string s)
  482. {
  483. #if(!DBG_PRINT)
  484. Console.dbgWriteLine("SIO2Listener: " + s);
  485. #endif
  486. }
  487. // Called when the console is activated for the first time.
  488. private void Initialize()
  489. {
  490. if(Fpass)
  491. {
  492. SIO2.Create();
  493. Fpass = false;
  494. }
  495. }
  496. #if UseParser
  497. private char[] ParseLeftover = null;
  498. // segment incoming string into CAT commands ... handle leftovers from when we read a parial
  499. //
  500. private void ParseString(byte[] rxdata, uint count)
  501. {
  502. if ( count == 0 ) return; // nothing to do
  503. int cmd_char_count = 0;
  504. int left_over_char_count = ( ParseLeftover == null ? 0 : ParseLeftover.Length );
  505. char[] cmd_chars = new char[count + left_over_char_count];
  506. if ( ParseLeftover != null ) // seed with leftovers from last read
  507. {
  508. for ( int j = 0; j < left_over_char_count; j++ ) // wjt fixme ... use C# equiv of System.arraycopy
  509. {
  510. cmd_chars[cmd_char_count] = ParseLeftover[j];
  511. ++cmd_char_count;
  512. }
  513. ParseLeftover = null;
  514. }
  515. for ( int j = 0; j < count; j++ ) // while we have chars to play with
  516. {
  517. cmd_chars[cmd_char_count] = (char)rxdata[j];
  518. ++cmd_char_count;
  519. if ( rxdata[j] == ';' ) // end of cmd -- parse it and execute it
  520. {
  521. string cmdword = new String(cmd_chars, 0, cmd_char_count);
  522. dbgWriteLine("cmdword: >" + cmdword + "<");
  523. // BT 06/08
  524. string answer = parser.Get(cmdword);
  525. byte[] out_string = AE.GetBytes(answer);
  526. uint result = SIO.put(out_string, (uint) out_string.Length);
  527. cmd_char_count = 0; // reset word counter
  528. }
  529. }
  530. // when we get here have processed all of the incoming buffer, if there's anyting
  531. // in cmd_chars we need to save it as we've not pulled a full command so we stuff
  532. // it in leftover for the next time we come through
  533. if ( cmd_char_count != 0 )
  534. {
  535. ParseLeftover = new char[cmd_char_count];
  536. for ( int j = 0; j < cmd_char_count; j++ ) // wjt fixme ... C# equiv of Sytsem.arraycopy
  537. {
  538. ParseLeftover[j] = cmd_chars[j];
  539. }
  540. }
  541. #if DBG_PRINT
  542. if ( ParseLeftover != null)
  543. {
  544. dbgWriteLine("Leftover >" + new String(ParseLeftover) + "<");
  545. }
  546. #endif
  547. return;
  548. }
  549. #endif
  550. #endregion Methods
  551. #region Events
  552. private void console_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  553. {
  554. if ( SIO2 != null )
  555. {
  556. SIO2.Destroy();
  557. }
  558. }
  559. private void console_Activated(object sender, EventArgs e)
  560. {
  561. if ( console.CAT2Enabled )
  562. {
  563. // Initialize(); // wjt enable CAT calls Initialize
  564. enableCAT2();
  565. }
  566. }
  567. StringBuilder CommBuffer = new StringBuilder();//""; //holds incoming serial data from the port
  568. private void SerialRX2EventHandler(object source, SerialRXEvent e)
  569. {
  570. // SIOMonitor.Interval = 5000; // set the timer for 5 seconds
  571. // SIOMonitor.Enabled = true; // start or restart the timer
  572. //double T0 = 0.00;
  573. //double T1 = 0.00;
  574. //int bufferLen = 0;
  575. CommBuffer.Append(e.buffer); // put the data in the string
  576. if(parser != null) // is the parser instantiated
  577. {
  578. //bufferLen = CommBuffer.Length;
  579. try
  580. {
  581. Regex rex = new Regex(".*?;"); //accept any string ending in ;
  582. string answer;
  583. uint result;
  584. for(Match m = rex.Match(CommBuffer.ToString()); m.Success; m = m.NextMatch()) //loop thru the buffer and find matches
  585. {
  586. //testTimer1.Start();
  587. answer = parser.Get(m.Value); //send the match to the parser
  588. //testTimer1.Stop();
  589. //T0 = testTimer1.DurationMsec;
  590. //testTimer2.Start();
  591. if(answer.Length > 0)
  592. result = SIO2.put(answer); //send the answer to the serial port
  593. //testTimer2.Stop();
  594. //T1 = testTimer2.DurationMsec;
  595. CommBuffer = CommBuffer.Replace(m.Value, "", 0, m.Length); //remove the match from the buffer
  596. //Debug.WriteLine("Parser decode time for "+m.Value.ToString()+": "+T0.ToString()+ "ms");
  597. //Debug.WriteLine("SIO send answer time: " + T1.ToString() + "ms");
  598. //Debug.WriteLine("CommBuffer Length: " + bufferLen.ToString());
  599. //if (bufferLen > 100)
  600. //Debug.WriteLine("Buffer contents: "+CommBuffer.ToString());
  601. }
  602. }
  603. catch(Exception)
  604. {
  605. //Add ex name to exception above to enable
  606. //Debug.WriteLine("RX Event: "+ex.Message);
  607. //Debug.WriteLine("RX Event: "+ex.StackTrace);
  608. }
  609. }
  610. }
  611. #endregion Events
  612. }
  613. //CAT3
  614. public class SIO3ListenerII
  615. {
  616. #region Constructor
  617. public SIO3ListenerII(Console c)
  618. {
  619. console = c;
  620. console.Activated += new EventHandler(console_Activated);
  621. console.Closing += new System.ComponentModel.CancelEventHandler(console_Closing);
  622. parser = new CATParser(console);
  623. //event handler for Serial RX Events
  624. SDRSerialPort3.serial_rx_event += new SerialRXEventHandler(SerialRX3EventHandler);
  625. if ( console.CAT3Enabled ) // if CAT is on, fire it up
  626. {
  627. try
  628. {
  629. enableCAT3();
  630. }
  631. catch ( Exception ex )
  632. {
  633. // fixme??? how cool is to to pop a msg box from an exception handler in a constructor ??
  634. // seems ugly to me (wjt)
  635. console.CAT3Enabled = false;
  636. if ( console.setupForm != null )
  637. {
  638. console.setupForm.copyCAT3PropsToDialogVars(); // need to make sure the props on the setup page get reset
  639. }
  640. MessageBox.Show("Could not initialize CAT control. Exception was:\n\n " + ex.Message +
  641. "\n\nCAT3 control has been disabled.", "Error Initializing CAT control",
  642. MessageBoxButtons.OK, MessageBoxIcon.Error);
  643. }
  644. }
  645. }
  646. public void enableCAT3()
  647. {
  648. lock ( this )
  649. {
  650. if ( cat3_enabled ) return; // nothing to do already enabled
  651. cat3_enabled = true;
  652. }
  653. Debug.WriteLine("==============CAT PORT OPEN");
  654. int port_num = console.CAT3Port;
  655. SIO3 = new SDRSerialPort3(port_num);
  656. SIO3.setCommParms(console.CAT3BaudRate,
  657. console.CAT3Parity,
  658. console.CAT3DataBits,
  659. console.CAT3StopBits,
  660. console.CAT3Handshake);
  661. Initialize();
  662. }
  663. public bool UseForKeyPTT
  664. {
  665. set
  666. {
  667. if(SIO3 != null)
  668. SIO3.UseForKeyPTT = value;
  669. }
  670. }
  671. public bool UseForPaddles
  672. {
  673. set
  674. {
  675. if (SIO3 != null)
  676. SIO3.UseForPaddles = value;
  677. }
  678. }
  679. public bool PTTOnDTR
  680. {
  681. set
  682. {
  683. if (SIO3 != null)
  684. SIO3.PTTOnDTR = value;
  685. }
  686. }
  687. public bool PTTOnRTS
  688. {
  689. set
  690. {
  691. if (SIO3 != null)
  692. SIO3.PTTOnRTS = value;
  693. }
  694. }
  695. public bool KeyOnDTR
  696. {
  697. set
  698. {
  699. if (SIO3 != null)
  700. SIO3.KeyOnDTR = value;
  701. }
  702. }
  703. public bool KeyOnRTS
  704. {
  705. set
  706. {
  707. if (SIO3 != null)
  708. SIO3.KeyOnRTS = value;
  709. }
  710. }
  711. // typically called when the end user has disabled CAT control through a UI element ... this
  712. // closes the serial port and neutralized the listeners we have in place
  713. public void disableCAT3()
  714. {
  715. lock ( this )
  716. {
  717. if ( !cat3_enabled ) return; /* nothing to do already disabled */
  718. cat3_enabled = false;
  719. }
  720. Debug.WriteLine("==============CAT PORT CLOSED");
  721. if ( SIO3 != null )
  722. {
  723. SIO3.Destroy();
  724. SIO3 = null;
  725. }
  726. Fpass = true; // reset init flag
  727. return;
  728. }
  729. #endregion Constructor
  730. #region Variables
  731. //HiPerfTimer testTimer1 = new HiPerfTimer();
  732. //HiPerfTimer testTimer2 = new HiPerfTimer();
  733. public SDRSerialPort3 SIO3;
  734. Console console;
  735. ASCIIEncoding AE = new ASCIIEncoding();
  736. private bool Fpass = true;
  737. private bool cat3_enabled = false; // is cat currently enabled by user?
  738. // private System.Timers.Timer SIOMonitor;
  739. CATParser parser;
  740. // private int SIOMonitorCount = 0;
  741. #endregion variables
  742. #region Methods
  743. private static void dbgWriteLine(string s)
  744. {
  745. #if(!DBG_PRINT)
  746. Console.dbgWriteLine("SIO3Listener: " + s);
  747. #endif
  748. }
  749. // Called when the console is activated for the first time.
  750. private void Initialize()
  751. {
  752. if(Fpass)
  753. {
  754. SIO3.Create();
  755. Fpass = false;
  756. }
  757. }
  758. #if UseParser
  759. private char[] ParseLeftover = null;
  760. // segment incoming string into CAT commands ... handle leftovers from when we read a parial
  761. //
  762. private void ParseString(byte[] rxdata, uint count)
  763. {
  764. if ( count == 0 ) return; // nothing to do
  765. int cmd_char_count = 0;
  766. int left_over_char_count = ( ParseLeftover == null ? 0 : ParseLeftover.Length );
  767. char[] cmd_chars = new char[count + left_over_char_count];
  768. if ( ParseLeftover != null ) // seed with leftovers from last read
  769. {
  770. for ( int j = 0; j < left_over_char_count; j++ ) // wjt fixme ... use C# equiv of System.arraycopy
  771. {
  772. cmd_chars[cmd_char_count] = ParseLeftover[j];
  773. ++cmd_char_count;
  774. }
  775. ParseLeftover = null;
  776. }
  777. for ( int j = 0; j < count; j++ ) // while we have chars to play with
  778. {
  779. cmd_chars[cmd_char_count] = (char)rxdata[j];
  780. ++cmd_char_count;
  781. if ( rxdata[j] == ';' ) // end of cmd -- parse it and execute it
  782. {
  783. string cmdword = new String(cmd_chars, 0, cmd_char_count);
  784. dbgWriteLine("cmdword: >" + cmdword + "<");
  785. // BT 06/08
  786. string answer = parser.Get(cmdword);
  787. byte[] out_string = AE.GetBytes(answer);
  788. uint result = SIO.put(out_string, (uint) out_string.Length);
  789. cmd_char_count = 0; // reset word counter
  790. }
  791. }
  792. // when we get here have processed all of the incoming buffer, if there's anyting
  793. // in cmd_chars we need to save it as we've not pulled a full command so we stuff
  794. // it in leftover for the next time we come through
  795. if ( cmd_char_count != 0 )
  796. {
  797. ParseLeftover = new char[cmd_char_count];
  798. for ( int j = 0; j < cmd_char_count; j++ ) // wjt fixme ... C# equiv of Sytsem.arraycopy
  799. {
  800. ParseLeftover[j] = cmd_chars[j];
  801. }
  802. }
  803. #if DBG_PRINT
  804. if ( ParseLeftover != null)
  805. {
  806. dbgWriteLine("Leftover >" + new String(ParseLeftover) + "<");
  807. }
  808. #endif
  809. return;
  810. }
  811. #endif
  812. #endregion Methods
  813. #region Events
  814. private void console_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  815. {
  816. if ( SIO3 != null )
  817. {
  818. SIO3.Destroy();
  819. }
  820. }
  821. private void console_Activated(object sender, EventArgs e)
  822. {
  823. if ( console.CAT3Enabled )
  824. {
  825. // Initialize(); // wjt enable CAT calls Initialize
  826. enableCAT3();
  827. }
  828. }
  829. StringBuilder CommBuffer = new StringBuilder();//""; //holds incoming serial data from the port
  830. private void SerialRX3EventHandler(object source, SerialRXEvent e)
  831. {
  832. // SIOMonitor.Interval = 5000; // set the timer for 5 seconds
  833. // SIOMonitor.Enabled = true; // start or restart the timer
  834. //double T0 = 0.00;
  835. //double T1 = 0.00;
  836. //int bufferLen = 0;
  837. CommBuffer.Append(e.buffer); // put the data in the string
  838. if(parser != null) // is the parser instantiated
  839. {
  840. //bufferLen = CommBuffer.Length;
  841. try
  842. {
  843. Regex rex = new Regex(".*?;"); //accept any string ending in ;
  844. string answer;
  845. uint result;
  846. for(Match m = rex.Match(CommBuffer.ToString()); m.Success; m = m.NextMatch()) //loop thru the buffer and find matches
  847. {
  848. //testTimer1.Start();
  849. answer = parser.Get(m.Value); //send the match to the parser
  850. //testTimer1.Stop();
  851. //T0 = testTimer1.DurationMsec;
  852. //testTimer2.Start();
  853. if(answer.Length > 0)
  854. result = SIO3.put(answer); //send the answer to the serial port
  855. //testTimer2.Stop();
  856. //T1 = testTimer2.DurationMsec;
  857. CommBuffer = CommBuffer.Replace(m.Value, "", 0, m.Length); //remove the match from the buffer
  858. //Debug.WriteLine("Parser decode time for "+m.Value.ToString()+": "+T0.ToString()+ "ms");
  859. //Debug.WriteLine("SIO send answer time: " + T1.ToString() + "ms");
  860. //Debug.WriteLine("CommBuffer Length: " + bufferLen.ToString());
  861. //if (bufferLen > 100)
  862. //Debug.WriteLine("Buffer contents: "+CommBuffer.ToString());
  863. }
  864. }
  865. catch(Exception)
  866. {
  867. //Add ex name to exception above to enable
  868. //Debug.WriteLine("RX Event: "+ex.Message);
  869. //Debug.WriteLine("RX Event: "+ex.StackTrace);
  870. }
  871. }
  872. }
  873. #endregion Events
  874. }
  875. //CAT4
  876. public class SIO4ListenerII
  877. {
  878. #region Constructor
  879. public SIO4ListenerII(Console c)
  880. {
  881. console = c;
  882. console.Activated += new EventHandler(console_Activated);
  883. console.Closing += new System.ComponentModel.CancelEventHandler(console_Closing);
  884. parser = new CATParser(console);
  885. //event handler for Serial RX Events
  886. SDRSerialPort2.serial_rx_event += new SerialRXEventHandler(SerialRX2EventHandler);
  887. if ( console.CAT4Enabled ) // if CAT is on, fire it up
  888. {
  889. try
  890. {
  891. enableCAT4();
  892. }
  893. catch ( Exception ex )
  894. {
  895. // fixme??? how cool is to to pop a msg box from an exception handler in a constructor ??
  896. // seems ugly to me (wjt)
  897. console.CAT4Enabled = false;
  898. if ( console.setupForm != null )
  899. {
  900. console.setupForm.copyCAT4PropsToDialogVars(); // need to make sure the props on the setup page get reset
  901. }
  902. MessageBox.Show("Could not initialize CAT control. Exception was:\n\n " + ex.Message +
  903. "\n\nCAT4 control has been disabled.", "Error Initializing CAT control",
  904. MessageBoxButtons.OK, MessageBoxIcon.Error);
  905. }
  906. }
  907. }
  908. public void enableCAT4()
  909. {
  910. lock ( this )
  911. {
  912. if ( cat4_enabled ) return; // nothing to do already enabled
  913. cat4_enabled = true;
  914. }
  915. Debug.WriteLine("==============CAT PORT OPEN");
  916. int port_num = console.CAT4Port;
  917. SIO4 = new SDRSerialPort2(port_num);
  918. SIO4.setCommParms(console.CAT4BaudRate,
  919. console.CAT4Parity,
  920. console.CAT4DataBits,
  921. console.CAT4StopBits,
  922. console.CAT4Handshake);
  923. Initialize();
  924. }
  925. public bool UseForKeyPTT
  926. {
  927. set
  928. {
  929. if(SIO4 != null)
  930. SIO4.UseForKeyPTT = value;
  931. }
  932. }
  933. public bool UseForPaddles
  934. {
  935. set
  936. {
  937. if (SIO4 != null)
  938. SIO4.UseForPaddles = value;
  939. }
  940. }
  941. public bool PTTOnDTR
  942. {
  943. set
  944. {
  945. if (SIO4 != null)
  946. SIO4.PTTOnDTR = value;
  947. }
  948. }
  949. public bool PTTOnRTS
  950. {
  951. set
  952. {
  953. if (SIO4 != null)
  954. SIO4.PTTOnRTS = value;
  955. }
  956. }
  957. public bool KeyOnDTR
  958. {
  959. set
  960. {
  961. if (SIO4 != null)
  962. SIO4.KeyOnDTR = value;
  963. }
  964. }
  965. public bool KeyOnRTS
  966. {
  967. set
  968. {
  969. if (SIO4 != null)
  970. SIO4.KeyOnRTS = value;
  971. }
  972. }
  973. // typically called when the end user has disabled CAT control through a UI element ... this
  974. // closes the serial port and neutralized the listeners we have in place
  975. public void disableCAT4()
  976. {
  977. lock ( this )
  978. {
  979. if ( !cat4_enabled ) return; /* nothing to do already disabled */
  980. cat4_enabled = false;
  981. }
  982. Debug.WriteLine("==============CAT PORT CLOSED");
  983. if ( SIO4 != null )
  984. {
  985. SIO4.Destroy();
  986. SIO4 = null;
  987. }
  988. Fpass = true; // reset init flag
  989. return;
  990. }
  991. #endregion Constructor
  992. #region Variables
  993. //HiPerfTimer testTimer1 = new HiPerfTimer();
  994. //HiPerfTimer testTimer2 = new HiPerfTimer();
  995. public SDRSerialPort2 SIO4;
  996. Console console;
  997. ASCIIEncoding AE = new ASCIIEncoding();
  998. private bool Fpass = true;
  999. private bool cat4_enabled = false; // is cat currently enabled by user?
  1000. // private System.Timers.Timer SIOMonitor;
  1001. CATParser parser;
  1002. // private int SIOMonitorCount = 0;
  1003. #endregion variables
  1004. #region Methods
  1005. private static void dbgWriteLine(string s)
  1006. {
  1007. #if(!DBG_PRINT)
  1008. Console.dbgWriteLine("SIO4Listener: " + s);
  1009. #endif
  1010. }
  1011. // Called when the console is activated for the first time.
  1012. private void Initialize()
  1013. {
  1014. if(Fpass)
  1015. {
  1016. SIO4.Create();
  1017. Fpass = false;
  1018. }
  1019. }
  1020. #if UseParser
  1021. private char[] ParseLeftover = null;
  1022. // segment incoming string into CAT commands ... handle leftovers from when we read a parial
  1023. //
  1024. private void ParseString(byte[] rxdata, uint count)
  1025. {
  1026. if ( count == 0 ) return; // nothing to do
  1027. int cmd_char_count = 0;
  1028. int left_over_char_count = ( ParseLeftover == null ? 0 : ParseLeftover.Length );
  1029. char[] cmd_chars = new char[count + left_over_char_count];
  1030. if ( ParseLeftover != null ) // seed with leftovers from last read
  1031. {
  1032. for ( int j = 0; j < left_over_char_count; j++ ) // wjt fixme ... use C# equiv of System.arraycopy
  1033. {
  1034. cmd_chars[cmd_char_count] = ParseLeftover[j];
  1035. ++cmd_char_count;
  1036. }
  1037. ParseLeftover = null;
  1038. }
  1039. for ( int j = 0; j < count; j++ ) // while we have chars to play with
  1040. {
  1041. cmd_chars[cmd_char_count] = (char)rxdata[j];
  1042. ++cmd_char_count;
  1043. if ( rxdata[j] == ';' ) // end of cmd -- parse it and execute it
  1044. {
  1045. string cmdword = new String(cmd_chars, 0, cmd_char_count);
  1046. dbgWriteLine("cmdword: >" + cmdword + "<");
  1047. // BT 06/08
  1048. string answer = parser.Get(cmdword);
  1049. byte[] out_string = AE.GetBytes(answer);
  1050. uint result = SIO.put(out_string, (uint) out_string.Length);
  1051. cmd_char_count = 0; // reset word counter
  1052. }
  1053. }
  1054. // when we get here have processed all of the incoming buffer, if there's anyting
  1055. // in cmd_chars we need to save it as we've not pulled a full command so we stuff
  1056. // it in leftover for the next time we come through
  1057. if ( cmd_char_count != 0 )
  1058. {
  1059. ParseLeftover = new char[cmd_char_count];
  1060. for ( int j = 0; j < cmd_char_count; j++ ) // wjt fixme ... C# equiv of Sytsem.arraycopy
  1061. {
  1062. ParseLeftover[j] = cmd_chars[j];
  1063. }
  1064. }
  1065. #if DBG_PRINT
  1066. if ( ParseLeftover != null)
  1067. {
  1068. dbgWriteLine("Leftover >" + new String(ParseLeftover) + "<");
  1069. }
  1070. #endif
  1071. return;
  1072. }
  1073. #endif
  1074. #endregion Methods
  1075. #region Events
  1076. private void console_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  1077. {
  1078. if ( SIO4 != null )
  1079. {
  1080. SIO4.Destroy();
  1081. }
  1082. }
  1083. private void console_Activated(object sender, EventArgs e)
  1084. {
  1085. if ( console.CAT4Enabled )
  1086. {
  1087. // Initialize(); // wjt enable CAT calls Initialize
  1088. enableCAT4();
  1089. }
  1090. }
  1091. StringBuilder CommBuffer = new StringBuilder();//""; //holds incoming serial data from the port
  1092. private void SerialRX2EventHandler(object source, SerialRXEvent e)
  1093. {
  1094. // SIOMonitor.Interval = 5000; // set the timer for 5 seconds
  1095. // SIOMonitor.Enabled = true; // start or restart the timer
  1096. //double T0 = 0.00;
  1097. //double T1 = 0.00;
  1098. //int bufferLen = 0;
  1099. CommBuffer.Append(e.buffer); // put the data in the string
  1100. if(parser != null) // is the parser instantiated
  1101. {
  1102. //bufferLen = CommBuffer.Length;
  1103. try
  1104. {
  1105. Regex rex = new Regex(".*?;"); //accept any string ending in ;
  1106. string answer;
  1107. uint result;
  1108. for(Match m = rex.Match(CommBuffer.ToString()); m.Success; m = m.NextMatch()) //loop thru the buffer and find matches
  1109. {
  1110. //testTimer1.Start();
  1111. answer = parser.Get(m.Value); //send the match to the parser
  1112. //testTimer1.Stop();
  1113. //T0 = testTimer1.DurationMsec;
  1114. //testTimer2.Start();
  1115. if(answer.Length > 0)
  1116. result = SIO4.put(answer); //send the answer to the serial port
  1117. //testTimer2.Stop();
  1118. //T1 = testTimer2.DurationMsec;
  1119. CommBuffer = CommBuffer.Replace(m.Value, "", 0, m.Length); //remove the match from the buffer
  1120. //Debug.WriteLine("Parser decode time for "+m.Value.ToString()+": "+T0.ToString()+ "ms");
  1121. //Debug.WriteLine("SIO send answer time: " + T1.ToString() + "ms");
  1122. //Debug.WriteLine("CommBuffer Length: " + bufferLen.ToString());
  1123. //if (bufferLen > 100)
  1124. //Debug.WriteLine("Buffer contents: "+CommBuffer.ToString());
  1125. }
  1126. }
  1127. catch(Exception)
  1128. {
  1129. //Add ex name to exception above to enable
  1130. //Debug.WriteLine("RX Event: "+ex.Message);
  1131. //Debug.WriteLine("RX Event: "+ex.StackTrace);
  1132. }
  1133. }
  1134. }
  1135. #endregion Events
  1136. }
  1137. }