Development Environment

include directories

Configuration Properties -> C/C++ -> General -> Additional Include Directories

$(PYLON_DEV_DIR)\include;

library directories

Configuration Properties -> Linker -> General -> Additional Library Directories

$(PYLON_DEV_DIR)\lib\x64;

Enable runtime type info

Configuration Properties -> C/C++ -> Language -> Enable Runtime Type Info = yes. By default, /GR is on, so no need to set.

Enable C++ exceptions

Configuration Properties -> C/C++ -> Code Generation -> Enable C++ Exceptions = yes. 默认位/EHsc,所以也无需设置。

Instant Camera Event Handler

  • Configuration event handler
  • Image event handler
  • Camera event handler

Heartbeat

When debugging a pylon application using GigE cameras you may encounter heartbeat timeouts. The application must send special network packets to the camera in defined intervals. If the camera doesn’t receive these heartbeats it will consider the connection as broken and won’t accept any commands from the application. This requires setting the heartbeat timeout of a camera to a higher value when debugging.

Initialization/Uninitialization

The pylon runtime system must be initialized before use. A pylon-based application must call the PylonInitialize() method before using any other functions of the pylon runtime system. Before an application exits, it must call the PylonTerminate() method to free resources allocated by the pylon runtime system.

The Pylon::PylonAutoInitTerm convenience class helps to do the above. The constructor of Pylon::PylonAutoInitTerm calls PylonInitialize(), the destructor calls PylonTerminate(). This ensures that the pylon runtime system is initialized during the lifetime of an object of the Pylon::PylonAutoInitTerm type.

#include <pylon/PylonIncludes.h>
using namespace Pylon;

int main(int argc, char* argv[])
{

Pylon::PylonAutoInitTerm autoInitTerm;  // PylonInitialize() will be called now

// Use pylon
// ..

}  // autoInitTerm's destructor calls PylonTerminate() now

Exception

try
{
    camera.Width.SetValue( 640 );
}
catch (const GenericException & e)
{
    cerr << "Failed to set the AOI width. Reason: "
    << e.GetDescription() << endl;
}

Creating a pylon Device

The first found camera device is created.

CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice() );

获取或设置参数

CIntegerParameter width( camera.GetNodeMap(), "Width" );
cout << width.GetMax() <<endl;
cout << width.GetInc() << endl;
width.SetValue(width.GetMax() - 1,  IntegerValueCorrection_Down);
width.TrySetValue(width.GetMax() - 1, IntegerValueCorrection_Down);

HeartBeat

By default, pylon sends a heartbeat signal to all GigE cameras and the cameras then respond with a heartbeat signal of their own going back to pylon. If the cameras don’t receive a heartbeat signal from pylon in the defined interval, the camera doesn’t respond to commands from pylon anymore and is disconnected.

Heartbeat During Debugging# When you work in debugging mode in pylon and hit a breakpoint in your pylon application, the debugger suspends all threads including the one sending the heartbeats. Thus, when you debug your pylon application and single-step through your code, no heartbeats are sent to the camera and the camera closes the connection.

To work around this, pylon detects when you debug your pylon application and extends the heartbeat timeout to one hour. This allows you to single-step through your pylon application without the camera closing the connection.

Extending the heartbeat timeout has the following side effect: When you terminate your pylon application using the debugger while the camera is opened and you restart your pylon application right after terminating it, you may get an error stating that the camera is currently in use. This is so, because due to the forced termination of the pylon application, pylon couldn’t inform the camera of the termination and did not close the connection. As a result, the camera doesn’t accept any new connections until the heartbeat timeout has elapsed or is restarted.

相机和软件连接时有一个HeartBeat信号,默认是300000ms(5min),如果网线异常断开,需要5min相机才能重新允许连接,可以修改这个值。这个值貌似每次重新打开相机都会被复位为5min。

CIntegerParameter heartbeat( camera.GetTLNodeMap(), "HeartbeatTimeout" );
cout << heartbeat.GetValue() << endl;
heartbeat.TrySetValue( 1000, IntegerValueCorrection_Nearest );  // set to 1000 ms timeout if writable, 此时程序必须每1s内发送一次指令,否则相机将会自动断开连接。程序在打断点时,是没有指令的,所以打断点调试时,应设置长一点的时间。

GevHeartbeatTimeout

Sets the heartbeat timeout (in milliseconds). If a timeout occurs, the camera releases the network connection and enters a state that allows reconnection. By default, the heartbeat timeout is set to 3000 ms. Changing the value is not required for normal camera operation. However, if you are debugging an application and stop at a break point, a heartbeat timeout may occur and the connection to the camera may be lost. Therefore, you should increase the GevHeartbeatTimeout parameter value while debugging. When debugging is complete, you should return the timeout to its normal setting.

The pylon API also provides a HeartbeatTimeout parameter that sets the heartbeat timeout on transport layer side. The HeartbeatTimeout and GevHeartbeatTimeout parameters are automatically synchronized. When you change one parameter, the other one adapts, and vice versa.

ConnectionGuardEnable

Pylon会自动检测是否为Debug模式而设置HeartbeatTimeout,当使用F5调试运行程序时,ConnectionGuardEnable自动被设置为true,且HeartbeatTimeout为5min,当使用Ctrl+F5直接运行程序时,ConnectionGuardEnable自动被设置为false,且HeartbeatTimeout为默认的3000ms。

StartGrabbing

发送取图指令,参数为取图的数量,MaxNumBuffer为缓存数量。

camera.MaxNumBuffer = 5;
camera.StartGrabbing( 100 );
camera.IsGrabbing() //判断是否取图完毕

CGrabResultPtr

图像

camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException );
ptrGrabResult->GrabSucceeded(); 判断取图是否成功还是超时
cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
const uint8_t* pImageBuffer = (uint8_t*) ptrGrabResult->GetBuffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;

Pylon::DisplayImage

显示图像

Pylon::DisplayImage( 1, ptrGrabResult );

WaitObject::Sleep(1000);

线程等待

CBaslerUniversalCameraEventHandler & CConfigurationEventHandler

事件参数