-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
151 lines (129 loc) · 5.1 KB
/
Form1.cs
File metadata and controls
151 lines (129 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace TestFormsC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String[] ports = SerialPort.GetPortNames();
cBoxComPorts.Items.AddRange(ports);
serialPort1.ReadTimeout = 3000;
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void buttonProg_Click(object sender, EventArgs e)
{
int freq;
String settings_str = "START";
try
{
freq = int.Parse(cBoxFreq.Text);
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Console.WriteLine("Введённая частота={0}", freq);
if ((freq < 0) | (freq == 0) | (freq < 3510) | (freq > 3600))
{
Console.WriteLine("Введена неправильная частота");
// Initializes the variables to pass to the MessageBox.Show method.
string message = "Диапазон используемых частот передатчика должен быть в пределах 3510 - 3600 кГц";
string caption = "Введена некорректная частота";
// MessageBoxButtons buttons = MessageBoxButtons.YesNo;
// DialogResult result;
// Displays the MessageBox.
MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (rbMOE.Checked == true) settings_str += ",MOE";
if (rbMOI.Checked == true) settings_str += ",MOI";
if (rbMOS.Checked == true) settings_str += ",MOS";
if (rbMOH.Checked == true) settings_str += ",MOH";
if (rbMO5.Checked == true) settings_str += ",MO5";
if (rbMO.Checked == true) settings_str += ",MO";
if (rbMM.Checked == true) settings_str += ",MM";
if (rb30s.Checked == true) settings_str += ",30";
if (rb60s.Checked == true) settings_str += ",60";
settings_str += ",";
settings_str += Convert.ToString(freq);
if (cBoxStartMode.Checked == true) settings_str += ",1";
else settings_str += ",0";
settings_str += ",STOP";
Console.WriteLine(settings_str);
try
{
serialPort1.PortName = cBoxComPorts.Text;
serialPort1.Open();
serialPort1.WriteLine(settings_str);
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
string txAnswerStr = serialPort1.ReadLine();
if (txAnswerStr == "OK") labelDevAnswer.Text = "Программирование выполнено!";
timer2.Start();
}
catch (Exception err)
{
MessageBox.Show("Таймаут ответа от передатчика", err.Message, MessageBoxButtons.OK,MessageBoxIcon.Error);
Console.WriteLine("Строка с настройками НЕ отправлена");
labelOutSrt.Text = "Строка с настройками НЕ отправлена";
serialPort1.Close();
timer1.Start();
return;
}
serialPort1.Close();
Console.WriteLine("Строка с настройками отправлена");
labelOutSrt.Text = "Строка с настройками отправлена";
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
labelOutSrt.Text = "";
}
private void cBoxFreq_SelectedIndexChanged(object sender, EventArgs e)
{
int freq;
try
{
freq = int.Parse(cBoxFreq.Text);
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if ((freq < 0) | (freq == 0) | (freq < 3510) | (freq > 3600)) cBoxFreq.ForeColor = Color.Red;
else cBoxFreq.ForeColor = Color.Black;
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
labelDevAnswer.Text = "";
}
}
}