Skip to content

Commit 9d7cd94

Browse files
Pylon Driver: Fixes and Error Handling
1 parent d527a3a commit 9d7cd94

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

Drivers/Pylon/Implementation/libmcdriver_pylon_driver_pylon.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,23 @@ void CDriver_Pylon::loadPylonSDK()
112112
if (m_pPylonSDK.get() == nullptr) {
113113
unloadPylonSDK();
114114

115-
if (m_PylonCDLLData.empty())
116-
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_SDKRESOURCENOTSET);
115+
try {
117116

118-
m_pWorkingDirectory = m_pDriverEnvironment->CreateWorkingDirectory();
119-
m_pPylonCDLLFile = m_pWorkingDirectory->StoreCustomData("pylonC.dll", m_PylonCDLLData);
117+
if (m_PylonCDLLData.empty())
118+
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_SDKRESOURCENOTSET);
120119

121-
m_pPylonSDK = std::make_shared<CLibPylonSDK>(m_pPylonCDLLFile->GetAbsoluteFileName());
120+
m_pWorkingDirectory = m_pDriverEnvironment->CreateWorkingDirectory();
121+
m_pPylonCDLLFile = m_pWorkingDirectory->StoreCustomData("pylonC.dll", m_PylonCDLLData);
122+
123+
m_pPylonSDK = std::make_shared<CLibPylonSDK>(m_pPylonCDLLFile->GetAbsoluteFileName());
124+
125+
m_pPylonSDK->checkError(m_pPylonSDK->PylonInitialize());
126+
127+
}
128+
catch (...) {
129+
unloadPylonSDK();
130+
throw;
131+
}
122132
}
123133
}
124134

Drivers/Pylon/Implementation/libmcdriver_pylon_pylondeviceinstance.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,19 @@ Abstract: This is a stub class definition of CPylonDeviceInstance
3434
#include "libmcdriver_pylon_pylondeviceinstance.hpp"
3535
#include "libmcdriver_pylon_interfaceexception.hpp"
3636

37+
//#include <iostream>
38+
3739
// Include custom headers here.
3840
#define PYLONFEATURENAME_AUTOEXPOSURE "ExposureAuto"
3941
#define PYLONFEATURENAME_EXPOSURETIME "ExposureTime"
4042

43+
/* pylon device access modes */
44+
#define PYLONACCESS_MODE_MONITOR 0
45+
#define PYLONACCESS_MODE_CONTROL 1
46+
#define PYLONACCESS_MODE_STREAM 2
47+
#define PYLONACCESS_MODE_EVENT 4
48+
#define PYLONACCESS_MODE_EXCLUSIVE 8
49+
4150
using namespace LibMCDriver_Pylon::Impl;
4251

4352
/*************************************************************************************************************************
@@ -52,11 +61,25 @@ CPylonDeviceInstance::CPylonDeviceInstance(const std::string & sIdentifier, PLib
5261
if (pDeviceHandle == nullptr)
5362
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_INVALIDPARAM);
5463

64+
m_pPylonSDK->checkError (m_pPylonSDK->PylonDeviceOpen(m_pDeviceHandle, PYLONACCESS_MODE_CONTROL | PYLONACCESS_MODE_STREAM));
65+
5566
}
5667

5768
CPylonDeviceInstance::~CPylonDeviceInstance()
5869
{
5970
if (m_pPylonSDK.get() != nullptr) {
71+
72+
if (m_pDeviceHandle != nullptr) {
73+
pylonBool bIsOpen = 0;
74+
pylonResult nResult = m_pPylonSDK->PylonDeviceIsOpen(m_pDeviceHandle, &bIsOpen);
75+
76+
if (nResult == 0) {
77+
if (bIsOpen != 0) {
78+
m_pPylonSDK->PylonDeviceClose(m_pDeviceHandle);
79+
}
80+
}
81+
}
82+
6083
m_pPylonSDK->PylonDestroyDevice(m_pDeviceHandle);
6184
}
6285
m_pDeviceHandle = nullptr;
@@ -211,8 +234,11 @@ void CPylonDeviceInstance::grabSingleGreyscaleImage(const LibMCDriver_Pylon_uint
211234
if (grabResult.m_Status != ePylonGrabStatus::pgsGrabbed)
212235
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_COULDNOTGRABIMAGE, "Could not grab image (#" + std::to_string (grabResult.m_nErrorCode) + ")");
213236

214-
if ((grabResult.m_OffsetX != 0) || (grabResult.m_OffsetY != 0))
215-
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_UNSUPPORTEDGRABOFFSET, "Unsupported Grab Offset (" + std::to_string(grabResult.m_OffsetX) + "/" + std::to_string(grabResult.m_OffsetY) + ")");
237+
//if ((grabResult.m_OffsetX != 0) || (grabResult.m_OffsetY != 0))
238+
//throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_UNSUPPORTEDGRABOFFSET, "Unsupported Grab Offset (" + std::to_string(grabResult.m_OffsetX) + "/" + std::to_string(grabResult.m_OffsetY) + ")");
239+
240+
//std::cout << "Grab offset: " << std::to_string(grabResult.m_OffsetX) + "/" + std::to_string(grabResult.m_OffsetY) << std::endl;
241+
//std::cout << "Padding: " << std::to_string(grabResult.m_PaddingX) + "/" + std::to_string(grabResult.m_PaddingY) << std::endl;
216242

217243
if ((grabResult.m_SizeX <= 0) || (grabResult.m_SizeY <= 0))
218244
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_INVALIDGRABIMAGESIZE, "Invalid Grab Image Size (" + std::to_string(grabResult.m_SizeX) + "/" + std::to_string(grabResult.m_SizeY) + ")");
@@ -226,8 +252,8 @@ void CPylonDeviceInstance::grabSingleGreyscaleImage(const LibMCDriver_Pylon_uint
226252
std::to_string(nImageSizeX) + "x" + std::to_string(nImageSizeY) + " != " +
227253
std::to_string(grabResult.m_SizeX) + "x" + std::to_string(grabResult.m_SizeY));
228254

229-
if ((grabResult.m_PaddingX != 0) || (grabResult.m_PaddingY != 0))
230-
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_UNSUPPORTEDGRABPADDING, "Unsupported Grab Padding (" + std::to_string(grabResult.m_PaddingX) + "/" + std::to_string(grabResult.m_PaddingY) + ")");
255+
//if ((grabResult.m_PaddingX != 0) || (grabResult.m_PaddingY != 0))
256+
// throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_UNSUPPORTEDGRABPADDING, "Unsupported Grab Padding (" + std::to_string(grabResult.m_PaddingX) + "/" + std::to_string(grabResult.m_PaddingY) + ")");
231257

232258
pImageInstance->SetPixels(0, 0, nImageSizeX, nImageSizeY, LibMCEnv::eImagePixelFormat::GreyScale8bit, payloadBuffer);
233259

Drivers/Pylon/Implementation/libmcdriver_pylon_sdk.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ CLibPylonSDK::CLibPylonSDK(const std::string& sDLLNameUTF8)
135135
this->PylonDeviceSetBooleanFeature = (PPylonDeviceSetBooleanFeature)_loadPylonAddress(hLibrary, "PylonDeviceSetBooleanFeature", 12);
136136
this->PylonDeviceGetBooleanFeature = (PPylonDeviceGetBooleanFeature)_loadPylonAddress(hLibrary, "PylonDeviceGetBooleanFeature", 12);
137137
this->PylonDeviceGrabSingleFrame = (PPylonDeviceGrabSingleFrame)_loadPylonAddress(hLibrary, "PylonDeviceGrabSingleFrame", 32);
138+
this->GenApiGetLastErrorDetail = (PGenApiGetLastErrorDetail)_loadPylonAddress(hLibrary, "GenApiGetLastErrorDetail", 8);
138139

139140
m_LibraryHandle = (void*) hLibrary;
140141
}
@@ -191,12 +192,26 @@ void CLibPylonSDK::resetFunctionPtrs()
191192
this->PylonDeviceSetBooleanFeature = nullptr;
192193
this->PylonDeviceGetBooleanFeature = nullptr;
193194
this->PylonDeviceGrabSingleFrame = nullptr;
195+
this->GenApiGetLastErrorDetail = nullptr;
194196

195197
}
196198

197199
void CLibPylonSDK::checkError(pylonResult statusCode)
198200
{
199201
if (statusCode != 0) {
200-
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_PYLONSDKERROR, "Pylon SDK Error: " + std::to_string (statusCode));
202+
std::string sErrorMessage = "unknown error";
203+
204+
if (this->GenApiGetLastErrorDetail != nullptr) {
205+
size_t nBufferSize = 0;
206+
this->GenApiGetLastErrorDetail (nullptr, &nBufferSize);
207+
if (nBufferSize > 0) {
208+
std::vector<char> buffer;
209+
buffer.resize(nBufferSize + 1);
210+
this->GenApiGetLastErrorDetail(&buffer[0], &nBufferSize);
211+
buffer.at(nBufferSize) = 0;
212+
sErrorMessage = std::string(&buffer[0]);
213+
}
214+
}
215+
throw ELibMCDriver_PylonInterfaceException(LIBMCDRIVER_PYLON_ERROR_PYLONSDKERROR, "Pylon SDK Error #" + std::to_string(statusCode) + ": " + sErrorMessage);
201216
}
202217
}

Drivers/Pylon/Implementation/libmcdriver_pylon_sdk.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535

3636
#ifdef _WIN32
3737

38-
#define PYLON_CALLINGCONVENTION __stdcall
38+
#define PYLON_CALLINGCONVENTION __cdecl
3939

4040
#else
4141

@@ -54,8 +54,8 @@ namespace LibMCDriver_Pylon {
5454
typedef void* libPylonDeviceInfoHandle;
5555
typedef void* libPylonDeviceHandle;
5656
typedef void* libPylonStreamBufferHandle;
57-
typedef uint64_t pylonResult;
58-
typedef uint32_t pylonBool;
57+
typedef int32_t pylonResult;
58+
typedef int8_t pylonBool;
5959

6060
enum class ePylonGrabStatus : int32_t
6161
{
@@ -147,6 +147,7 @@ namespace LibMCDriver_Pylon {
147147

148148
typedef pylonResult(PYLON_CALLINGCONVENTION* PPylonDeviceGrabSingleFrame) (libPylonDeviceHandle pDeviceHandle, size_t nChannel, uint8_t * pBuffer, size_t nBufferSize, sPylonGrabResult * pGrabResult, pylonBool * pbReady, uint32_t nTimeOut);
149149

150+
typedef pylonResult(PYLON_CALLINGCONVENTION* PGenApiGetLastErrorDetail) (char * pszBuffer, size_t * pnBufLen);
150151

151152

152153
class CLibPylonSDK {
@@ -186,6 +187,7 @@ namespace LibMCDriver_Pylon {
186187
PPylonDeviceSetBooleanFeature PylonDeviceSetBooleanFeature = nullptr;
187188
PPylonDeviceGetBooleanFeature PylonDeviceGetBooleanFeature = nullptr;
188189
PPylonDeviceGrabSingleFrame PylonDeviceGrabSingleFrame = nullptr;
190+
PGenApiGetLastErrorDetail GenApiGetLastErrorDetail = nullptr;
189191

190192
CLibPylonSDK(const std::string & sDLLNameUTF8);
191193

0 commit comments

Comments
 (0)