###前提・実現したいこと
前提条件①:C#にて4つのdouble型変数をスペース区切りでstring型変数にしている
前提条件②:そのstringをUTF8でエンコードしてbyte[]としている。
前提条件③:UDPでbyte[]を送信している(ここまではC#の出来事)
前提条件④:以下URLを参考にprocessingでbyte[]を受け取っている(はず)。
URL:http://memorandums.hatenablog.com/entry/2016/11/08/203610
目標①:受け取ったバイト配列をスペース区切りで4つに分割してprocessing側で新規に作成したdouble型変数(ここではA_nowとか)に格納する。
目標②:updateTime()での値更新。
###発生している問題・エラーメッセージ
変数の値(A_now,B_now,C_now,D_now,A_before,B_before,C_before,D_before)
が変わっていない
ずっと0.0のまま
Bashoで確認
Bashoについて:https://www.youtube.com/watch?v=8Z23xNN7IGU
###該当のソースコード
C#
1using System; 2using System.IO; 3using System.Collections.Generic; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7using System.Runtime.InteropServices; 8 9namespace test4_P5.ver 10{ 11 [StructLayout(LayoutKind.Sequential)] 12 struct STRUCTURE 13 { 14 public double AA; 15 public double BB; 16 public double CC; 17 public double D1; 18 public double D2; 19 }; 20 21 class Program 22 { 23 static void Main(string[] args) 24 { 25 System.String name; 26 DateTime dt = System.DateTime.Now; 27 name = @"C:\Users\hogehoge\Desktop\フォルダ\" + dt.ToString("tthh時mm分ss秒") + ".csv"; 28 System.IO.StreamWriter w = new System.IO.StreamWriter(name, false); 29 System.Threading.Thread.Sleep(5000); 30 w.Write("time,AA,AA,BB,BB,CC,CC,D1,D2,DD\n"); 31 32 //バインドするローカルIPとポート番号 33 string localIpString = "〇.〇.〇.〇"; 34 //string localIpString = "127.0.0.1"; 35 36 System.Net.IPAddress localAddress = 37 System.Net.IPAddress.Parse(localIpString); 38 int localPort = 12000; 39 40 //UdpClientを作成し、ローカルエンドポイントにバインドする 41 System.Net.IPEndPoint localEP = 42 new System.Net.IPEndPoint(localAddress, localPort); 43 System.Net.Sockets.UdpClient udp = 44 new System.Net.Sockets.UdpClient(localEP); 45 46 //データを送信するリモートホストとポート番号 47 string remoteHost = "172.0.0.1"; 48 int remotePort = 60000; 49 50 //UdpCliantオブジェクトを作成する。 51 System.Net.Sockets.UdpClient client = 52 new System.Net.Sockets.UdpClient(); 53 54 DateTime startdt = System.DateTime.Now; 55 56 //Processingにデータを渡すための前処理 57 double AA, BB, CC, DD; 58 59 for (; ; ) 60 { 61 //データを受信する 62 System.Net.IPEndPoint remoteEP = null; 63 byte[] rcvBytes = udp.Receive(ref remoteEP); 64 //データを文字列に変換する 65 FSSM rcvMsg = new STRUCTURE(); 66 //Marshal.PtrToStructure(rcvBytes, typeof(STRUCTURE)); 67 68 //string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes); 69 70 //↓[追加]バイト配列を構造体にコピーする。 71 IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rcvMsg)); 72 73 Marshal.Copy(rcvBytes, 0, ptr, Marshal.SizeOf(rcvMsg)); 74 rcvMsg = (STRUCTURE)Marshal.PtrToStructure(ptr, typeof(STRUCTURE)); 75 Console.WriteLine(Marshal.SizeOf(rcvMsg)); 76 Marshal.FreeHGlobal(ptr); 77 78 //送信するデータを作成する 79 AA = rcvMsg.AA; 80 BB = rcvMsg.BB; 81 CC = rcvMsg.CC; 82 DD = ((rcvMsg.D1 + rcvMsg.D2) / 2); 83 84 string sendMsg = AA + " " + BB + " " + CC + " " + DD; 85 byte[] sendBytes = System.Text.Encoding.UTF8.GetBytes(sendMsg); 86 87 //リモートホストを指定してデータを転送する 88 client.Send(sendBytes, sendBytes.Length, remoteHost, remotePort); 89 90 //"exit"を受信したら終了 91 if (rcvMsg.Equals("exit")) 92 { 93 w.Close(); 94 break; 95 } 96 } 97 98 //UdpClientを閉じる 99 udp.Close(); 100 Console.WriteLine("終了しました。"); 101 Console.ReadLine(); 102 w.Close(); 103 104 client.Close(); 105 } 106 } 107} 108
processing
1import hypermedia.net.*; //<>// 2UDP udp; 3//Basho basho; 4//Double_Absolute D_ABS; 5 6int dW, dH, dW30, dW15, dW10, dH30, dH15, dH10 ; 7 8//現在値 9double A_now = 0.0; 10double B_now = 0.0; 11double C_now = 0.0; 12double D_now = 0.0; 13 14//1フレーム前の値 15double A_before; 16double B_before; 17double C_before; 18double D_before; 19 20 21void setup() { 22 fullScreen(); 23 frameRate(60);//フレームレートを設定 24 dW=displayWidth; 25 dH=displayHeight; 26 27 udp = new UDP (this, 60000); 28 udp.listen(true); 29 //basho = new Basho(this); 30} 31 32void receive (byte[] data, String ip, int port) { 33 String message = new String(data); 34 String[] rcvMSG = splitTokens(message, " "); 35 36 A_now = Double.parseDouble(rcvMSG[0]); 37 B_now = Double.parseDouble(rcvMSG[1]); 38 C_now = Double.parseDouble(rcvMSG[2]); 39 D_now = Double.parseDouble(rcvMSG[3]); 40 //println( "receive: \""+message+"\" from "+ip+" on port "+port ); 41} 42 43 44void draw() { 45 background(0, 0, 0);//黒色背景 46 smooth();//滑らかな描写 47 stroke(0, 255, 0);//線の色:緑 48 fill(0, 255, 0);//充填の色:緑 49 scale(1, -1);//x軸右側正&y軸上側正 50 translate(dW/2, -dH/2);//画面中央に原点移動 51 updateTime(); 52} 53 54void updateTime() {//一個前の値を差分処理用に格納 55 A_before = A_now; 56 B_before = B_now; 57 C_before = C_now; 58 D_before = D_now; 59} 60 61class Double_Absolute {//abs()がdoubleをサポートしていないのでdouble用に作成 62 double ABS (double b) 63 { 64 if (b<0) 65 { 66 b = -b; 67 } 68 return b; 69 } 70}
###試したこと
デバッグモードでの変数確認や先程記載したBashoでのリアルタイム変数遷移の確認。
↑でもやっぱり値が変わらず...
もしかして,送信と受信のヘルツがあってないとダメ?
回答1件
あなたの回答
tips
プレビュー