質問編集履歴

3

説明の改善

2017/11/28 06:12

投稿

P5_USER
P5_USER

スコア73

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,14 @@
16
16
 
17
17
  目標②:updateTime()での値更新。
18
18
 
19
+
20
+
21
+ 今回C#にさせる役割はデータの仲介です。
22
+
23
+ ![通信の全体図](d6dc4ae9e30e20308f918cc2481f0afb.png)
24
+
25
+
26
+
19
27
  ###発生している問題・エラーメッセージ
20
28
 
21
29
  変数の値(A_now,B_now,C_now,D_now,A_before,B_before,C_before,D_before)

2

C#側(UDP送信側)のソースコードを記載しました。

2017/11/28 06:12

投稿

P5_USER
P5_USER

スコア73

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,227 @@
34
34
 
35
35
  ###該当のソースコード
36
36
 
37
+ ```C#
38
+
39
+ using System;
40
+
41
+ using System.IO;
42
+
43
+ using System.Collections.Generic;
44
+
45
+ using System.Linq;
46
+
47
+ using System.Text;
48
+
49
+ using System.Threading.Tasks;
50
+
51
+ using System.Runtime.InteropServices;
52
+
53
+
54
+
55
+ namespace test4_P5.ver
56
+
57
+ {
58
+
59
+ [StructLayout(LayoutKind.Sequential)]
60
+
61
+ struct STRUCTURE
62
+
63
+ {
64
+
65
+ public double AA;
66
+
67
+ public double BB;
68
+
69
+ public double CC;
70
+
71
+ public double D1;
72
+
73
+ public double D2;
74
+
75
+ };
76
+
77
+
78
+
79
+ class Program
80
+
81
+ {
82
+
83
+ static void Main(string[] args)
84
+
85
+ {
86
+
87
+ System.String name;
88
+
89
+ DateTime dt = System.DateTime.Now;
90
+
91
+ name = @"C:\Users\hogehoge\Desktop\フォルダ\" + dt.ToString("tthh時mm分ss秒") + ".csv";
92
+
93
+ System.IO.StreamWriter w = new System.IO.StreamWriter(name, false);
94
+
95
+ System.Threading.Thread.Sleep(5000);
96
+
97
+ w.Write("time,AA,AA,BB,BB,CC,CC,D1,D2,DD\n");
98
+
99
+
100
+
101
+ //バインドするローカルIPとポート番号
102
+
103
+ string localIpString = "〇.〇.〇.〇";
104
+
105
+ //string localIpString = "127.0.0.1";
106
+
107
+
108
+
109
+ System.Net.IPAddress localAddress =
110
+
111
+ System.Net.IPAddress.Parse(localIpString);
112
+
113
+ int localPort = 12000;
114
+
115
+
116
+
117
+ //UdpClientを作成し、ローカルエンドポイントにバインドする
118
+
119
+ System.Net.IPEndPoint localEP =
120
+
121
+ new System.Net.IPEndPoint(localAddress, localPort);
122
+
123
+ System.Net.Sockets.UdpClient udp =
124
+
125
+ new System.Net.Sockets.UdpClient(localEP);
126
+
127
+
128
+
129
+ //データを送信するリモートホストとポート番号
130
+
131
+ string remoteHost = "172.0.0.1";
132
+
133
+ int remotePort = 60000;
134
+
135
+
136
+
137
+ //UdpCliantオブジェクトを作成する。
138
+
139
+ System.Net.Sockets.UdpClient client =
140
+
141
+ new System.Net.Sockets.UdpClient();
142
+
143
+
144
+
145
+ DateTime startdt = System.DateTime.Now;
146
+
147
+
148
+
149
+ //Processingにデータを渡すための前処理
150
+
151
+ double AA, BB, CC, DD;
152
+
153
+
154
+
155
+ for (; ; )
156
+
157
+ {
158
+
159
+ //データを受信する
160
+
161
+ System.Net.IPEndPoint remoteEP = null;
162
+
163
+ byte[] rcvBytes = udp.Receive(ref remoteEP);
164
+
165
+ //データを文字列に変換する
166
+
167
+ FSSM rcvMsg = new STRUCTURE();
168
+
169
+ //Marshal.PtrToStructure(rcvBytes, typeof(STRUCTURE));
170
+
171
+
172
+
173
+ //string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
174
+
175
+
176
+
177
+ //↓[追加]バイト配列を構造体にコピーする。
178
+
179
+ IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rcvMsg));
180
+
181
+
182
+
183
+ Marshal.Copy(rcvBytes, 0, ptr, Marshal.SizeOf(rcvMsg));
184
+
185
+ rcvMsg = (STRUCTURE)Marshal.PtrToStructure(ptr, typeof(STRUCTURE));
186
+
187
+ Console.WriteLine(Marshal.SizeOf(rcvMsg));
188
+
189
+ Marshal.FreeHGlobal(ptr);
190
+
191
+
192
+
193
+ //送信するデータを作成する
194
+
195
+ AA = rcvMsg.AA;
196
+
197
+ BB = rcvMsg.BB;
198
+
199
+ CC = rcvMsg.CC;
200
+
201
+ DD = ((rcvMsg.D1 + rcvMsg.D2) / 2);
202
+
203
+
204
+
205
+ string sendMsg = AA + " " + BB + " " + CC + " " + DD;
206
+
207
+ byte[] sendBytes = System.Text.Encoding.UTF8.GetBytes(sendMsg);
208
+
209
+
210
+
211
+ //リモートホストを指定してデータを転送する
212
+
213
+ client.Send(sendBytes, sendBytes.Length, remoteHost, remotePort);
214
+
215
+
216
+
217
+ //"exit"を受信したら終了
218
+
219
+ if (rcvMsg.Equals("exit"))
220
+
221
+ {
222
+
223
+ w.Close();
224
+
225
+ break;
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ //UdpClientを閉じる
234
+
235
+ udp.Close();
236
+
237
+ Console.WriteLine("終了しました。");
238
+
239
+ Console.ReadLine();
240
+
241
+ w.Close();
242
+
243
+
244
+
245
+ client.Close();
246
+
247
+ }
248
+
249
+ }
250
+
251
+ }
252
+
253
+
254
+
255
+ ```
256
+
37
- ```Processing
257
+ ```processing
38
258
 
39
259
  import hypermedia.net.*; //<>//
40
260
 

1

誤字修正

2017/11/28 05:51

投稿

P5_USER
P5_USER

スコア73

test CHANGED
File without changes
test CHANGED
@@ -182,7 +182,7 @@
182
182
 
183
183
  ###試したこと
184
184
 
185
- デバッグモードでの変数確認や先帆の記載したBashoでのリアルタイム変数遷移の確認。
185
+ デバッグモードでの変数確認や先記載したBashoでのリアルタイム変数遷移の確認。
186
186
 
187
187
  ↑でもやっぱり値が変わらず...
188
188