-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmMoreSerial.cs
More file actions
154 lines (136 loc) · 5.08 KB
/
FrmMoreSerial.cs
File metadata and controls
154 lines (136 loc) · 5.08 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SerialPortForward
{
public partial class FrmMoreSerial : Form
{
public FrmMoreSerial()
{
InitializeComponent();
}
SerialPortInfo com1, com2;
public FrmMoreSerial(ref SerialPortInfo com1, ref SerialPortInfo com2)
{
InitializeComponent();
this.com1 = com1;
this.com2 = com2;
ShowPort(com1, cmbCom1Stop, cmbCom1Data, cmbCom1Parity, cbCom1DTR, cbCom1RTS, nudCom1Timer, nudCom1TimeOut, txtCom1IP, nudCom1Port);
ShowPort(com2, cmbCom2Stop, cmbCom2Data, cmbCom2Parity, cbCom2DTR, cbCom2RTS, nudCom2Timer, nudCom2TimeOut, txtCom2IP, nudCom2Port);
}
private void FrmMoreSerial_Load(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
SavePort(com1, cmbCom1Stop, cmbCom1Data, cmbCom1Parity, cbCom1DTR, cbCom1RTS, nudCom1Timer, nudCom1TimeOut, txtCom1IP, nudCom1Port);
SavePort(com2, cmbCom2Stop, cmbCom2Data, cmbCom2Parity, cbCom2DTR, cbCom2RTS, nudCom2Timer, nudCom2TimeOut, txtCom2IP, nudCom2Port);
DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
void ShowPort(SerialPortInfo sp, ComboBox cmbStop, ComboBox cmbData, ComboBox cmbParity, CheckBox cbDTR, CheckBox cbRTS, NumericUpDown nudTimer, NumericUpDown nudTimeOut, TextBox txtIP, NumericUpDown nudPort)
{
switch (sp.StopBits)
{
case StopBits.None:
break;
case StopBits.One:
cmbStop.SelectedIndex = 0;
break;
case StopBits.Two:
cmbStop.SelectedIndex = 2;
break;
case StopBits.OnePointFive:
cmbStop.SelectedIndex = 1;
break;
default:
break;
}
switch (sp.DataBits)
{
case 8: cmbData.SelectedIndex = 0; break;
case 7: cmbData.SelectedIndex = 1; break;
case 6: cmbData.SelectedIndex = 2; break;
case 5:
default:
cmbData.SelectedIndex = 3; break;
}
switch (sp.Parity)
{
case Parity.None:
cmbParity.SelectedIndex = 0;
break;
case Parity.Odd:
cmbParity.SelectedIndex = 1;
break;
case Parity.Even:
cmbParity.SelectedIndex = 2;
break;
case Parity.Mark:
cmbParity.SelectedIndex = 3;
break;
case Parity.Space:
cmbParity.SelectedIndex = 4;
break;
default:
break;
}
cbDTR.Checked = sp.DtrEnable;
cbRTS.Checked = sp.RtsEnable;
nudTimer.Value = sp.Timer;
nudTimeOut.Value = sp.TimeOut;
txtIP.Text = sp.IP.ToString();
nudPort.Value = sp.Port;
}
void SavePort(SerialPortInfo sp, ComboBox cmbStop, ComboBox cmbData, ComboBox cmbParity, CheckBox cbDTR, CheckBox cbRTS, NumericUpDown nudTimer, NumericUpDown nudTimeOut, TextBox txtIP, NumericUpDown nudPort)
{
switch (cmbStop.SelectedIndex)
{
case 1: sp.StopBits = StopBits.OnePointFive; break;
case 2: sp.StopBits = StopBits.Two; break;
case 0:
default:
sp.StopBits = StopBits.One; break;
}
switch (cmbData.SelectedIndex)
{
case 1: sp.DataBits = 7; break;
case 2: sp.DataBits = 6; break;
case 3: sp.DataBits = 5; break;
case 0:
default:
sp.DataBits = 8; break;
}
switch (cmbParity.SelectedIndex)
{
case 1: sp.Parity = Parity.Odd; break;
case 2: sp.Parity = Parity.Even; break;
case 3: sp.Parity = Parity.Mark; break;
case 4: sp.Parity = Parity.Space; break;
case 0:
default:
sp.Parity = Parity.None; break;
}
sp.DtrEnable = cbDTR.Checked;
sp.RtsEnable = cbRTS.Checked;
sp.Timer = (int)nudTimer.Value;
sp.TimeOut = (int)nudTimeOut.Value;
sp.IP = IPAddress.Parse(txtIP.Text);
sp.Port = (int)nudPort.Value;
}
}
}