Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 KB | None | 0 0
  1.  
  2.  
  3. //#define DEBUGin
  4.  
  5. using System;
  6. using System.Management;
  7. using System.IO.Ports;
  8. using System.Threading;
  9.  
  10. namespace LaparoTalker
  11. {
  12.     class Program
  13.     {
  14.         static byte[] CMP = { 0x43, 0x4D, 0x50, 0x0, 0x0, 0x0, 0x0, 0xE0 };
  15.         static byte[] CMS = { 0x43, 0x4D, 0x53, 0x0, 0x0, 0x0, 0x0, 0xE3 };
  16.         static string CMP_p = ByteToHexStringConverter.ByteToHexBitFiddle(CMP);
  17.         static string CMP_s = ByteToHexStringConverter.ByteToHexBitFiddle(CMS);
  18.  
  19.  
  20.         static string portName;
  21.         static SerialPort Port = new SerialPort();
  22.         static FlagCarrier _continue = new FlagCarrier();
  23.         static int order = 0;
  24.         static string command = null;
  25.  
  26.         public static void Main()
  27.         {
  28.             FindPortName();
  29.             Init();
  30.             OpenPort();
  31.  
  32.             ResponseListener Listener = new ResponseListener(Port, ref _continue);
  33.             Thread ListenerThread = new Thread(new ThreadStart(Listener.Run));
  34.             ListenerThread.Start();
  35.  
  36.             Pinger Pinger = new Pinger(Port, ref _continue);
  37.             Thread PingerThread = new Thread(new ThreadStart(Pinger.Run));
  38.             PingerThread.Start();
  39.  
  40.             Console.WriteLine("1.Wyslij CMP\n" + "2.Wyslij CMS\n" + "3. Zakoncz");
  41.             while (_continue.bContinue)
  42.             {
  43. //              Console.WriteLine("1.Wyslij CMP\n" + "2.Wyslij CMS\n" + "3. Zakoncz");
  44.                 Thread.Sleep(50);
  45.                 do
  46.                 {
  47.                     Console.Write(">");
  48.                     int.TryParse(Console.ReadLine(), out order);
  49.                 } while (order > 3 || order < 1);
  50.  
  51.                 switch (order)
  52.                 {
  53.                     case 1:
  54.                         command = ByteToHexStringConverter.ByteToHexBitFiddle(CMP);
  55.                         break;
  56.                     case 2:
  57.                         command = ByteToHexStringConverter.ByteToHexBitFiddle(CMS);
  58.                         break;
  59.                     case 3:
  60.                         _continue.bContinue = false;
  61.                         break;
  62.                     default:
  63.                         break;
  64.  
  65.                 }
  66.                 Port.WriteLine(command);
  67. //              Console.Clear();
  68.             }
  69.  
  70.  
  71.             ListenerThread.Join();
  72.             Thread.Sleep(1000);
  73.             ClosePort();
  74.         }
  75.  
  76.         public static void Init()
  77.         {
  78.             Port.PortName = portName;
  79.             Port.BaudRate = 9600;
  80.             Port.Parity = Parity.None;
  81.             Port.StopBits = StopBits.One;
  82.             Port.DataBits = 8;
  83.             Port.Handshake = Handshake.None;
  84.             Port.ReadTimeout = 500;
  85.             Port.WriteTimeout = 500;
  86.         }
  87.  
  88.         public static void FindPortName()
  89.         {
  90.  
  91.             ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");
  92.             foreach (ManagementObject ManObj in searcher.Get())
  93.             {
  94. #if DEBUGin
  95.                 Console.WriteLine("-------------------------------");
  96.                 Console.WriteLine("DeviceID: {0}", ManObj["DeviceID"].ToString());
  97.                 Console.WriteLine("PNPDeviceID: {0}", ManObj["PNPDeviceID"].ToString());
  98.                 Console.WriteLine("Name: {0}", ManObj["Name"].ToString());
  99.                 Console.WriteLine("Caption: {0}", ManObj["Caption"].ToString());
  100.                 Console.WriteLine("Description: {0}", ManObj["Description"].ToString());
  101.                 Console.WriteLine("Status: {0}", ManObj["Status"].ToString());
  102.                 Console.WriteLine("\n");
  103. #endif
  104.                 if (ManObj["DeviceID"].ToString().Contains("PID_6001"))
  105.                 {
  106.                     string[] substrings = ManObj["Name"].ToString().Split('(');
  107.                     substrings = substrings[1].Split(')');
  108. #if DEBUGin
  109.                     Console.WriteLine("Port do podlaczenia: {0}", substrings[0]);
  110. #endif
  111.                     portName = substrings[0];
  112.                 }
  113.  
  114.             }
  115.  
  116.         }
  117.  
  118.         public static void OpenPort()
  119.         {
  120.             Port.Open();
  121.             Console.WriteLine("port {0} zostal otwarty", portName);
  122.         }
  123.  
  124.         public static void ClosePort()
  125.         {
  126.             Port.Close();
  127.             Console.WriteLine("port {0} zostal zamkniety", portName);
  128.         }
  129.  
  130.     }
  131. }
  132. class ByteToHexStringConverter                                      // https://stackoverflow.com/a/14333437
  133. {
  134.         static public string ByteToHexBitFiddle(byte[] bytes)
  135.         {
  136.             char[] c = new char[bytes.Length * 2];
  137.             int b;
  138.             for (int i = 0; i < bytes.Length; i++)
  139.             {
  140.                 b = bytes[i] >> 4;
  141.                 c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
  142.                 b = bytes[i] & 0xF;
  143.                 c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
  144.             }
  145.             return new string(c);
  146.         }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement