-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormMain.cs
More file actions
95 lines (80 loc) · 2.72 KB
/
FormMain.cs
File metadata and controls
95 lines (80 loc) · 2.72 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
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 SerialConnect
{
public partial class FormMain : Form
{
static string StatusText;
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach(string port in ports)
{
comboBoxSerialPort.Items.Add(port);
}
comboBoxSerialSpeed.SelectedIndex = 11;
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if(comboBoxSerialPort.Text.Trim().Length == 0)
{
MessageBox.Show("Select a port and try again", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if(comboBoxSerialSpeed.Text.Trim().Length == 0)
{
MessageBox.Show("Select port connect speed and try again", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
serialPort1.PortName = comboBoxSerialPort.Text.Trim();
serialPort1.BaudRate = Convert.ToInt32(comboBoxSerialSpeed.Text);
serialPort1.Open();
if(serialPort1.IsOpen)
{
buttonConnect.Enabled = false;
buttonDisconnect.Enabled = true;
}
}
private void WriteStatusLine(string text)
{
textBoxStatus.AppendText(String.Format("{0}\r\n", text));
}
private void DisplayThreadText(object sender, EventArgs e)
{
WriteStatusLine(StatusText);
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
StatusText = serialPort1.ReadExisting();
Invoke(new EventHandler(DisplayThreadText));
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen)
{
serialPort1.Close();
buttonDisconnect.Enabled = false;
buttonConnect.Enabled = true;
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen && textBoxDataSend.Text.Length > 0)
{
serialPort1.WriteLine(textBoxDataSend.Text);
}
}
}
}