/// <summary> /// Timer event handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _timerHighSpeed_Tick(object sender, EventArgs e) { uint notify = 0; List <int[]> data = ThreadSafeBuffer.Get(ref notify); uint count = 0; foreach (int[] profile in data) { // Process received data here. count++; } _lblReceiveProfileCount.Text = (Convert.ToUInt32(_lblReceiveProfileCount.Text) + count).ToString(); if ((notify & 0xFFFF) != 0) { // If the lower 16 bits of notify are not 0, high-speed communication was interrupted, so stop the timer. _timerHighSpeed.Stop(); MessageBox.Show(string.Format("Communication closed. 0x{0:x8}", notify)); } if ((notify & 0x10000) != 0) { // If a task is required when batch measurements end, code it here. } }
/// <summary> /// タイマーイベントハンドラ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _timerHighSpeed_Tick(object sender, EventArgs e) { uint notify = 0; List <int[]> data = ThreadSafeBuffer.Get(ref notify); uint count = 0; foreach (int[] profile in data) { // ここで受信データを加工する count++; } _lblReceiveProfileCount.Text = (Convert.ToUInt32(_lblReceiveProfileCount.Text) + count).ToString(); if ((notify & 0xFFFF) != 0) { // notifyの下位16bitが0でない場合、高速通信が中断されたのでタイマーをとめる。 _timerHighSpeed.Stop(); MessageBox.Show(string.Format("通信終了しました。0x{0:x8}", notify)); } if ((notify & 0x10000) != 0) { // バッチ測定完了時の処理が必要であればここに記述する。 } }
/// <summary> /// High-speed communication callback process /// </summary> /// <param name="buffer">Pointer to beginning of received data</param> /// <param name="size">Byte size per 1 profile in received data</param> /// <param name="count">Profile count</param> /// <param name="notify">Exited or not</param> /// <param name="user">Information passed when high-speed data communication was initialized (thread ID)</param> private static void ReceiveData(IntPtr buffer, uint size, uint count, uint notify, uint user) { // Received data is in BYTE units, set as a group of INT units. uint profileSize = size / sizeof(int); List <int[]> reciveBuffer = new List <int[]>(); int[] bufferArray = new int[profileSize * count]; Marshal.Copy(buffer, bufferArray, 0, (int)(profileSize * count)); // Retain profile data for (int i = 0; i < count; i++) { int[] oneProfile = new int[profileSize]; Array.Copy(bufferArray, i * profileSize, oneProfile, 0, profileSize); reciveBuffer.Add(oneProfile); } ThreadSafeBuffer.Add(reciveBuffer, notify); }
/// <summary> /// 高速通信のコールバック処理 /// </summary> /// <param name="buffer">受信データの先頭ポインタ</param> /// <param name="size">受信データの1プロファイルあたりのBYTEサイズ</param> /// <param name="count">プロファイル数</param> /// <param name="notify">終了したかどうか</param> /// <param name="user">高速データ通信初期化時に渡した情報(スレッドID)</param> private static void ReceiveData(IntPtr buffer, uint size, uint count, uint notify, uint user) { // 受信はBYTE単位なのでINT単位のまとまりに設定 uint profileSize = size / sizeof(int); List <int[]> reciveBuffer = new List <int[]>(); int[] bufferArray = new int[profileSize * count]; Marshal.Copy(buffer, bufferArray, 0, (int)(profileSize * count)); // プロファイルデータ保持 for (int i = 0; i < count; i++) { int[] oneProfile = new int[profileSize]; Array.Copy(bufferArray, i * profileSize, oneProfile, 0, profileSize); reciveBuffer.Add(oneProfile); } ThreadSafeBuffer.Add(reciveBuffer, notify); }
/// <summary> /// High-speed data communication "Start" button pressed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _btnStartHighSpeed_Click(object sender, EventArgs e) { // Stop/exit high-speed data communication. NativeMethods.LJV7IF_StopHighSpeedDataCommunication(DEVICE_ID); NativeMethods.LJV7IF_HighSpeedDataCommunicationFinalize(DEVICE_ID); // Initialize data. ThreadSafeBuffer.Clear(); Rc rc = Rc.Ok; // Initialize the high-speed communication path. // High-speed communication start prep LJV7IF_HIGH_SPEED_PRE_START_REQ req = new LJV7IF_HIGH_SPEED_PRE_START_REQ(); try { uint frequency = Convert.ToUInt32(_txtCallbackFrequency.Text); uint threadId = (uint)DEVICE_ID; if (_rdUsb.Checked) { // Initialize USB high-speed data communication rc = (Rc)NativeMethods.LJV7IF_HighSpeedDataUsbCommunicationInitalize(DEVICE_ID, _callback, frequency, threadId); } else { // Create Ethernet communication settings. ushort highSpeedPort = 0; _ethernetConfig.abyIpAddress = new byte[] { Convert.ToByte(_txtIpFirstSegment.Text), Convert.ToByte(_txtIpSecondSegment.Text), Convert.ToByte(_txtIpThirdSegment.Text), Convert.ToByte(_txtIpFourthSegment.Text) }; _ethernetConfig.wPortNo = Convert.ToUInt16(_txtCommandPort.Text); highSpeedPort = Convert.ToUInt16(_txtHighSpeedPort.Text); // Initialize Ethernet high-speed data communication rc = (Rc)NativeMethods.LJV7IF_HighSpeedDataEthernetCommunicationInitalize(DEVICE_ID, ref _ethernetConfig, highSpeedPort, _callback, frequency, threadId); } if (!CheckReturnCode(rc)) { return; } req.bySendPos = Convert.ToByte(_txtStartProfileNo.Text); } catch (FormatException ex) { MessageBox.Show(this, ex.Message); return; } catch (OverflowException ex) { MessageBox.Show(this, ex.Message); return; } // High-speed data communication start prep LJV7IF_PROFILE_INFO profileInfo = new LJV7IF_PROFILE_INFO(); rc = (Rc)NativeMethods.LJV7IF_PreStartHighSpeedDataCommunication(DEVICE_ID, ref req, ref profileInfo); if (!CheckReturnCode(rc)) { return; } // Start high-speed data communication. rc = (Rc)NativeMethods.LJV7IF_StartHighSpeedDataCommunication(DEVICE_ID); if (!CheckReturnCode(rc)) { return; } _lblReceiveProfileCount.Text = "0"; _timerHighSpeed.Start(); }