浏览代码

2025/5/13
基本能使用

ruanzl 3 周之前
父节点
当前提交
45a18e8cc8
共有 1 个文件被更改,包括 118 次插入39 次删除
  1. 118 39
      CameraModel/Form1.cs

+ 118 - 39
CameraModel/Form1.cs

@@ -5,6 +5,7 @@ using System.Data;
 using System.Drawing;
 using System.IO;
 using System.Linq;
+using System.Net;
 using System.Runtime.InteropServices;
 using System.Runtime.Remoting.Contexts;
 using System.Text;
@@ -17,6 +18,7 @@ using System.Windows.Forms;
 using MvCamCtrl.NET;
 using MvCameraControl;
 using Vanara.PInvoke;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 using static AntdUI.Math3D;
 using static CameraModel.Form1;
 using static MvCamCtrl.NET.MyCamera;
@@ -63,7 +65,7 @@ namespace CameraModel
 
             //// ch: 枚举设备 | en: Enum Device List
             //DeviceListAcq();
-
+            SDKSystem.Initialize();
             // 初始化定时器
             InitTimer();
             StartTimer();
@@ -73,10 +75,13 @@ namespace CameraModel
 
         private void updateDeviceSuccess()
         {
-            //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 || _device.IsConnected)
+            {
+                //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);
+            }
             // 修改完后,相机释放
             ReleaseCamera();
             pictureBox1.Image = global::CameraModel.Properties.Resources.电力_红灯;
@@ -89,9 +94,6 @@ namespace CameraModel
             button_clear.Enabled = false;
             IsSuccess = false;
             IsConnected = false;
-      
-            
-
         }
 
 
@@ -191,7 +193,7 @@ namespace CameraModel
             string UserDefinedName = _device.DeviceInfo.UserDefinedName;
             if (!UserDefinedName.Equals(input1.Text))
             {
-                MessageBox.Show("更改失败");
+                //MessageBox.Show("更改失败");
                 return;
             }
         }
@@ -273,6 +275,7 @@ namespace CameraModel
             button_clear.Enabled = false;
             button_connect.Enabled = false;
             devInfoList.Clear();
+            cbDeviceList.Items.Clear();
             // ch:枚举设备 | en:Enum device
             int ret = DeviceEnumerator.EnumDevices(devLayerType, out devInfoList);
             if (ret != MvError.MV_OK)
@@ -288,8 +291,19 @@ namespace CameraModel
             // ch:选择第一项 | en:Select the first item
             if (devInfoList.Count != 0)
             {
-                cbDeviceList.SelectedIndex = 0;
-                cbDeviceList.Text = cbDeviceList.Items[cbDeviceList.SelectedIndex] + "";
+                
+                if (cbDeviceList.InvokeRequired)
+                {
+                    cbDeviceList.Invoke(new Action(() => {
+                        cbDeviceList.SelectedIndex = 0;
+                        cbDeviceList.Text = cbDeviceList.Items[cbDeviceList.SelectedIndex] + "";
+                    }));
+                }
+                else
+                {
+                    cbDeviceList.SelectedIndex = 0;
+                    cbDeviceList.Text = cbDeviceList.Items[cbDeviceList.SelectedIndex] + "";
+                }
                 pictureBox1.Image = global::CameraModel.Properties.Resources.绿灯;
                 button_connect.Enabled = true;
                 return true;
@@ -314,6 +328,7 @@ namespace CameraModel
         }
         private bool OpenDevice()
         {
+            int nRet = -1;
             button_update.Enabled = false;
             button_clear.Enabled = false;
             pictureBox2.Image = global::CameraModel.Properties.Resources.电力_红灯;
@@ -325,37 +340,101 @@ namespace CameraModel
 
             // ch:创建设备 | en:Create device
             _device = DeviceFactory.CreateDevice(devInfoList[cbDeviceList.SelectedIndex]);
+            IGigEDevice _IGigEDevice = _device as IGigEDevice;
 
-            //IGigEDevice _IGigEDevice = _device as IGigEDevice;
-            //int nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
-            //if(nRet != MvError.MV_OK)
-            //{
-            //    Console.WriteLine("设置静态");
-            //    return false;
-            //}
-            //nRet = _IGigEDevice.Parameters.SetStringValue("IP", "192.168.1.12");
-            //if (nRet != MvError.MV_OK)
-            //{
-            //    Console.WriteLine("设置IP");
-            //    return false;
-            //}
-            ////192.168.1.
-            //_device = DeviceFactory.CreateDeviceByIp("192.168.1.12", "192.168.1.13");
-
-            int nRet = _device.Open();
-
-            if (MvError.MV_OK != nRet)
-            {
-                //m_MyCamera.MV_CC_DestroyDevice_NET();
-                LogHelper.LoggerMsg(LogLevel.Error, "Device open fail! 打开相机状态码:" + nRet);
-                LogListBox("Device open fail!");
-                return false;
-            }
+            bool accessible = DeviceEnumerator.IsDeviceAccessible(devInfoList[cbDeviceList.SelectedIndex], DeviceAccessMode.AccessExclusive);
+            if (accessible)
+            {
+                nRet = _IGigEDevice.SetIpConfig(IpConfigType.Static);
 
+                if (nRet != MvError.MV_OK)
+                {
+                    Console.WriteLine("设置静态失败,状态码:" + nRet);
+                    _IGigEDevice.Dispose();
+                    return false;
+                }
+                if (SetIp(_IGigEDevice))
+                {
+                    // 打开并修改
+                    nRet = _IGigEDevice.Open();
 
-            pictureBox2.Image = global::CameraModel.Properties.Resources.绿灯;
-            button_update.Enabled = true;
-            button_clear.Enabled = true;
+                    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;
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
+            }
+            else
+            {
+                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("192.168.1.12", 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;
+        }
+        public uint IpToUint(string ipAddress)
+        {
+            IPAddress clsIpAddr;
+            if (false == IPAddress.TryParse(ipAddress, out clsIpAddr))
+            {
+                LogListBox("Please enter correct IP");
+                return 0;
+            }
+            return (uint)(IPAddress.NetworkToHostOrder(clsIpAddr.Address) >> 32);
+        }
+        private bool SetIp(IGigEDevice _IGigEDevice)
+        {
+            string ip = "192.168.1.12";
+            string subnet = "255.255.255.0";
+            string gateway = "192.168.1.254";
+
+            uint ipUint = IpToUint(ip);
+            uint subnetUint = IpToUint(subnet);
+            uint gatewayUint = IpToUint(gateway);
+            int nRet = _IGigEDevice.ForceIp(ipUint, subnetUint, gatewayUint);
+            if (nRet != MvError.MV_OK)
+            {
+                Console.WriteLine("设置IP失败,状态码:" + nRet);
+                _IGigEDevice.Dispose();
+                return false;
+            }
             return true;
         }
         private void LogListBox(string content)