-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
195 lines (169 loc) · 5.19 KB
/
Copy pathForm1.cs
File metadata and controls
195 lines (169 loc) · 5.19 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ArduinoTest
{
public partial class Form1 : Form
{
bool isConnected = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttonConnect.Enabled = false;
label1.Text = "0";
}
void btnStatus() {
if (serialPort.IsOpen)
this.buttonStatus.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
else
buttonStatus.BackColor = default;
}
private void connectToArduino()
{
isConnected = true;
string selectedPort = comboBoxCom.GetItemText(comboBoxCom.SelectedItem);
serialPort.PortName = selectedPort;
serialPort.Open();
if (serialPort.IsOpen)
{
serialPort.Write("9");
}
btnStatus();
buttonConnect.Text = "Disconnect";
}
private void disconnectFromArduino()
{
isConnected = false;
if (serialPort.IsOpen)
{
serialPort.Write("8");
}
serialPort.Close();
buttonConnect.Text = "Connect";
btnStatus();
}
private void buttonConnect_Click(object sender, EventArgs e)
{
try
{
if (!isConnected)
{
connectToArduino();
comboBoxCom.Enabled = false;
}
else
{
disconnectFromArduino();
comboBoxCom.Enabled = true;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "ERROR");
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort.IsOpen) serialPort.Close();
}
private void buttonOnLed_Click(object sender, EventArgs e)
{
if (isConnected)
{
serialPort.Write("1");
}
}
private void buttonOffLed_Click(object sender, EventArgs e)
{
if (isConnected)
{
serialPort.Write("0");
}
}
private void buttonGetComPort_Click(object sender, EventArgs e)
{
comboBoxCom.Items.Clear();
// Получаем список COM портов доступных в системе
string[] portnames = SerialPort.GetPortNames();
// Проверяем есть ли доступные
if (portnames.Length == 0)
{
buttonConnect.Enabled = false;
MessageBox.Show("COM PORT not found");
}
foreach (string portName in portnames)
{
//добавляем доступные COM порты в список
comboBoxCom.Items.Add(portName);
Console.WriteLine(portnames.Length);
if (portnames[0] != null)
{
comboBoxCom.SelectedItem = portnames[0];
buttonConnect.Enabled = true;
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
// var v=trackBar1.Value.ToString();
//label1.Text = v;
//serialPort.WriteLine(v);
}
private void button2_Click(object sender, EventArgs e)
{
if (isConnected)
{
serialPort.Write("2");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (isConnected)
{
serialPort.Write("3");
}
}
}
}
/*
//Скетч для приёма сообщений.
char commandValue; // данные, поступаемые с последовательного порта
int ledPinA = 3;
int ledPinB = 5;
String data="";
void setup() {
pinMode(ledPinA, OUTPUT); // режим на вывод данных
pinMode(ledPinB, OUTPUT); // режим на вывод данных
analogWrite(ledPinA,0);
analogWrite(ledPinB,255);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
commandValue = (char)Serial.read();
if(commandValue !='\n')
data +=commandValue;
else{
analogWrite(ledPinA,data.toInt());
analogWrite(ledPinB,255-data.toInt());
data="";
}
}
// if (commandValue == '1') {
// digitalWrite(ledPin, HIGH); // включаем светодиод
// }
// else {
// digitalWrite(ledPin, LOW); // в противном случае выключаем
// }
// delay(10); // задержка перед следующим чтением данных
}
*/