質問するログイン新規登録

質問編集履歴

2

コード全文を記載

2020/09/24 04:55

投稿

tme_ike
tme_ike

スコア1

title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,34 @@
5
5
  受信イベントが発生しません。
6
6
 
7
7
  ### 該当のソースコード
8
- 設定と受信イベント部
8
+ Form1.cs
9
9
  ```C#
10
+ using System;
11
+ using System.Collections.Generic;
12
+ using System.ComponentModel;
13
+ using System.Data;
14
+ using System.Drawing;
15
+ using System.IO.Ports;
16
+ using System.Linq;
17
+ using System.Text;
18
+ using System.Threading.Tasks;
19
+ using System.Windows.Forms;
10
20
 
21
+ namespace _232CTest
22
+ {
23
+ public partial class Form1 : Form
24
+ {
25
+ string[] ports;
26
+ string DELIMITER = "\r\n";
27
+ public Form1()
28
+ {
29
+ InitializeComponent();
30
+ }
31
+
32
+ private void Form1_Load(object sender, EventArgs e)
33
+ {
34
+ ports = SerialPort.GetPortNames();
35
+ serialPort1.PortName = "COM4";
11
36
  serialPort1.BaudRate = 9600;
12
37
  serialPort1.DataBits = 8;
13
38
  serialPort1.StopBits = System.IO.Ports.StopBits.One;
@@ -16,7 +41,23 @@
16
41
  serialPort1.RtsEnable = true;
17
42
  serialPort1.Handshake = Handshake.None;
18
43
  serialPort1.ReceivedBytesThreshold = 1;
44
+
45
+ try
46
+ {
47
+ if(serialPort1.IsOpen == true) { return; }
48
+ serialPort1.Open();
49
+ }
50
+ catch(Exception ex) { MessageBox.Show(ex.Message); }
51
+
52
+ if(serialPort1.IsOpen == true)
53
+ {
54
+ serialPort1.NewLine = DELIMITER;
55
+ try { serialPort1.WriteLine("sousin"); }
56
+ catch (Exception ex) { MessageBox.Show(ex.Message); }
57
+ }
58
+ }
59
+
19
- private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
60
+ private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
20
61
  {
21
62
  string strDataReceived = "";
22
63
  try { strDataReceived = serialPort1.ReadExisting(); }
@@ -28,8 +69,88 @@
28
69
  MessageBox.Show(strDataReceived);
29
70
  }
30
71
  }
72
+ private void SerialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
73
+ {
74
+ MessageBox.Show("error");
75
+ }
76
+
77
+ private void button1_Click(object sender, EventArgs e)
78
+ {
79
+ MessageBox.Show(serialPort1.ReadExisting());
80
+ }
81
+ }
82
+ }
83
+
31
84
  ```
85
+ Form1.Designercs
86
+ ```C#
87
+ namespace _232CTest
88
+ {
89
+ partial class Form1
90
+ {
91
+ /// <summary>
92
+ /// 必要なデザイナー変数です。
93
+ /// </summary>
94
+ private System.ComponentModel.IContainer components = null;
32
95
 
96
+ /// <summary>
97
+ /// 使用中のリソースをすべてクリーンアップします。
98
+ /// </summary>
99
+ /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
100
+ protected override void Dispose(bool disposing)
101
+ {
102
+ if (disposing && (components != null))
103
+ {
104
+ components.Dispose();
105
+ }
106
+ base.Dispose(disposing);
107
+ }
108
+
109
+ #region Windows フォーム デザイナーで生成されたコード
110
+
111
+ /// <summary>
112
+ /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
113
+ /// コード エディターで変更しないでください。
114
+ /// </summary>
115
+ private void InitializeComponent()
116
+ {
117
+ this.components = new System.ComponentModel.Container();
118
+ this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
119
+ this.button1 = new System.Windows.Forms.Button();
120
+ this.SuspendLayout();
121
+ //
122
+ // button1
123
+ //
124
+ this.button1.Location = new System.Drawing.Point(111, 66);
125
+ this.button1.Name = "button1";
126
+ this.button1.Size = new System.Drawing.Size(88, 60);
127
+ this.button1.TabIndex = 0;
128
+ this.button1.Text = "button1";
129
+ this.button1.UseVisualStyleBackColor = true;
130
+ this.button1.Click += new System.EventHandler(this.button1_Click);
131
+ //
132
+ // Form1
133
+ //
134
+ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
135
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
136
+ this.ClientSize = new System.Drawing.Size(800, 450);
137
+ this.Controls.Add(this.button1);
138
+ this.Name = "Form1";
139
+ this.Text = "Form1";
140
+ this.Load += new System.EventHandler(this.Form1_Load);
141
+ this.ResumeLayout(false);
142
+
143
+ }
144
+
145
+ #endregion
146
+
147
+ private System.IO.Ports.SerialPort serialPort1;
148
+ private System.Windows.Forms.Button button1;
149
+ }
150
+ }
151
+
152
+
153
+ ```
33
154
  ### 試したこと
34
155
 
35
156
  別のPCのSeristerというソフトとUSBシリアルを使い通信確認。

1

コードを挿入に修正

2020/09/24 04:55

投稿

tme_ike
tme_ike

スコア1

title CHANGED
File without changes
body CHANGED
@@ -6,28 +6,29 @@
6
6
 
7
7
  ### 該当のソースコード
8
8
  設定と受信イベント部
9
+ ```C#
9
10
 
10
-  serialPort1.PortName = "COM4";
11
-  serialPort1.BaudRate = 9600;
11
+ serialPort1.BaudRate = 9600;
12
-  serialPort1.DataBits = 8;
12
+ serialPort1.DataBits = 8;
13
-  serialPort1.StopBits = System.IO.Ports.StopBits.One;
13
+ serialPort1.StopBits = System.IO.Ports.StopBits.One;
14
-  serialPort1.Parity = System.IO.Ports.Parity.None;
14
+ serialPort1.Parity = System.IO.Ports.Parity.None;
15
-  serialPort1.DtrEnable = true;
15
+ serialPort1.DtrEnable = true;
16
-  serialPort1.RtsEnable = true;
16
+ serialPort1.RtsEnable = true;
17
-  serialPort1.Handshake = Handshake.None;
17
+ serialPort1.Handshake = Handshake.None;
18
-  serialPort1.ReceivedBytesThreshold = 1;
18
+ serialPort1.ReceivedBytesThreshold = 1;
19
+ private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
20
+ {
21
+ string strDataReceived = "";
22
+ try { strDataReceived = serialPort1.ReadExisting(); }
23
+ catch (Exception ex) { MessageBox.Show(ex.Message); }
19
24
 
20
-  private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
25
+ if(serialPort1.IsOpen == true)
21
-  {
26
+ {
22
-   string strDataReceived = "";
23
-   try { strDataReceived = serialPort1.ReadExisting(); }
24
-   catch (Exception ex) { MessageBox.Show(ex.Message); }
25
-   if(serialPort1.IsOpen == true)
26
-   {
27
-    serialPort1.Close();
27
+ serialPort1.Close();
28
-    MessageBox.Show(strDataReceived);
28
+ MessageBox.Show(strDataReceived);
29
-   }
29
+ }
30
-  }
30
+ }
31
+ ```
31
32
 
32
33
  ### 試したこと
33
34