Пример #1
0
        public static MOTOR_DATA_KV_SP ToStruct(byte[] bytes)
        {
            GCHandle         gch    = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            MOTOR_DATA_KV_SP result = (MOTOR_DATA_KV_SP)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(MOTOR_DATA_KV_SP));

            gch.Free();
            return(result);
        }
Пример #2
0
 //メインスレッドでの処理
 private void worker_udp_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     // 画面表示
     if ((id % 1) == 0)
     {
         MOTOR_DATA_KV_SP kmd3 = (MOTOR_DATA_KV_SP)e.UserState;
         string           s    = string.Format("worker_udp_ProgressChanged:[{0} {1} az:{2} alt:{3}]\n", kmd3.cmd, kmd3.t, kmd3.az, kmd3.alt);
         textBox1.AppendText(s);
     }
 }
Пример #3
0
        static byte[] ToBytes(MOTOR_DATA_KV_SP obj)
        {
            int    size = Marshal.SizeOf(typeof(MOTOR_DATA_KV_SP));
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(obj, ptr, false);
            byte[] bytes = new byte[size];
            Marshal.Copy(ptr, bytes, 0, size);
            Marshal.FreeHGlobal(ptr);
            return(bytes);
        }
Пример #4
0
        // 別スレッド処理(UDP)
        private void worker_udp_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = (BackgroundWorker)sender;

            //バインドするローカルポート番号
            int localPort = 24404;

            System.Net.Sockets.UdpClient udpc = null;;
            try
            {
                udpc = new System.Net.Sockets.UdpClient(localPort);
            }
            catch (Exception ex)
            {
                //匿名デリゲートで表示する
                this.Invoke(new dlgSetString(ShowRText), new object[] { richTextBox1, ex.ToString() });
            }


            //文字コードを指定する
            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            //データを送信するリモートホストとポート番号
            //string remoteHost = "localhost";
            //   string remoteHost = "192.168.1.204";
            //   int remotePort = 24404;
            //送信するデータを読み込む
            string sendMsg = "test送信するデータ";

            byte[] sendBytes = enc.GetBytes(sendMsg);

            //リモートホストを指定してデータを送信する
            // udpc.Send(sendBytes, sendBytes.Length, remoteHost, remotePort);


            string           str;
            MOTOR_DATA_KV_SP kmd3 = new MOTOR_DATA_KV_SP();
            int size = Marshal.SizeOf(kmd3);

            //データを受信する
            System.Net.IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, localPort);
            while (bw.CancellationPending == false)
            {
                byte[] rcvBytes = udpc.Receive(ref remoteEP);
                if (rcvBytes.Length == size)
                {
                    kmd3 = ToStruct(rcvBytes);
                    if (kmd3.cmd == 1) //mmMove:1
                    {
                        FishMode  = FISH_DETECT;
                        MT3Status = SAVE;
                        this.Invoke(new dlgSetColor(SetTimer), new object[] { timerSaveMainTime, START });
                        //timerSaveMainTime.Start();
                        buttonSave_Click(sender, e);
                    }
                    else
                    if (kmd3.cmd == 16) //mmLost:16
                    {
                        FishMode = LOST;
                        //ButtonSaveEnd_Click(sender, e);
                        //匿名デリゲートで表示する
                        this.Invoke(new dlgSetColor(SetTimer), new object[] { timerSaveMainTime, STOP });
                        this.Invoke(new dlgSetColor(SetTimer), new object[] { timerSavePostTime, START });
                        //timerSavePostTime.Start();
                    }
                    else
                    if (kmd3.cmd == 17) //mmMoveEnd:17
                    {
                        MT3Status = STOP;
                    }

                    str = "受信したデータ(kmd3):" + kmd3.cmd + ":" + kmd3.t + ":" + kmd3.az + "\n";
                    this.Invoke(new dlgSetString(ShowRText), new object[] { richTextBox1, str });
                    bw.ReportProgress(0, kmd3);
                }
                else
                {
                    string rcvMsg = enc.GetString(rcvBytes);
                    str = "受信したデータ:[" + rcvMsg + "]\n";
                    this.Invoke(new dlgSetString(ShowRText), new object[] { richTextBox1, str });
                }

                str = "送信元アドレス:{0}/ポート番号:{1}/Size:{2}\n" + remoteEP.Address + "/" + remoteEP.Port + "/" + rcvBytes.Length;
                this.Invoke(new dlgSetString(ShowRText), new object[] { richTextBox1, str });
            }

            //UDP接続を終了
            udpc.Close();
        }