| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using SR7Link;
- namespace SRAPI
- {
- class SR7ApiInfiniteLoopCallbackImpl : SR7ApiBase
- {
- private SR7IF_RollDataCallback RollDataCallback;
- public IntPtr LoopImgBuff32;
- public IntPtr LoopImgBuff16;
- public IntPtr LoopGrayBuff;
- public IntPtr LoopEncoder;
- public override int Open(int lDeviceId, string strIP, int timeout, ErrConnectCallBack pfErrCallBack)
- {
- ControllerID = lDeviceId;
- string[] ipTmp = strIP.Split('.');
- if (ipTmp.Length != 4)
- {
- throw new SRAPIException($"Failed to resolve IP ");
- }
- SR7IF_ETHERNET_CONFIG ethernetConfig;
- ethernetConfig.abyIpAddress = new Byte[]
- {
- Convert.ToByte(ipTmp[0]),
- Convert.ToByte(ipTmp[1]),
- Convert.ToByte(ipTmp[2]),
- Convert.ToByte(ipTmp[3])
- };
- int openRet = SR7LinkFunc.SR7IF_EthernetOpenExt(ControllerID, ref ethernetConfig, 2000, pfErrCallBack);
- if (openRet != 0)
- return openRet;
- int nCameraB = SR7LinkFunc.SR7IF_GetOnlineCameraB(ControllerID);
- CameraAOnline = true;
- CameraBOnline = (nCameraB == 0);
- return openRet;
- }
- public override int Init(int dwProfileCnt, uint ProfileBits, int nTimeout, SR7IFGetDataCallBack pfGetDataCallBack)
- {
- GetDataCallBack = pfGetDataCallBack;
- RollDataCallback = new SR7IF_RollDataCallback(SR7IFRollDataCallback);
- int ret = SR7LinkFunc.SR7IF_RollDataCallbackInitalize(ControllerID, RollDataCallback, dwProfileCnt,300000);
- return ret;
- }
- public void SR7IFRollDataCallback(IntPtr pProfileBuffer, IntPtr pIntensityBuffer, IntPtr pEncoder, int dwSize, int dwCount, int dwRet, int dwDeviceId)
- {
- LoopImgBuff32 = pProfileBuffer;
- LoopGrayBuff = pIntensityBuffer;
- LoopEncoder = pEncoder;
- GetDataCallBack(dwSize, dwCount, 1, dwRet,0);
- }
- public override int GetData(int[] pHighData, byte[] pGrayData, int[] pEncoderData, int nProfileWidth, int nHighlen, int ABCam)
- {
- if (LoopImgBuff32 == IntPtr.Zero || LoopGrayBuff == IntPtr.Zero || LoopEncoder == IntPtr.Zero)
- return -1;
- if (!CameraBOnline && ABCam == 0)//只有相机A取相机A的数据 / Only camera A can retrieve the data of camera A.
- {
- if (pHighData != null)
- Marshal.Copy(LoopImgBuff32, pHighData, 0, nProfileWidth * nHighlen);
- if (pGrayData != null)
- Marshal.Copy(LoopGrayBuff, pGrayData, 0, nProfileWidth * nHighlen);
- }
- else if (CameraBOnline && ABCam == 0)//双相机取相机A的数据 / Dual cameras take data from camera A
- {
- int width = nProfileWidth;
- for (int i = 0; i < nHighlen; i++)
- {
- int new_start = i * width;
- int old_start = i * nProfileWidth*2;
- if (pHighData != null)
- Marshal.Copy(IntPtr.Add(LoopImgBuff32, old_start * sizeof(int)), pHighData, new_start, width);
- if (pGrayData != null)
- Marshal.Copy(IntPtr.Add(LoopGrayBuff, old_start), pGrayData, new_start, width);
- }
- }
- else if (CameraBOnline && ABCam == 1) //双相机取相机B的数据 / Dual cameras get data from camera B
- {
- int width = nProfileWidth;
- for (int i = 0; i < nHighlen; i++)
- {
- int new_start = i * width;
- int old_start = i * nProfileWidth*2 + width;
- if (pHighData != null)
- Marshal.Copy(IntPtr.Add(LoopImgBuff32, old_start * sizeof(int)), pHighData, new_start, width);
- if (pGrayData != null)
- Marshal.Copy(IntPtr.Add(LoopGrayBuff, old_start), pGrayData, new_start, width);
- }
- }
- if (pEncoderData != null)
- Marshal.Copy(LoopEncoder, pEncoderData, 0, nHighlen);
- return 0;
- }
- public override int Start(bool bIOTrigger, int timeout)
- {
- //设置无限循环终止行数 / Set the infinite loop termination line number
- int callbackPointRet = SR7LinkFunc.SR7IF_SetBatchRollProfilePoint(ControllerID, IntPtr.Zero, (uint)BatchPoints);
- if (callbackPointRet != 0)
- {
- return callbackPointRet;
- }
- int ret = 0;
- if (bIOTrigger)//hard trigger
- ret = SR7LinkFunc.SR7IF_StartIOTriggerMeasure(ControllerID, timeout, 0);
- else//Soft trigger
- ret = SR7LinkFunc.SR7IF_StartMeasure(ControllerID, timeout);
- return ret;
- }
- public override int Stop(int instantStop)
- {
- int ret = SR7LinkFunc.SR7IF_StopMeasure(ControllerID);
- return ret;
- }
- }
- }
|