SR7ApiInfiniteLoopCallbackImpl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using SR7Link;
  8. namespace SRAPI
  9. {
  10. class SR7ApiInfiniteLoopCallbackImpl : SR7ApiBase
  11. {
  12. private SR7IF_RollDataCallback RollDataCallback;
  13. public IntPtr LoopImgBuff32;
  14. public IntPtr LoopImgBuff16;
  15. public IntPtr LoopGrayBuff;
  16. public IntPtr LoopEncoder;
  17. public override int Open(int lDeviceId, string strIP, int timeout, ErrConnectCallBack pfErrCallBack)
  18. {
  19. ControllerID = lDeviceId;
  20. string[] ipTmp = strIP.Split('.');
  21. if (ipTmp.Length != 4)
  22. {
  23. throw new SRAPIException($"Failed to resolve IP ");
  24. }
  25. SR7IF_ETHERNET_CONFIG ethernetConfig;
  26. ethernetConfig.abyIpAddress = new Byte[]
  27. {
  28. Convert.ToByte(ipTmp[0]),
  29. Convert.ToByte(ipTmp[1]),
  30. Convert.ToByte(ipTmp[2]),
  31. Convert.ToByte(ipTmp[3])
  32. };
  33. int openRet = SR7LinkFunc.SR7IF_EthernetOpenExt(ControllerID, ref ethernetConfig, 2000, pfErrCallBack);
  34. if (openRet != 0)
  35. return openRet;
  36. int nCameraB = SR7LinkFunc.SR7IF_GetOnlineCameraB(ControllerID);
  37. CameraAOnline = true;
  38. CameraBOnline = (nCameraB == 0);
  39. return openRet;
  40. }
  41. public override int Init(int dwProfileCnt, uint ProfileBits, int nTimeout, SR7IFGetDataCallBack pfGetDataCallBack)
  42. {
  43. GetDataCallBack = pfGetDataCallBack;
  44. RollDataCallback = new SR7IF_RollDataCallback(SR7IFRollDataCallback);
  45. int ret = SR7LinkFunc.SR7IF_RollDataCallbackInitalize(ControllerID, RollDataCallback, dwProfileCnt,300000);
  46. return ret;
  47. }
  48. public void SR7IFRollDataCallback(IntPtr pProfileBuffer, IntPtr pIntensityBuffer, IntPtr pEncoder, int dwSize, int dwCount, int dwRet, int dwDeviceId)
  49. {
  50. LoopImgBuff32 = pProfileBuffer;
  51. LoopGrayBuff = pIntensityBuffer;
  52. LoopEncoder = pEncoder;
  53. GetDataCallBack(dwSize, dwCount, 1, dwRet,0);
  54. }
  55. public override int GetData(int[] pHighData, byte[] pGrayData, int[] pEncoderData, int nProfileWidth, int nHighlen, int ABCam)
  56. {
  57. if (LoopImgBuff32 == IntPtr.Zero || LoopGrayBuff == IntPtr.Zero || LoopEncoder == IntPtr.Zero)
  58. return -1;
  59. if (!CameraBOnline && ABCam == 0)//只有相机A取相机A的数据 / Only camera A can retrieve the data of camera A.
  60. {
  61. if (pHighData != null)
  62. Marshal.Copy(LoopImgBuff32, pHighData, 0, nProfileWidth * nHighlen);
  63. if (pGrayData != null)
  64. Marshal.Copy(LoopGrayBuff, pGrayData, 0, nProfileWidth * nHighlen);
  65. }
  66. else if (CameraBOnline && ABCam == 0)//双相机取相机A的数据 / Dual cameras take data from camera A
  67. {
  68. int width = nProfileWidth;
  69. for (int i = 0; i < nHighlen; i++)
  70. {
  71. int new_start = i * width;
  72. int old_start = i * nProfileWidth*2;
  73. if (pHighData != null)
  74. Marshal.Copy(IntPtr.Add(LoopImgBuff32, old_start * sizeof(int)), pHighData, new_start, width);
  75. if (pGrayData != null)
  76. Marshal.Copy(IntPtr.Add(LoopGrayBuff, old_start), pGrayData, new_start, width);
  77. }
  78. }
  79. else if (CameraBOnline && ABCam == 1) //双相机取相机B的数据 / Dual cameras get data from camera B
  80. {
  81. int width = nProfileWidth;
  82. for (int i = 0; i < nHighlen; i++)
  83. {
  84. int new_start = i * width;
  85. int old_start = i * nProfileWidth*2 + width;
  86. if (pHighData != null)
  87. Marshal.Copy(IntPtr.Add(LoopImgBuff32, old_start * sizeof(int)), pHighData, new_start, width);
  88. if (pGrayData != null)
  89. Marshal.Copy(IntPtr.Add(LoopGrayBuff, old_start), pGrayData, new_start, width);
  90. }
  91. }
  92. if (pEncoderData != null)
  93. Marshal.Copy(LoopEncoder, pEncoderData, 0, nHighlen);
  94. return 0;
  95. }
  96. public override int Start(bool bIOTrigger, int timeout)
  97. {
  98. //设置无限循环终止行数 / Set the infinite loop termination line number
  99. int callbackPointRet = SR7LinkFunc.SR7IF_SetBatchRollProfilePoint(ControllerID, IntPtr.Zero, (uint)BatchPoints);
  100. if (callbackPointRet != 0)
  101. {
  102. return callbackPointRet;
  103. }
  104. int ret = 0;
  105. if (bIOTrigger)//hard trigger
  106. ret = SR7LinkFunc.SR7IF_StartIOTriggerMeasure(ControllerID, timeout, 0);
  107. else//Soft trigger
  108. ret = SR7LinkFunc.SR7IF_StartMeasure(ControllerID, timeout);
  109. return ret;
  110. }
  111. public override int Stop(int instantStop)
  112. {
  113. int ret = SR7LinkFunc.SR7IF_StopMeasure(ControllerID);
  114. return ret;
  115. }
  116. }
  117. }