Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.IO.Ports;
  6. using System.Threading;
  7. using System.Reflection;
  8.  
  9. namespace Scratchpad
  10. {
  11.     class gpsTest
  12.     {
  13.         SerialPort gpsPort;
  14.        
  15.         public gpsTest()
  16.         {
  17.             gpsPort = new SerialPort();
  18.             if (gpsPort != null)
  19.             {
  20.                 gpsPort.ReadTimeout = 2000;
  21.                 gpsPort.BaudRate = 9600;
  22.             }
  23.         }
  24.  
  25.         internal bool openPort(string _portName, ref string error)
  26.         {
  27.             error = string.Empty;
  28.             bool opened = false;
  29.             gpsPort.PortName = _portName;
  30.             try
  31.             {
  32.                 if (!gpsPort.IsOpen)
  33.                 {
  34.                     //ndl_gps.SerialPortFixer.Execute(gpsPort.PortName);
  35.  
  36.                     gpsPort.Open();
  37.                     opened = true;
  38.                 }
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.                 error = ex.Message;
  43.             }
  44.  
  45.             return opened;
  46.  
  47.         }
  48.  
  49.         internal bool closePort()
  50.         {
  51.             bool closed = false;
  52.  
  53.             try
  54.             {
  55.                 if (gpsPort.IsOpen)
  56.                 {
  57.                     gpsPort.Close();
  58.                    
  59.                     closed = true;
  60.                 }
  61.  
  62.             }
  63.             catch (Exception ex)
  64.             {
  65.                 Console.WriteLine(ex.Message);
  66.             }
  67.  
  68.             return closed;
  69.         }
  70.  
  71.         public List<string> findAvailablePorts()
  72.         {
  73.             List<string> ports = new List<string>();
  74.  
  75.             foreach (string str in SerialPort.GetPortNames())
  76.                 ports.Add(str);
  77.  
  78.             return ports;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement