Advertisement
xorkrus

Untitled

Apr 13th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO.Ports;
  6. using System.Windows.Forms;
  7. using System.Threading;
  8.  
  9. namespace ConsoleApplication1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {}
  15.         public sealed class Init
  16.         {
  17.             public static SerialPort mySerialPort = new SerialPort();
  18.             public static string seriya = "";
  19.             public static string nomer = "";
  20.             public static string karta = "";
  21.             public static string karta16;
  22.  
  23.             public void PortProperties()
  24.             {
  25.  
  26.                 if (mySerialPort.IsOpen == false)
  27.                 {
  28.                     mySerialPort.BaudRate = 9600;
  29.                     mySerialPort.Parity = Parity.None;
  30.                     mySerialPort.StopBits = StopBits.One;
  31.                     mySerialPort.DataBits = 8;
  32.                     mySerialPort.Handshake = Handshake.None;
  33.                     mySerialPort.ReadTimeout = 200;
  34.                     mySerialPort.PortName = "COM2";
  35.                     try
  36.                     {
  37.                         mySerialPort.Open();
  38.                     }
  39.                     catch (System.IO.IOException)
  40.                     {
  41.                         //throw new Exception("Устройство не подключено");
  42.                     }
  43.                     Thread t = new Thread(Opros);
  44.                     t.Start();
  45.                     t.IsBackground = true;
  46.                     mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
  47.                     OnPick += OnPicked;
  48.  
  49.                 }
  50.             }
  51.  
  52.             static void Opros()
  53.             {
  54.                 var data_for_controller1 = new byte[5] { 242, 255, 1, 2, 3 };
  55.                 var data_for_controller = new byte[7] { 242, 255, 3, 1, 0, 26, 24 };
  56.  
  57.                 if (mySerialPort.IsOpen)
  58.                 mySerialPort.Write(data_for_controller, 0, 7);
  59.  
  60.                 while (mySerialPort.IsOpen)
  61.                 {
  62.                     try
  63.                     {
  64.                         mySerialPort.Write(data_for_controller1, 0, 5);
  65.                     }
  66.                     catch(System.IO.IOException)
  67.                     {}
  68.                 }
  69.  
  70.             }
  71.  
  72.             public static void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  73.             {
  74.  
  75.                 byte[] buffer = new byte[mySerialPort.BytesToRead];
  76.                 mySerialPort.Read(buffer, 0, buffer.Length);
  77.  
  78.                 for (int i = 2; i < buffer.Length; i = i + 8)
  79.                 {
  80.                     if (buffer[i] == 8)
  81.                     {
  82.                         int nomk = (buffer[i + 5] >> 1) + (buffer[i + 6] & 1) * 128;
  83.                         int ss = (buffer[i + 6] >> 1) + (buffer[i + 7] & 1) * 128;
  84.                         nomk = nomk + ss * 256;
  85.                         int serk = (buffer[i + 7] >> 1) + (buffer[i + 8] & 1) * 128;
  86.  
  87.                         karta = serk.ToString() + nomk.ToString();
  88.  
  89.                         karta16 = buffer[i + 5].ToString() + buffer[i + 6].ToString() + buffer[i + 7].ToString() + buffer[i + 8].ToString();
  90.                         seriya = serk.ToString();
  91.                         nomer = nomk.ToString();
  92.                         Thread.Sleep(200);
  93.                         OnRaiseCustomEvent(new EventArgs());
  94.  
  95.                     }
  96.                 }
  97.                 if (mySerialPort.IsOpen)
  98.                 {
  99.                     mySerialPort.DiscardOutBuffer();
  100.                 }
  101.             }
  102.  
  103.             public static event EventHandler<EventArgs> OnPick;
  104.  
  105.             public static void OnRaiseCustomEvent(EventArgs e)
  106.             {
  107.                 EventHandler<EventArgs> handler = OnPick;
  108.                 if (handler != null)
  109.                 {
  110.                     handler(null, e);
  111.                 }
  112.             }
  113.  
  114.             void OnPicked(object sender, EventArgs e)
  115.             {
  116.                 for (int i = 0; i < 10; i++)
  117.                 {
  118.                     SendKeys.SendWait("{BS}");  
  119.                 }
  120.                 SendKeys.SendWait(seriya + " " + nomer);
  121.             }
  122.  
  123.         }
  124.      
  125.     }
  126. }
  127.  
  128. // perko reader .net c#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement