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; } } }