質問編集履歴
2
コード全文を記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,11 +12,61 @@
|
|
12
12
|
|
13
13
|
### 該当のソースコード
|
14
14
|
|
15
|
-
|
15
|
+
Form1.cs
|
16
16
|
|
17
17
|
```C#
|
18
18
|
|
19
|
-
|
19
|
+
using System;
|
20
|
+
|
21
|
+
using System.Collections.Generic;
|
22
|
+
|
23
|
+
using System.ComponentModel;
|
24
|
+
|
25
|
+
using System.Data;
|
26
|
+
|
27
|
+
using System.Drawing;
|
28
|
+
|
29
|
+
using System.IO.Ports;
|
30
|
+
|
31
|
+
using System.Linq;
|
32
|
+
|
33
|
+
using System.Text;
|
34
|
+
|
35
|
+
using System.Threading.Tasks;
|
36
|
+
|
37
|
+
using System.Windows.Forms;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
namespace _232CTest
|
42
|
+
|
43
|
+
{
|
44
|
+
|
45
|
+
public partial class Form1 : Form
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
string[] ports;
|
50
|
+
|
51
|
+
string DELIMITER = "\r\n";
|
52
|
+
|
53
|
+
public Form1()
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
InitializeComponent();
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
private void Form1_Load(object sender, EventArgs e)
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
ports = SerialPort.GetPortNames();
|
68
|
+
|
69
|
+
serialPort1.PortName = "COM4";
|
20
70
|
|
21
71
|
serialPort1.BaudRate = 9600;
|
22
72
|
|
@@ -34,7 +84,39 @@
|
|
34
84
|
|
35
85
|
serialPort1.ReceivedBytesThreshold = 1;
|
36
86
|
|
87
|
+
|
88
|
+
|
89
|
+
try
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
if(serialPort1.IsOpen == true) { return; }
|
94
|
+
|
95
|
+
serialPort1.Open();
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
catch(Exception ex) { MessageBox.Show(ex.Message); }
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
if(serialPort1.IsOpen == true)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
serialPort1.NewLine = DELIMITER;
|
108
|
+
|
109
|
+
try { serialPort1.WriteLine("sousin"); }
|
110
|
+
|
111
|
+
catch (Exception ex) { MessageBox.Show(ex.Message); }
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
37
|
-
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
119
|
+
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
38
120
|
|
39
121
|
{
|
40
122
|
|
@@ -58,9 +140,169 @@
|
|
58
140
|
|
59
141
|
}
|
60
142
|
|
143
|
+
private void SerialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
MessageBox.Show("error");
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
private void button1_Click(object sender, EventArgs e)
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
MessageBox.Show(serialPort1.ReadExisting());
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
61
167
|
```
|
62
168
|
|
63
|
-
|
169
|
+
Form1.Designercs
|
170
|
+
|
171
|
+
```C#
|
172
|
+
|
173
|
+
namespace _232CTest
|
174
|
+
|
175
|
+
{
|
176
|
+
|
177
|
+
partial class Form1
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
/// <summary>
|
182
|
+
|
183
|
+
/// 必要なデザイナー変数です。
|
184
|
+
|
185
|
+
/// </summary>
|
186
|
+
|
187
|
+
private System.ComponentModel.IContainer components = null;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
/// <summary>
|
192
|
+
|
193
|
+
/// 使用中のリソースをすべてクリーンアップします。
|
194
|
+
|
195
|
+
/// </summary>
|
196
|
+
|
197
|
+
/// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
|
198
|
+
|
199
|
+
protected override void Dispose(bool disposing)
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
if (disposing && (components != null))
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
components.Dispose();
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
base.Dispose(disposing);
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
#region Windows フォーム デザイナーで生成されたコード
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
/// <summary>
|
222
|
+
|
223
|
+
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
|
224
|
+
|
225
|
+
/// コード エディターで変更しないでください。
|
226
|
+
|
227
|
+
/// </summary>
|
228
|
+
|
229
|
+
private void InitializeComponent()
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
this.components = new System.ComponentModel.Container();
|
234
|
+
|
235
|
+
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
|
236
|
+
|
237
|
+
this.button1 = new System.Windows.Forms.Button();
|
238
|
+
|
239
|
+
this.SuspendLayout();
|
240
|
+
|
241
|
+
//
|
242
|
+
|
243
|
+
// button1
|
244
|
+
|
245
|
+
//
|
246
|
+
|
247
|
+
this.button1.Location = new System.Drawing.Point(111, 66);
|
248
|
+
|
249
|
+
this.button1.Name = "button1";
|
250
|
+
|
251
|
+
this.button1.Size = new System.Drawing.Size(88, 60);
|
252
|
+
|
253
|
+
this.button1.TabIndex = 0;
|
254
|
+
|
255
|
+
this.button1.Text = "button1";
|
256
|
+
|
257
|
+
this.button1.UseVisualStyleBackColor = true;
|
258
|
+
|
259
|
+
this.button1.Click += new System.EventHandler(this.button1_Click);
|
260
|
+
|
261
|
+
//
|
262
|
+
|
263
|
+
// Form1
|
264
|
+
|
265
|
+
//
|
266
|
+
|
267
|
+
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
|
268
|
+
|
269
|
+
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
270
|
+
|
271
|
+
this.ClientSize = new System.Drawing.Size(800, 450);
|
272
|
+
|
273
|
+
this.Controls.Add(this.button1);
|
274
|
+
|
275
|
+
this.Name = "Form1";
|
276
|
+
|
277
|
+
this.Text = "Form1";
|
278
|
+
|
279
|
+
this.Load += new System.EventHandler(this.Form1_Load);
|
280
|
+
|
281
|
+
this.ResumeLayout(false);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
#endregion
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
private System.IO.Ports.SerialPort serialPort1;
|
294
|
+
|
295
|
+
private System.Windows.Forms.Button button1;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
```
|
64
306
|
|
65
307
|
### 試したこと
|
66
308
|
|
1
コードを挿入に修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,49 +14,51 @@
|
|
14
14
|
|
15
15
|
設定と受信イベント部
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
serialPort1.PortName = "COM4";
|
20
|
-
|
21
|
-
serialPort1.BaudRate = 9600;
|
22
|
-
|
23
|
-
|
17
|
+
```C#
|
24
|
-
|
25
|
-
serialPort1.StopBits = System.IO.Ports.StopBits.One;
|
26
|
-
|
27
|
-
serialPort1.Parity = System.IO.Ports.Parity.None;
|
28
|
-
|
29
|
-
serialPort1.DtrEnable = true;
|
30
|
-
|
31
|
-
serialPort1.RtsEnable = true;
|
32
|
-
|
33
|
-
serialPort1.Handshake = Handshake.None;
|
34
|
-
|
35
|
-
serialPort1.ReceivedBytesThreshold = 1;
|
36
18
|
|
37
19
|
|
38
20
|
|
39
|
-
|
21
|
+
serialPort1.BaudRate = 9600;
|
40
22
|
|
41
|
-
|
23
|
+
serialPort1.DataBits = 8;
|
42
24
|
|
43
|
-
|
25
|
+
serialPort1.StopBits = System.IO.Ports.StopBits.One;
|
44
26
|
|
45
|
-
|
27
|
+
serialPort1.Parity = System.IO.Ports.Parity.None;
|
46
28
|
|
47
|
-
|
29
|
+
serialPort1.DtrEnable = true;
|
48
30
|
|
49
|
-
|
31
|
+
serialPort1.RtsEnable = true;
|
50
32
|
|
51
|
-
|
33
|
+
serialPort1.Handshake = Handshake.None;
|
52
34
|
|
53
|
-
|
35
|
+
serialPort1.ReceivedBytesThreshold = 1;
|
54
36
|
|
55
|
-
|
37
|
+
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
56
38
|
|
57
|
-
|
39
|
+
{
|
58
40
|
|
41
|
+
string strDataReceived = "";
|
42
|
+
|
43
|
+
try { strDataReceived = serialPort1.ReadExisting(); }
|
44
|
+
|
45
|
+
catch (Exception ex) { MessageBox.Show(ex.Message); }
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
if(serialPort1.IsOpen == true)
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
serialPort1.Close();
|
54
|
+
|
55
|
+
MessageBox.Show(strDataReceived);
|
56
|
+
|
59
|
-
|
57
|
+
}
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
```
|
60
62
|
|
61
63
|
|
62
64
|
|