-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
209 lines (191 loc) · 6.47 KB
/
Form1.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.IO;
using System.Collections;
namespace SSCOM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.btnSendData.Enabled = false;
this.SetCode.Enabled = false;
this.SetIP.Enabled = false;
this.WifiConnect.Enabled = false;
}
public static string strPortName = "";
public static string strBaudRate = "";
public static string strDataBits = "";
public static string strStopBits = "";
SerialPort sp = new SerialPort();
private void btnSetSP_Click(object sender, EventArgs e)
{
sp.Close();
ComSet dlg = new ComSet();
if (dlg.ShowDialog() == DialogResult.OK)
{
sp.PortName = strPortName;
sp.BaudRate = int.Parse(strBaudRate);
sp.DataBits = int.Parse(strDataBits);
sp.StopBits = (StopBits)int.Parse(strStopBits);
sp.ReadTimeout = 500;
}
}
private void btnSwitchSP_Click(object sender, EventArgs e)
{
if (btnSwitchSP.Text == "打开串口")
{
if (strPortName != "" && strBaudRate != "" && strDataBits != "" && strStopBits != "")
{
try
{
if (sp.IsOpen)
{
sp.Close();
sp.Open(); //打开串口
timer2.Enabled = true;
}
else
{
sp.Open();//打开串口
timer2.Enabled = true;
}
btnSwitchSP.Text = "关闭串口";
btnSetSP.Enabled = false;
}
catch
{
}
this.btnSendData.Enabled = true;
this.SetCode.Enabled = true;
this.SetIP.Enabled = true;
this.WifiConnect.Enabled = true;
}
else
{
MessageBox.Show("请先设置串口!", "RS232串口通信");
}
}
else
{
this.btnSendData.Enabled = false;
this.SetCode.Enabled = false;
this.SetIP.Enabled = false;
this.WifiConnect.Enabled = false;
timer2.Enabled = false;
btnSetSP.Enabled = true;
btnSwitchSP.Text = "打开串口";
sp.Close();
}
}
private void btnSendData_Click(object sender, EventArgs e)
{
string wifiname = ":FA" + txtSend.Text + "#";
if (sp.IsOpen)
{
try
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
sp.Write(wifiname);
}
catch (Exception ex)//若应用程序出现调试问题
{
MessageBox.Show("错误:" + ex.Message);
}
}
else
{
MessageBox.Show("请先打开串口!");
}
}
private void timer2_Tick(object sender, EventArgs e)
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
string str = sp.ReadExisting();
string str4 = str.Replace("\r", "\r\n");
textBox1.AppendText(str4);
textBox1.ScrollToCaret();
}
private void btnReceiveData_Click(object sender, EventArgs e)
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
if (sp.IsOpen)
{
timer2.Enabled = true;
}
else
{
MessageBox.Show("请先打开串口!");
}
}
private void SetCode_Click(object sender, EventArgs e)
{
string wificode = ":FC" + textCode.Text + "#";
if (sp.IsOpen)
{
try
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
sp.Write(wificode);
}
catch (Exception ex)//若应用程序出现调试问题
{
MessageBox.Show("错误:" + ex.Message);
}
}
else
{
MessageBox.Show("请先打开串口!");
}
}
private void SetIP_Click(object sender, EventArgs e)
{
string serverIp = ":FW" + textip.Text + "#";
if (sp.IsOpen)
{
try
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
sp.Write(serverIp);
}
catch (Exception ex)//若应用程序出现调试问题
{
MessageBox.Show("错误:" + ex.Message);
}
}
else
{
MessageBox.Show("请先打开串口!");
}
}
private void WifiConnect_Click(object sender, EventArgs e)
{
string WifiCon = ":FY#";
if (sp.IsOpen)
{
try
{
sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");//GB2312即信息交换用汉字编码字符集
sp.Write(WifiCon);
}
catch (Exception ex)//若应用程序出现调试问题
{
MessageBox.Show("错误:" + ex.Message);
}
}
else
{
MessageBox.Show("请先打开串口!");
}
}
}
}