#include <mil.h>
#if M_MIL_USE_WINDOWS
#include <Windows.h>
#endif
#define BUFFERING_SIZE_MAX 22
#define htonl(x) ( ( ( ( x ) & 0x000000ff ) << 24 ) | \
( ( ( x ) & 0x0000ff00 ) << 8 ) | \
( ( ( x ) & 0x00ff0000 ) >> 8 ) | \
( ( ( x ) & 0xff000000 ) >> 24 ) )
#define htons(x) ( ( ( ( x ) & 0xff00 ) >> 8 ) | \
( ( ( x ) & 0x00ff ) << 8 ) )
#define ntohl(x) htonl(x)
#define ntohs(x) htons(x)
#define IPV4_ADDRESS_SIZE 20
typedef struct
{
MIL_ID MilImageDisp;
MIL_INT ProcessedImageCount;
MIL_INT CorruptImageCount;
MIL_TEXT_PTR MulticastAddress;
} HookDataStruct;
void PrintCameraInfo(MIL_ID MilDigitizer);
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType,
MIL_ID HookId,
void* HookDataPtr);
int MosMain(void)
{
MIL_ID MilApplication;
MIL_ID MilSystem ;
MIL_ID MilDigitizer;
MIL_ID MilDisplay;
MIL_ID MilImageDisp;
MIL_ID MilGrabBufferList[BUFFERING_SIZE_MAX];
MIL_INT MilGrabBufferListSize;
MIL_INT ProcessFrameCount = 0;
MIL_INT DigProcessInProgress = M_FALSE;
MIL_INT BoardType = 0;
MIL_DOUBLE ProcessFrameRate = 0;
MIL_INT64 SourceDataFormat;
MIL_TEXT_CHAR MulticastAddr[IPV4_ADDRESS_SIZE] = {'\0'};
HookDataStruct UserHookData;
MIL_INT n = 0;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MsysInquire(MilSystem, M_BOARD_TYPE, &BoardType);
if(((BoardType & M_BOARD_TYPE_MASK) != M_GIGE_VISION))
{
MosPrintf(MIL_TEXT("This example requires a M_GIGE_VISION system type.\n"));
MosPrintf(MIL_TEXT("Please change system type in milconfig.\n"));
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
MdigAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_GC_MULTICAST_MASTER,
&MilDigitizer);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
MdigInquire(MilDigitizer, M_SOURCE_DATA_FORMAT, &SourceDataFormat);
MbufAllocColor(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
MdigInquire(MilDigitizer, M_SIZE_X, M_NULL),
MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL),
MdigInquire(MilDigitizer, M_TYPE, M_NULL),
M_IMAGE+M_DISP+M_GRAB+M_PROC+SourceDataFormat,
&MilImageDisp);
MbufClear(MilImageDisp, M_COLOR_BLACK);
MdispSelect(MilDisplay, MilImageDisp);
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
for(MilGrabBufferListSize = 0;
MilGrabBufferListSize<BUFFERING_SIZE_MAX; MilGrabBufferListSize++)
{
MbufAllocColor(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
MdigInquire(MilDigitizer, M_SIZE_X, M_NULL),
MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL),
MdigInquire(MilDigitizer, M_TYPE, M_NULL),
M_IMAGE+M_GRAB+M_PROC+SourceDataFormat,
&MilGrabBufferList[MilGrabBufferListSize]);
if (MilGrabBufferList[MilGrabBufferListSize])
{
MbufClear(MilGrabBufferList[MilGrabBufferListSize], M_COLOR_WHITE);
}
else
break;
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
for (n=0; n<2 && MilGrabBufferListSize; n++)
{
MilGrabBufferListSize--;
MbufFree(MilGrabBufferList[MilGrabBufferListSize]);
}
MosPrintf(MIL_TEXT("This example demonstrates the use of IP Multicast with GigE Vision"));
MosPrintf(MIL_TEXT(" devices.\n"));
MosPrintf(MIL_TEXT("It allocates a Multicast master digitizer that can read, write and"));
MosPrintf(MIL_TEXT(" grab from\n"));
MosPrintf(MIL_TEXT("a GigE Vision device.\n\n"));
MosPrintf(MIL_TEXT("This example must be used along with MulticastSlave.cpp connected to"));
MosPrintf(MIL_TEXT(" the same\n"));
MosPrintf(MIL_TEXT("GigE Vision device and running on another PC.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue."));
MosGetch();
PrintCameraInfo(MilDigitizer);
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.ProcessedImageCount = 0;
UserHookData.CorruptImageCount = 0;
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize,
M_START, M_DEFAULT, ProcessingFunction, &UserHookData);
MosPrintf(MIL_TEXT("If the MulticastSlave program is already running on the other PC, it"));
MosPrintf(MIL_TEXT(" should\n"));
MosPrintf(MIL_TEXT("have detected that this device is controlled by a multicast master"));
MosPrintf(MIL_TEXT(" digitizer\n"));
MosPrintf(MIL_TEXT("and have started image acquisition.\n\n"));
MosPrintf(MIL_TEXT("If the MulticastSlave program is not yet started then it should be"));
MosPrintf(MIL_TEXT(" started now.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to stop.\n"));
MosGetch();
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize,
M_STOP, M_DEFAULT, ProcessingFunction, &UserHookData);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_COUNT, &ProcessFrameCount);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_RATE, &ProcessFrameRate);
MosPrintf(MIL_TEXT("\n\n%ld frames grabbed at %.1f frames/sec (%.1f ms/frame).\n"),
ProcessFrameCount, ProcessFrameRate, 1000.0/ProcessFrameRate);
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
while(MilGrabBufferListSize > 0)
MbufFree(MilGrabBufferList[--MilGrabBufferListSize]);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
return 0;
}
void PrintCameraInfo(MIL_ID MilDigitizer)
{
MIL_TEXT_PTR DeviceVendor = NULL;
MIL_TEXT_PTR DeviceModel = NULL;
MIL_TEXT_PTR PixelFormat = NULL;
MIL_INT64 Width = 0, Height = 0;
MIL_INT Port = 0;
MIL_INT Len = 0;
MIL_TEXT_PTR MulticastAddress;
#if M_MIL_USE_WINDOWS
system("cls");
#endif
MdigInquire(MilDigitizer, M_CAMERA_VENDOR_SIZE, &Len);
DeviceVendor = new MIL_TEXT_CHAR[Len];
MdigInquire(MilDigitizer, M_CAMERA_VENDOR, DeviceVendor);
MdigInquire(MilDigitizer, M_CAMERA_MODEL_SIZE, &Len);
DeviceModel = new MIL_TEXT_CHAR[Len];
MdigInquire(MilDigitizer, M_CAMERA_MODEL, DeviceModel);
MdigInquireFeature(MilDigitizer, M_FEATURE_VALUE_AS_STRING+M_STRING_SIZE,
MIL_TEXT("PixelFormat"), M_DEFAULT, &Len);
PixelFormat = new MIL_TEXT_CHAR[Len];
MdigInquireFeature(MilDigitizer, M_FEATURE_VALUE_AS_STRING, MIL_TEXT("PixelFormat"),
M_DEFAULT, PixelFormat);
MdigInquireFeature(MilDigitizer, M_FEATURE_VALUE, MIL_TEXT("Width"), M_TYPE_INT64,
&Width);
MdigInquireFeature(MilDigitizer, M_FEATURE_VALUE, MIL_TEXT("Height"), M_TYPE_INT64,
&Height);
MdigInquire(MilDigitizer, M_GC_STREAM_CHANNEL_MULTICAST_ADDRESS_STRING_SIZE,
&Len);
MulticastAddress = new MIL_TEXT_CHAR[Len];
MdigInquire(MilDigitizer, M_GC_STREAM_CHANNEL_MULTICAST_ADDRESS_STRING,
MulticastAddress);
MdigInquire(MilDigitizer, M_GC_STREAM_PORT, &Port);
MosPrintf(MIL_TEXT("\n--------------------- Master digitizer connection status. "));
MosPrintf(MIL_TEXT("---------------------\n\n"), DeviceVendor, DeviceModel);
MosPrintf(MIL_TEXT("Connected to %s %s\n"), DeviceVendor, DeviceModel);
MosPrintf(MIL_TEXT("Device pixel format: %s\n"), PixelFormat);
MosPrintf(MIL_TEXT("Device AOI: %d x %d\n"), Width, Height);
MosPrintf(MIL_TEXT("IPv4 Multicast address: %s\n"), MulticastAddress);
MosPrintf(MIL_TEXT("Stream port: %d\n\n"), Port);
if(DeviceVendor)
delete [] DeviceVendor;
if(DeviceModel)
delete [] DeviceModel;
if(PixelFormat)
delete [] PixelFormat;
if(MulticastAddress)
delete [] MulticastAddress;
}
#define STRING_LENGTH_MAX 20
#define STRING_POS_X 20
#define STRING_POS_Y 20
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType,
MIL_ID HookId,
void* HookDataPtr)
{
HookDataStruct *UserHookDataPtr = (HookDataStruct *)HookDataPtr;
MIL_ID ModifiedBufferId;
MIL_TEXT_CHAR Text[STRING_LENGTH_MAX]= {MIL_TEXT('\0'),};
MIL_INT IsFrameCorrupt = M_FALSE;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER+M_BUFFER_ID, &ModifiedBufferId);
MdigGetHookInfo(HookId, M_CORRUPTED_FRAME, &IsFrameCorrupt);
UserHookDataPtr->ProcessedImageCount++;
if(IsFrameCorrupt)
UserHookDataPtr->CorruptImageCount++;
MosSprintf(Text, STRING_LENGTH_MAX, MIL_TEXT("%ld"),
UserHookDataPtr->ProcessedImageCount);
MgraText(M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, Text);
MbufCopy(ModifiedBufferId, UserHookDataPtr->MilImageDisp);
#if M_MIL_USE_CE
if(MdigInquire(UserHookDataPtr->MilDigitizer, M_PROCESS_PENDING_GRAB_NUM, M_NULL) <= 1)
{
if ((UserHookDataPtr->ProcessedImageCount%10) == 0)
Sleep(2);
}
#endif
return 0;
}