Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace ComConnector
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private ComPort _comPort;
  17.         private Modul_I7021 _modulI7021;
  18.         private Modul_I7044 _modulI7044;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.  
  24.             UserUI();
  25.         }
  26.  
  27.         public void SettingsUI()
  28.         {
  29.             GBSettings.Visible = true;
  30.             GBTarget.Visible = false;
  31.             this.Height = 260;
  32.         }
  33.         public void UserUI()
  34.         {
  35.             GBTarget.Visible = true;
  36.             GBSettings.Visible = false;
  37.             GBTarget.Location = new Point(9, 27);
  38.             this.Height = 345;
  39.         }
  40.  
  41.  
  42.         private void OpenPort()
  43.         {
  44.             Thread.Sleep(100);
  45.             _comPort = new ComPort(TextBoxComPort.Text, (int)NumericBaudRate.Value);
  46.             var resultOpenPort = _comPort.OpenPort();
  47.             AddLogMessage(resultOpenPort);
  48.  
  49.             _modulI7021 = new Modul_I7021(_comPort, (int)NUD7021.Value);
  50.             _modulI7044 = new Modul_I7044(_comPort);
  51.  
  52.             if (!resultOpenPort.ToLower().Contains("ошибка")) ;
  53.         }
  54.  
  55.         private void ButtonOpenPort_Click(object sender, EventArgs e)
  56.         {
  57.            
  58.         }
  59.  
  60.         private void ButtonClosePort_Click(object sender, EventArgs e)
  61.         {
  62.             AddLogMessage(_comPort.ClosePort());
  63.            
  64.         }
  65.  
  66.         private void ButtonHandler_Click(object sender, EventArgs e)
  67.         {
  68.             if (_comPort == null)
  69.             {
  70.                 OpenPort();
  71.             }
  72.  
  73.  
  74.  
  75.             var count = _modulI7044.GetCountPort((int) NUD7044.Value);
  76.             AddLogMessage($"С устройства {NUD7044.Value} считали {count}");
  77.  
  78.  
  79.             if (count <= NumericFirstN.Value)
  80.             {
  81.                 AddLogMessage($"{count} <= n1. " +
  82.                               $"Установим 5 вольт");
  83.                 _modulI7021.SetVolt((int) NUD7021.Value, "7FF");
  84.             }
  85.             else if (count <= NumericSecondN.Value)
  86.             {
  87.                 AddLogMessage($"{count} <= n2. " +
  88.                               $"Установим 2.5 вольта");
  89.                 _modulI7021.SetVolt((int) NUD7021.Value, "3FF");
  90.             }
  91.             else
  92.             {
  93.                 AddLogMessage("Количество импульсов больше чем н2");
  94.             }
  95.         }
  96.         private void ButtonClearPort_Click(object sender, EventArgs e)
  97.         {
  98.             AddLogMessage(_modulI7044.ClearCountPort((int)NUD7044.Value));
  99.         }
  100.  
  101.         private void AddLogMessage(string message)
  102.         {
  103.             TextBoxLog.AppendText($"{message}{Environment.NewLine}");
  104.             TextBoxLog.ScrollToCaret();
  105.         }
  106.  
  107.         private void ОбАвторахToolStripMenuItem_Click(object sender, EventArgs e)
  108.         {
  109.             MessageBox.Show($"Дмитриев Юрий\nБражникова Алина\nГруппа 6401", "Об авторах", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  110.         }
  111.  
  112.         private bool settings = false;
  113.         private void НастрокиToolStripMenuItem_Click(object sender, EventArgs e)
  114.         {
  115.             if (!settings)
  116.             {
  117.                 SettingsUI();
  118.             }
  119.             else
  120.             {
  121.                 UserUI();
  122.             }
  123.  
  124.             settings = !settings;
  125.         }
  126.  
  127.         private void SendCommand_Click(object sender, EventArgs e)
  128.         {
  129.             if (_comPort == null)
  130.             {
  131.                 OpenPort();
  132.                 _comPort.SendPort(TBCommand.Text);
  133.                 while (_comPort.Answer == null) ;
  134.             }
  135.  
  136.             _comPort.SendPort(TBCommand.Text);
  137.             while (_comPort.Answer == null) ;
  138.            
  139.             TBAnswer.Text = _comPort.Answer;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement