- Added QRValidation vision control system - Includes CapCleaningControl UI components - WebSocket-based barcode validation system - Support for Crevis PLC integration - Test projects for PLC emulator, motion, IO panel, and Modbus 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
259 lines
9.6 KiB
C#
259 lines
9.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Crevis.VirtualFG40Library;
|
|
using Emgu.CV;
|
|
using Emgu.CV.CvEnum;
|
|
using Emgu.CV.Structure;
|
|
#if V22
|
|
using Euresys.Open_eVision_22_12;
|
|
using Euresys.Open_eVision_22_12.Easy3D;
|
|
#else
|
|
using Euresys.Open_eVision_2_11;
|
|
#endif
|
|
|
|
namespace Project
|
|
{
|
|
public partial class fMain
|
|
{
|
|
|
|
|
|
UInt32 camNum = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Crevis must be initialized in the main thread
|
|
/// </summary>
|
|
void _SM_RUN_INIT_CAMERA()
|
|
{
|
|
//Crevis Init
|
|
PUB._virtualFG40 = new VirtualFG40Library();
|
|
Int32 status = VirtualFG40Library.MCAM_ERR_SUCCESS;
|
|
try
|
|
{
|
|
//System InitializSend_WSocke
|
|
|
|
status = PUB._virtualFG40.InitSystem();
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
PUB.log_[0].AddE($"Crevis:System Initialize failed : {status}");
|
|
PUB.log_[1].AddE($"Crevis:System Initialize failed : {status}");
|
|
}
|
|
else
|
|
{
|
|
// Update Device List
|
|
//status = _virtualFG40.UpdateDevice();
|
|
//if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
//{
|
|
// _virtualFG40.FreeSystem();
|
|
// throw new Exception(String.Format("Update Device list failed : {0}", status));
|
|
//}
|
|
|
|
status = PUB._virtualFG40.GetAvailableCameraNum(ref camNum);
|
|
if (camNum <= 0)
|
|
{
|
|
PUB._virtualFG40.FreeSystem();
|
|
PUB.log_[0].AddE("The camera can not be connected.");
|
|
PUB.log_[1].AddE("The camera can not be connected.");
|
|
}
|
|
else Console.WriteLine($"Camera {camNum} Found");
|
|
|
|
//camNum = 1;
|
|
}
|
|
|
|
//if (camNum > 0)
|
|
//{
|
|
// //카메라 인덱스는 0,1 이지만 비젼인덱스는 기존대로 0,2번을 사용한다. 1번은 중앙 키엔스용이다
|
|
// Pub.log.Add($"{Pub.setting.CameraIndex}번 카메라 연결 진행");
|
|
// if (CrevisOpen(Pub.setting.CameraIndex)) CrevisGrab(true); //한장을 수집해본다
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PUB.log_[0].AddE("Crevis:" + ex.Message);
|
|
PUB.log_[1].AddE("Crevis:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
int[] FirstCrevisIndex = new int[] { -1, -1 };
|
|
|
|
/// <summary>
|
|
/// Camera open
|
|
/// </summary>
|
|
/// <param name="camIdx"></param>
|
|
/// <returns></returns>
|
|
Boolean CrevisOpen(int camIdx)
|
|
{
|
|
Int32 status = VirtualFG40Library.MCAM_ERR_SUCCESS;
|
|
var logIdx = camIdx == PUB.setting.CameraIndexL ? 0 : 1;
|
|
try
|
|
{
|
|
// camera open
|
|
status = PUB._virtualFG40.OpenDevice((uint)camIdx, ref PUB._hDevice[camIdx]);
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
PUB._virtualFG40.FreeSystem();
|
|
PUB.log_[logIdx].AddE($"Open device failed : {status}");
|
|
PUB._isCrevisOpen[camIdx] = false;
|
|
|
|
return false;
|
|
}
|
|
|
|
if (FirstCrevisIndex[camIdx] == -1)
|
|
FirstCrevisIndex[camIdx] = PUB._hDevice[camIdx];
|
|
|
|
PUB._isCrevisOpen[camIdx] = true;
|
|
|
|
// Call Set Feature
|
|
CrevisSetFeature((uint)camIdx);
|
|
|
|
// Get Width
|
|
status = PUB._virtualFG40.GetIntReg(PUB._hDevice[camIdx], VirtualFG40Library.MCAM_WIDTH, ref PUB._width[camIdx]);
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
throw new Exception(String.Format("Read Register MCAM_WIDTH failed : {0}", status));
|
|
}
|
|
|
|
// Get Height
|
|
status = PUB._virtualFG40.GetIntReg(PUB._hDevice[camIdx], VirtualFG40Library.MCAM_HEIGHT, ref PUB._height[camIdx]);
|
|
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
throw new Exception(String.Format("Read Register MCAM_HEIGHT failed : {0}", status));
|
|
}
|
|
|
|
// Get FPS
|
|
//status = _virtualFG40.GetIntReg(_hDevice, VirtualFG40Library.MCAM_ACQUISITION_FRAME_COUNT, ref _fps);
|
|
//if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
//{
|
|
// throw new Exception(String.Format("Read Register MCAM_ACQUISITION_FRAME_COUNT failed : {0}", status));
|
|
//}
|
|
|
|
|
|
PUB.log_[logIdx].AddI($"Camera({camIdx}) connection complete({PUB._width[camIdx]}x{PUB._height[camIdx]})");
|
|
|
|
uint grabtimeout = 0;
|
|
status = PUB._virtualFG40.GetGrabTimeout(PUB._hDevice[camIdx], ref grabtimeout);
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
throw new Exception(String.Format("Read TimeOut failed : {0}", status));
|
|
}
|
|
else PUB._virtualFG40.SetGrabTimeout(PUB._hDevice[camIdx], 1000); //타임아웃 1초로 지정(기본값:5초) 210113
|
|
|
|
|
|
|
|
// Image buffer allocation
|
|
PUB._bufferSize[camIdx] = PUB._width[camIdx] * PUB._height[camIdx];
|
|
PUB.camPtr[camIdx] = Marshal.AllocHGlobal(PUB._bufferSize[camIdx]);
|
|
|
|
//데이터를 받을 저장소 초기화(흑백)
|
|
var bitsPerPixel = 8;
|
|
PUB._stride[camIdx] = (Int32)((PUB._width[camIdx] * bitsPerPixel + 7) / 8);
|
|
//var imgRect = new Rectangle(0, 0, _width[camIdx], _height[camIdx]);
|
|
//OrgBitmap = new Bitmap(_width, _height, _stride, PixelFormat.Format8bppIndexed, _pImage);
|
|
// SetGrayscalePalette(OrgBitmap);
|
|
|
|
//오픈이비젼은 사용가능하면 처리한다
|
|
//if (OrgEImage[camIdx] != null) OrgEImage[camIdx].Dispose();
|
|
//OrgEImage[camIdx] = new EImageBW8();
|
|
//OrgEImage[camIdx].SetImagePtr(_width[camIdx], _height[camIdx], pImage);
|
|
|
|
//openCv 이미지 처리
|
|
// OrgImage = new Image<Gray, byte>(_width, _height, _stride, _pImage);
|
|
//OrgCImage = new Image<Bgr, byte>(_width, _height, new Bgr(Color.Black));
|
|
|
|
//데이터를 표시할 저장소 초기화 (컬러)
|
|
//OrgBitmapC = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);
|
|
//var lockColor = OrgBitmapC.LockBits(imgRect, ImageLockMode.ReadOnly, OrgBitmapC.PixelFormat);
|
|
//OrgImageC = new Image<Bgr, byte>(_width, _height, lockColor.Stride, lockColor.Scan0);
|
|
//OrgBitmapC.UnlockBits(lockColor);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
PUB._isCrevisOpen[camIdx] = false;
|
|
MakeBlankImage(camIdx, "ERROR"); //빈 이미지 설정
|
|
PUB.log_[logIdx].AddE("Crevis Error:" + ex.Message);// MessageBox.Show(ex.Message);
|
|
}
|
|
|
|
PUB.flag.set(eFlag.CAMERAINIT, PUB._isCrevisOpen[camIdx], "");
|
|
//이미지를 화면에 표시한다.
|
|
//ivL.Image = OrgEImage; ivL.ZoomFit();
|
|
return PUB._isCrevisOpen[camIdx];
|
|
}
|
|
|
|
void MakeBlankImage(int camIdx, string title = "")
|
|
{
|
|
PUB._width[camIdx] = 1024;
|
|
PUB._height[camIdx] = 768;
|
|
|
|
if (PUB.mre[camIdx].WaitOne(100))
|
|
{
|
|
PUB.mre[camIdx].Reset();
|
|
PUB.OrgImage[camIdx] = new EImageBW8();
|
|
PUB.OrgImage[camIdx].SetSize(PUB._width[camIdx], PUB._height[camIdx]);
|
|
PUB.mre[camIdx].Set();
|
|
}
|
|
}
|
|
|
|
private void CrevisSetFeature(uint camIdx)
|
|
{
|
|
Int32 status = VirtualFG40Library.MCAM_ERR_SUCCESS;
|
|
var logIdx = camIdx == PUB.setting.CameraIndexL ? 0 : 1;
|
|
try
|
|
{
|
|
// Set Trigger Mode
|
|
status = PUB._virtualFG40.SetEnumReg(PUB._hDevice[camIdx], VirtualFG40Library.MCAM_TRIGGER_MODE, VirtualFG40Library.TRIGGER_MODE_OFF);
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
throw new Exception(String.Format("Write Register failed : {0}", status));
|
|
}
|
|
|
|
// Set PixelFormat
|
|
status = PUB._virtualFG40.SetEnumReg(PUB._hDevice[camIdx], VirtualFG40Library.MCAM_PIXEL_FORMAT, VirtualFG40Library.PIXEL_FORMAT_MONO8);
|
|
if (status != VirtualFG40Library.MCAM_ERR_SUCCESS)
|
|
{
|
|
throw new Exception(String.Format("Write Register failed : {0}", status));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//MessageBox.Show(ex.Message);
|
|
PUB.log_[logIdx].AddE(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//private void SetGrayscalePalette(Bitmap bitmap)
|
|
//{
|
|
// ColorPalette GrayscalePalette = bitmap.Palette;
|
|
// for (int i = 0; i < 255; i++)
|
|
// {
|
|
// GrayscalePalette.Entries[i] = Color.FromArgb(i, i, i);
|
|
// }
|
|
// bitmap.Palette = GrayscalePalette;
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|