set_ip.cpp
Go to the documentation of this file.
00001 #include <cstdio>
00002 #include "prosilica/prosilica.h"
00003 #include <arpa/inet.h>
00004 
00005 void printSettings(const tPvCameraInfo& info, const tPvIpSettings& conf)
00006 {
00007   printf("\t%s - %7s - Unique ID = %lu\n", info.SerialString, info.DisplayName, info.UniqueId);
00008   printf("\tMode:\t\t");
00009   if (conf.ConfigMode & ePvIpConfigPersistent)
00010     printf("FIXED\n");
00011   else if (conf.ConfigMode & ePvIpConfigDhcp)
00012     printf("DHCP&AutoIP\n");
00013   else if (conf.ConfigMode & ePvIpConfigAutoIp)
00014     printf("AutoIP\n");
00015   else
00016     printf("None\n");
00017 
00018   struct in_addr addr;
00019   addr.s_addr = conf.CurrentIpAddress;
00020   printf("\tAddress:\t%s\n", inet_ntoa(addr));
00021   addr.s_addr = conf.CurrentIpSubnet;
00022   printf("\tSubnet:\t\t%s\n", inet_ntoa(addr));
00023   addr.s_addr = conf.CurrentIpGateway;
00024   printf("\tGateway:\t%s\n", inet_ntoa(addr));
00025 }
00026 
00027 int main(int argc, char** argv)
00028 {
00029   if (argc < 2) {
00030     printf("Usage: %s 10.68.0.20 [255.255.255.0] [0.0.0.0]\n", argv[0]);
00031     return 0;
00032   }
00033 
00034   char* netmask = "255.255.255.0";
00035   if (argc >= 3) {
00036     netmask = argv[2];
00037   }
00038 
00039   char* gateway = "0.0.0.0";
00040   if (argc >= 4) {
00041     netmask = argv[3];
00042   }
00043 
00044   prosilica::init();
00045   // Make sure we call prosilica::fini() on exit.
00046   boost::shared_ptr<void> guard(static_cast<void*>(0), boost::bind(prosilica::fini));
00047 
00048   // Check if camera IP is already set
00049   char* ip_address = argv[1];
00050   unsigned long IP = inet_addr(ip_address);
00051   tPvCameraInfo info;
00052   tPvIpSettings conf;
00053   tPvErr err = PvCameraInfoByAddr(IP, &info, &conf);
00054   if (!err) {
00055     printf("Camera found at requested IP address:\n");
00056     printSettings(info, conf);
00057   }
00058   else {
00059     printf("No camera found at %s, trying to change settings of a local camera...\n", ip_address);
00060     size_t num_cams = prosilica::numCameras();
00061     if (num_cams == 0) {
00062       printf("ERROR: No camera detected. Is it plugged in?\n");
00063       return 1;
00064     }
00065     if (num_cams == 2) {
00066       printf("ERROR: Multiple cameras (%u) found. Do you have more than one plugged in?\n", (unsigned)num_cams);
00067       return 1;
00068     }
00069     
00070     printf("Detected camera.\n");
00071     unsigned long uid = prosilica::getGuid(0);
00072     
00073     if (PvCameraInfo(uid, &info)) {
00074       printf("ERROR: could not retrieve camera info.\n");
00075       return 1;
00076     }
00077     if (PvCameraIpSettingsGet(uid, &conf)) {
00078       printf("ERROR: could not retrieve camera IP settings.\n");
00079       return 1;
00080     }
00081     printf("Original settings:\n");
00082     printSettings(info, conf);
00083 
00084     printf("Applying new settings...\n");
00085     conf.ConfigMode = ePvIpConfigPersistent;
00086     //conf.ConfigMode = ePvIpConfigDhcp;
00087     conf.CurrentIpAddress = conf.PersistentIpAddr = IP;
00088     conf.CurrentIpSubnet = conf.PersistentIpSubnet = inet_addr(netmask);
00089     conf.CurrentIpGateway = conf.PersistentIpGateway = inet_addr(gateway);
00090     if (PvCameraIpSettingsChange(uid, &conf)) {
00091       printf("ERROR: Failed to apply the new settings\n");
00092       return 1;
00093     }
00094     printf("New settings:\n");
00095     printSettings(info, conf);
00096   }
00097 
00098   return 0;
00099 }


prosilica_camera
Author(s): Patrick Mihelich
autogenerated on Thu Jan 2 2014 11:46:39