|
@@ -60,25 +60,40 @@ namespace CameraModel
|
|
|
|
|
|
private void updateDeviceSuccess()
|
|
|
{
|
|
|
- if(_device != null)
|
|
|
+ try
|
|
|
{
|
|
|
- //MVCC_STRINGVALUE pstValue = new MVCC_STRINGVALUE();
|
|
|
- //m_MyCamera.MV_CC_GetDeviceUserID_NET(ref pstValue);
|
|
|
- string UserDefinedName = _device.DeviceInfo.UserDefinedName;
|
|
|
- AntdUI.Message.success(this, "成功修改型号为:" + UserDefinedName, Font, 5);
|
|
|
+ if (_device != null)
|
|
|
+ {
|
|
|
+ //MVCC_STRINGVALUE pstValue = new MVCC_STRINGVALUE();
|
|
|
+ //m_MyCamera.MV_CC_GetDeviceUserID_NET(ref pstValue);
|
|
|
+ string UserDefinedName = _device.DeviceInfo.UserDefinedName;
|
|
|
+ AntdUI.Message.success(this, "成功修改型号为:" + UserDefinedName, Font, 10);
|
|
|
+ }
|
|
|
}
|
|
|
- // 修改完后,相机释放
|
|
|
- ReleaseCamera();
|
|
|
- pictureBox1.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
- pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
- cbDeviceList.Items.Clear();
|
|
|
- cbDeviceList.Text = null;
|
|
|
- button_enum.Enabled = true;
|
|
|
- button_connect.Enabled = true;
|
|
|
- button_update.Enabled = false;
|
|
|
- button_clear.Enabled = false;
|
|
|
- IsSuccess = false;
|
|
|
- IsConnected = false;
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogListBox("查看当前设备型号失败!");
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Error, "查看当前设备型号失败:\t" + ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // 修改完后,相机释放
|
|
|
+ ReleaseCamera();
|
|
|
+ pictureBox1.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
+ pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
+ cbDeviceList.Items.Clear();
|
|
|
+ cbDeviceList.Text = null;
|
|
|
+ button_enum.Enabled = true;
|
|
|
+ button_connect.Enabled = true;
|
|
|
+ button_update.Enabled = false;
|
|
|
+ button_clear.Enabled = false;
|
|
|
+ if (IsSuccess)
|
|
|
+ {
|
|
|
+ isSuccess = false;
|
|
|
+ }
|
|
|
+ IsConnected = false;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -89,6 +104,7 @@ namespace CameraModel
|
|
|
InitTimer();
|
|
|
}
|
|
|
LogListBox("开始检测设备");
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Info, "开始检测设备");
|
|
|
timer.Start();
|
|
|
|
|
|
}
|
|
@@ -97,6 +113,7 @@ namespace CameraModel
|
|
|
if (timer != null)
|
|
|
{
|
|
|
LogListBox("停止检测设备");
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Info, "停止检测设备");
|
|
|
timer.Stop();
|
|
|
}
|
|
|
}
|
|
@@ -106,15 +123,25 @@ namespace CameraModel
|
|
|
if (flag) // 连接
|
|
|
{
|
|
|
// 定时开启检测相机是否还在连接
|
|
|
- ConnentedTimer.Start();
|
|
|
+ if (!ConnentedTimer.Enabled)
|
|
|
+ {
|
|
|
+ ConnentedTimer.Start();
|
|
|
+ }
|
|
|
LogListBox("设备已连接");
|
|
|
- StopTimer();
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Info, "设备已连接");
|
|
|
+ if (timer.Enabled)
|
|
|
+ {
|
|
|
+ StopTimer();
|
|
|
+ }
|
|
|
button_enum.Enabled = false;
|
|
|
button_connect.Enabled = false;
|
|
|
button_update.Enabled = true;
|
|
|
button_clear.Enabled = true;
|
|
|
|
|
|
-
|
|
|
+ if (_device == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
string ModelName = _device.DeviceInfo.ModelName;
|
|
|
|
|
|
// 查找型号
|
|
@@ -219,10 +246,14 @@ namespace CameraModel
|
|
|
private void OnConnectedTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
{
|
|
|
// 检测是否连接
|
|
|
- if (!_device.IsConnected)
|
|
|
+ if (_device == null || !_device.IsConnected)
|
|
|
{
|
|
|
IsConnected = false;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ IsConnected = true;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
private void Reconnect()
|
|
@@ -283,6 +314,9 @@ namespace CameraModel
|
|
|
else
|
|
|
{
|
|
|
LogListBox("没有检测到相机");
|
|
|
+ button_connect.Enabled = false;
|
|
|
+ pictureBox1.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
+ pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -290,45 +324,59 @@ namespace CameraModel
|
|
|
|
|
|
private bool OpenDevice()
|
|
|
{
|
|
|
- int nRet = -1;
|
|
|
- button_update.Enabled = false;
|
|
|
- button_clear.Enabled = false;
|
|
|
- pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
- if (devInfoList.Count == 0 || cbDeviceList.SelectedIndex == -1)
|
|
|
- {
|
|
|
- LogListBox("没有检测或选择设备!");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // ch:创建设备 | en:Create device
|
|
|
- _device = DeviceFactory.CreateDevice(devInfoList[cbDeviceList.SelectedIndex]);
|
|
|
- IGigEDevice _IGigEDevice = _device as IGigEDevice;
|
|
|
-
|
|
|
- bool accessible = DeviceEnumerator.IsDeviceAccessible(devInfoList[cbDeviceList.SelectedIndex], DeviceAccessMode.AccessExclusive);
|
|
|
- if (accessible)
|
|
|
+ bool is_aotu = false;
|
|
|
+ try
|
|
|
{
|
|
|
- nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
|
|
|
-
|
|
|
- if (nRet != MvError.MV_OK)
|
|
|
+ int nRet = -1;
|
|
|
+ button_update.Enabled = false;
|
|
|
+ button_clear.Enabled = false;
|
|
|
+ pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
+ if (devInfoList.Count == 0 || cbDeviceList.SelectedIndex == -1)
|
|
|
{
|
|
|
- Console.WriteLine("设置静态失败,状态码:" + nRet);
|
|
|
- _IGigEDevice.Dispose();
|
|
|
+ LogListBox("没有检测或选择设备!");
|
|
|
return false;
|
|
|
}
|
|
|
- if (SetIp(_IGigEDevice))
|
|
|
+ is_aotu = timer.Enabled;
|
|
|
+ if (is_aotu)
|
|
|
{
|
|
|
- // 打开并修改
|
|
|
- nRet = _IGigEDevice.Open();
|
|
|
+ timer.Stop();
|
|
|
+ }
|
|
|
+ // ch:创建设备 | en:Create device
|
|
|
+ _device = DeviceFactory.CreateDevice(devInfoList[cbDeviceList.SelectedIndex]);
|
|
|
+ IGigEDevice _IGigEDevice = _device as IGigEDevice;
|
|
|
|
|
|
+ bool accessible = DeviceEnumerator.IsDeviceAccessible(devInfoList[cbDeviceList.SelectedIndex], DeviceAccessMode.AccessExclusive);
|
|
|
+ if (accessible)
|
|
|
+ {
|
|
|
+ nRet = _IGigEDevice.Open();
|
|
|
if (MvError.MV_OK != nRet)
|
|
|
{
|
|
|
- //m_MyCamera.MV_CC_DestroyDevice_NET();
|
|
|
- LogHelper.LoggerMsg(LogLevel.Error, "Device open fail! 打开相机状态码:" + nRet);
|
|
|
- LogListBox("Device open fail!");
|
|
|
- _IGigEDevice.Dispose();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
|
|
|
+
|
|
|
+ if (nRet != MvError.MV_OK)
|
|
|
+ {
|
|
|
+ Console.WriteLine("设置静态失败,状态码:" + nRet);
|
|
|
+ _IGigEDevice.Dispose();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (SetIp(_IGigEDevice))
|
|
|
+ {
|
|
|
+ // 打开并修改
|
|
|
+ nRet = _IGigEDevice.Open();
|
|
|
+
|
|
|
+ if (MvError.MV_OK != nRet)
|
|
|
+ {
|
|
|
+ //m_MyCamera.MV_CC_DestroyDevice_NET();
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Error, "Device open fail! 打开相机状态码:" + nRet);
|
|
|
+ LogListBox("Device open fail!");
|
|
|
+ _IGigEDevice.Dispose();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+ }
|
|
|
pictureBox2.Image = global::CameraModel.Properties.Resources.绿灯;
|
|
|
button_update.Enabled = true;
|
|
|
button_clear.Enabled = true;
|
|
@@ -336,40 +384,49 @@ namespace CameraModel
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return false;
|
|
|
+ SetIp(_IGigEDevice);
|
|
|
+ _IGigEDevice.Dispose();
|
|
|
+ IDeviceInfo deviceInfo = devInfoList[cbDeviceList.SelectedIndex];
|
|
|
+ IGigEDeviceInfo gigeDevInfo = deviceInfo as IGigEDeviceInfo;
|
|
|
+
|
|
|
+ uint nIp1 = ((gigeDevInfo.NetExport & 0xff000000) >> 24);
|
|
|
+ uint nIp2 = ((gigeDevInfo.NetExport & 0x00ff0000) >> 16);
|
|
|
+ uint nIp3 = ((gigeDevInfo.NetExport & 0x0000ff00) >> 8);
|
|
|
+ uint nIp4 = (gigeDevInfo.NetExport & 0x000000ff);
|
|
|
+ string netExportIp = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();
|
|
|
+ //ch:需要重新创建句柄,设置为静态IP方式进行保存 | en: Need to recreate the handle and set it to static IP mode for saving
|
|
|
+ //ch: 创建设备 | en: Create device
|
|
|
+ _device = DeviceFactory.CreateDeviceByIp(ip, netExportIp);
|
|
|
+ if (null == _device)
|
|
|
+ {
|
|
|
+ LogListBox("Create handle fail");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ _IGigEDevice = _device as IGigEDevice;
|
|
|
+ nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
|
|
|
+ if (MvError.MV_OK != nRet)
|
|
|
+ {
|
|
|
+ LogListBox("Set Ip config fail");
|
|
|
+ _IGigEDevice.Dispose();
|
|
|
+ _device = null;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
- else
|
|
|
+ catch(Exception ex)
|
|
|
{
|
|
|
- SetIp(_IGigEDevice);
|
|
|
- _IGigEDevice.Dispose();
|
|
|
- IDeviceInfo deviceInfo = devInfoList[cbDeviceList.SelectedIndex];
|
|
|
- IGigEDeviceInfo gigeDevInfo = deviceInfo as IGigEDeviceInfo;
|
|
|
-
|
|
|
- uint nIp1 = ((gigeDevInfo.NetExport & 0xff000000) >> 24);
|
|
|
- uint nIp2 = ((gigeDevInfo.NetExport & 0x00ff0000) >> 16);
|
|
|
- uint nIp3 = ((gigeDevInfo.NetExport & 0x0000ff00) >> 8);
|
|
|
- uint nIp4 = (gigeDevInfo.NetExport & 0x000000ff);
|
|
|
- string netExportIp = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();
|
|
|
- //ch:需要重新创建句柄,设置为静态IP方式进行保存 | en: Need to recreate the handle and set it to static IP mode for saving
|
|
|
- //ch: 创建设备 | en: Create device
|
|
|
- _device = DeviceFactory.CreateDeviceByIp(ip, netExportIp);
|
|
|
- if (null == _device)
|
|
|
- {
|
|
|
- LogListBox("Create handle fail");
|
|
|
- return false;
|
|
|
- }
|
|
|
- _IGigEDevice = _device as IGigEDevice;
|
|
|
- nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
|
|
|
- if (MvError.MV_OK != nRet)
|
|
|
+ LogListBox("打开设备失败:" + ex.Message);
|
|
|
+ LogHelper.LoggerMsg(LogLevel.Error, "打开设备失败:" + ex);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (is_aotu)
|
|
|
{
|
|
|
- LogListBox("Set Ip config fail");
|
|
|
- _IGigEDevice.Dispose();
|
|
|
- _device = null;
|
|
|
- return false;
|
|
|
+ timer.Start();
|
|
|
}
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
public uint IpToUint(string ipAddress)
|
|
|
{
|
|
@@ -387,7 +444,8 @@ namespace CameraModel
|
|
|
uint ipUint = IpToUint(ip);
|
|
|
uint subnetUint = IpToUint(subnet);
|
|
|
uint gatewayUint = IpToUint(gateway);
|
|
|
- int nRet = _IGigEDevice.ForceIp(ipUint, subnetUint, gatewayUint);
|
|
|
+
|
|
|
+ int nRet = _IGigEDevice.ForceIp(ipUint, subnetUint, gatewayUint); // 重置ip后会自动断连
|
|
|
if (nRet != MvError.MV_OK)
|
|
|
{
|
|
|
Console.WriteLine("设置IP失败,状态码:" + nRet);
|
|
@@ -426,8 +484,8 @@ namespace CameraModel
|
|
|
{
|
|
|
|
|
|
// 查找型号
|
|
|
- string UserDefinedName = _device.DeviceInfo.UserDefinedName;
|
|
|
- input1.Text = GetDeviceId(UserDefinedName);
|
|
|
+ string ModelName = _device.DeviceInfo.ModelName;
|
|
|
+ input1.Text = GetDeviceId(ModelName);
|
|
|
//m_MyCamera.MV_CC_SetDeviceUserID_NET(input1.Text);
|
|
|
int nRet = _device.Parameters.SetStringValue("DeviceUserID", input1.Text);
|
|
|
if (nRet != MvError.MV_OK)
|
|
@@ -437,7 +495,7 @@ namespace CameraModel
|
|
|
return;
|
|
|
}
|
|
|
LogListBox("型号修改成功," + cbDeviceList.SelectedValue + "=====>" + input1.Text);
|
|
|
- string ModelName = _device.DeviceInfo.ModelName;
|
|
|
+ ModelName = _device.DeviceInfo.ModelName;
|
|
|
if (!devices.ContainsKey(ModelName))
|
|
|
{
|
|
|
devices.Add(ModelName, input1.Text);
|
|
@@ -461,18 +519,7 @@ namespace CameraModel
|
|
|
//m_MyCamera.MV_CC_SetDeviceUserID_NET("");
|
|
|
_device.Parameters.SetStringValue("DeviceUserID", "");
|
|
|
LogListBox("型号清空成功," + cbDeviceList.SelectedValue);
|
|
|
- pictureBox1.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
- pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
|
|
|
- if (!DeviceListAcq()) // 枚举相机
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- pictureBox1.Image = global::CameraModel.Properties.Resources.绿灯;
|
|
|
- if (!OpenDevice()) // 打开相机
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- pictureBox2.Image = global::CameraModel.Properties.Resources.绿灯;
|
|
|
+
|
|
|
}
|
|
|
private bool isManual = false;
|
|
|
private void button_auto_Click(object sender, EventArgs e)
|