-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiigSerial.cs
255 lines (218 loc) · 8.06 KB
/
ConfiigSerial.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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;
namespace SmartCity
{
public partial class ConfigSerial : DevExpress.XtraEditors.XtraForm
{
public ConfigSerial()
{
try
{
InitializeComponent();
ScanSerialPortNumber();//扫描所有串口并列表
SP_Setup();//设置当前串口参数
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);
}
public ConfigSerial(string phoneno, string msg)
{
InitializeComponent();
ScanSerialPortNumber();//扫描所有串口并列表
SP_Setup();//设置当前串口参数
tB_PhNumber.Text = phoneno;
tB_Send.Text = msg;
//serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);
}
private void ScanSerialPortNumber()
{
string[] portNames = System.IO.Ports.SerialPort.GetPortNames();
for (int i = 0; i <= portNames.LongLength - 1; i++)
{
cb_SPNumber.Items.Add(portNames[i]);
}
cb_SPNumber.SelectedIndex = 0;//默认将显示COM1
cb_SPBaudRate.SelectedIndex = 6;//默认显示波特率为9600
cb_SPDataBits.SelectedIndex = 0;//默认显示数据位为8
cb_SPStopBits.SelectedIndex = 0;//默认显示停止位为1
cb_SPParity.SelectedIndex = 0;//默认显示校验位为NONE
cb_SPHandshake.SelectedIndex = 2;//默认显示流控制为NONE
}
private void SP_Setup()
{
serialPort1.Close();
serialPort1.PortName = cb_SPNumber.SelectedItem.ToString(); //设置串口号
serialPort1.BaudRate = Convert.ToInt16(cb_SPBaudRate.SelectedItem.ToString());//设置波特率
serialPort1.DataBits = Convert.ToInt16(cb_SPDataBits.SelectedItem.ToString());//设置数据位
switch (cb_SPStopBits.SelectedItem.ToString())//设置停止位
{
case "1":
serialPort1.StopBits = StopBits.One;
break;
case "1.5":
serialPort1.StopBits = StopBits.OnePointFive;
break;
case "2":
serialPort1.StopBits = StopBits.Two;
break;
}
switch (cb_SPHandshake.SelectedItem.ToString())//设置流控制
{
case "HARDWARE":
serialPort1.Handshake = Handshake.RequestToSend;
break;
case "SOFTWARE":
serialPort1.Handshake = Handshake.XOnXOff;
break;
case "NONE":
serialPort1.Handshake = Handshake.None;
break;
case "Custom":
serialPort1.Handshake = Handshake.RequestToSendXOnXOff;
break;
}
try
{
serialPort1.Open();
}
catch
{
MessageBox.Show(cb_SPNumber.SelectedItem.ToString() + "已经被占用,请重新选择端口", "警告");
return;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void SaveConfig()
{
ComConfig ds =new ComConfig();
ds.ReadXml("ComConfig.xsd");
if (ds.config.Rows.Count==0)
{
// rConfig
}
serialPort1.PortName = cb_SPNumber.SelectedItem.ToString(); //设置串口号
serialPort1.BaudRate = Convert.ToInt16(cb_SPBaudRate.SelectedItem.ToString());//设置波特率
serialPort1.DataBits = Convert.ToInt16(cb_SPDataBits.SelectedItem.ToString());//设置数据位
switch (cb_SPStopBits.SelectedItem.ToString())//设置停止位
{
case "1":
serialPort1.StopBits = StopBits.One;
break;
case "1.5":
serialPort1.StopBits = StopBits.OnePointFive;
break;
case "2":
serialPort1.StopBits = StopBits.Two;
break;
}
switch (cb_SPHandshake.SelectedItem.ToString())//设置流控制
{
case "HARDWARE":
serialPort1.Handshake = Handshake.RequestToSend;
break;
case "SOFTWARE":
serialPort1.Handshake = Handshake.XOnXOff;
break;
case "NONE":
serialPort1.Handshake = Handshake.None;
break;
case "Custom":
serialPort1.Handshake = Handshake.RequestToSendXOnXOff;
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
SP_Setup();
if (serialPort1.IsOpen)
{
MessageBox.Show("设置成功,当前端口为" + cb_SPNumber.SelectedItem.ToString(), "提示");
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.WriteLine("AT+CMGF=1\r");//开启文本短信
Delay(100);//100ms延时。目的是等待GPRS模块响应
serialPort1.WriteLine("AT+CMGS=" + "\"" + tB_PhNumber.Text + "\"\r");//输入手机号
Delay(100);
serialPort1.WriteLine(tB_Send.Text + "\r");//发送文本内容
Delay(100);
serialPort1.WriteLine("\x1A");//发送Ctrl-Z完成发送
Delay(100);
serialPort1.DiscardOutBuffer();//清串口输出缓存
MessageBox.Show("数据发送完毕!", "系统提示");
serialPort1.Close();
}
catch
{
MessageBox.Show("端口设置异常,请重新设置端口", "警告");
}
}
private void tB_Take_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
//private void button3_Click_1(object sender, EventArgs e)
//{
// tB_Take.Text = serialPort1.ReadLine();
// serialPort1.DiscardInBuffer();
//}
private void button4_Click(object sender, EventArgs e)
{
//serialPort1.WriteLine("\x1A");
//serialPort1.DiscardOutBuffer();
//tB_Take.Text=DataReceived
}
private void label9_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
//延时
private void Delay(int mm)
{
DateTime current = DateTime.Now;
while (current.AddMilliseconds(mm) > DateTime.Now)
{
Application.DoEvents();
}
return;
}
//将接收的数据显示到tB_Take中。
//void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
//{
// MessageBox.Show("ok");
// string serialReadString;
// serialReadString = serialPort1.ReadExisting();
// this.tB_Take.Invoke(new MethodInvoker(delegate
// {
// this.tB_Take.AppendText(serialReadString);
// }
// )
// );
//}
}
}