机缘
时光荏苒,两年时间转瞬即逝。即使现在还是能回想起前年的3月1日应公司自主开发的需求被调到新成立的自动化部门。从原来的设备管理员正式进入新手程序员的行列。通过同事的介绍接触到CSDN,当时正在学习c#很多的资源都会跳转到CSDN网站上。很庆幸CSDN能有这么庞大的用户并且有如此海量的资源,很多博主把问题写的十分全面,方便我程序问题的查找,提升了我项目的效率。
收获
这两年接触了很多的内容,虽然不是很精深但是都达到了应用的条件:
1.Halcon和c#联合
使用 Halcon 完成项目的模板匹配,二维码识别,二维码等级确认,铸造二维码的识别,双目系统的搭建并获得点云等。
C#配合进行人机界面的设计,逻辑的设计,评分获取及PLC通讯,调取光机动态库控制光机运行等。
2.Python、thonny
使用pycharm IDE 爬取网络资源。使用thonny进行树莓派串口读取以及图片转文字等初步应用。
3.树莓派、arduino
使用树莓派进行神经网络学习。使用arduino进行串口通讯代替plc,可以实现功能,但是串口用一用就内存大,后来用了一个usb开关继电器完成的。
4.Unity
使用unity进行UR机械手通讯,安装虚拟机通过Linux系统下UR虚拟机进行实时显示机械手状态,后续可以使用socket进行数据传送,UR机械手程序写入待机即可。Unity配置MRTK和pico的Pxr-sdk,实现HoloLens和pico的使用。
5.3Dmax
3Dmax可以导出fbx的模型文件,配合unity进行建模。可以进行简单的模型制作及渲染。
6.Azure Kinect
使用Azure Kinect SDK 的window版,实现保存文件。
7.HoloLens
使用HoloLens制作了一个汽车发动机拆卸的项目,基本模型没有什么问题,但是在进行气门运动时候需要将各坐标点反转以及活塞和连杆同步移动时只单纯的加上铰链关节无法满足效果。所以有添加了移动的设定,这个墨迹点但是结果是实现了 。
8.TouchDesigner
Azure Kinect进行时想导出具体的点云的点,window系统下一直无法完成。上YouTube看见TD可以实现,得出点云之后可以进行视频的各种美化但是还是没有导出。整不好后续还得用虚拟机上ROS。之前做协助机器人时候ROS就没有用明白,一直和教程的结果不一样,后续有时间再研究一下。
9.Piconeo3
使用pico做一个小项目,展现自动化部门的成长的轨迹,正在做ing…
日常
自动化部门主要是进行各个工厂的外观检测的项目,金属表面深度学习等。下班或剩余的时间基本就是按照自己制定的计划进行unity的学习和项目。
成就
放一个c#和海康软件联合的程序,关于标识符检测的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HRMyTool;
using System.Threading;
using System.Runtime.InteropServices;
using NsIOControllerSDK;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.Windows;
using Size = System.Drawing.Size;
using System.Threading.Tasks;
using VM.Core;
using ImageSourceModuleCs;
using GxIAPINET;
using GxIAPINET.Sample.Common;
namespace IOController
{
public partial class IOController : Form
{
IGXFactory m_objIGXFactory = null; ///<Factory对像
IGXDevice m_objIGXDevice = null; ///<设备对像
IGXStream m_objIGXStream = null; ///<流对像
IGXFeatureControl m_objIGXFeatureControl = null; ///<远端设备属性控制器对像
IGXFeatureControl m_objIGXStreamFeatureControl = null; ///<流层属性控制器对象
GxBitmap m_objGxBitmap = null; ///<图像显示类对象
string m_strFilePath = ""; ///<应用程序当前路径
public delegate void Action();
// NPN/PNP State definition
public enum PNP_ENABLE_STATE
{
PNP = 0x01,
NPN = 0x02,
};
/// <summary>
/// 构造函数
/// </summary>
public IOController()
{
InitializeComponent();
// 获取应用程序的当前执行路径
m_strFilePath = Directory.GetCurrentDirectory().ToString();
m_strFilePath = m_strFilePath + "\\GxSingleCamImages";
//查看是否存在此文件
if (!Directory.Exists(m_strFilePath))
{
Directory.CreateDirectory(m_strFilePath);
}
Environment.ExitCode = 1;//获取或设置退出进程的代码
int nRet = CIOControllerSDK.MV_OK;
nRet = CIOControllerSDK.MV_IO_WinIO_Init_CS();
if (nRet != CIOControllerSDK.MV_OK)
{
CIOControllerSDK.MV_IO_WinIO_DeInit_CS();
}
else
{
}
}
int count;
int countNG;
private void fresh()
{
try
{
//刷新界面
m_objIGXFactory = IGXFactory.GetInstance();
m_objIGXFactory.Init();
List<IGXDeviceInfo> listGXDeviceInfo = new List<IGXDeviceInfo>();
__CloseStream();
__CloseDevice();
m_objIGXFactory.UpdateDeviceList(200, listGXDeviceInfo);
// 判断当前连接设备个数
if (listGXDeviceInfo.Count <= 0)
{
MessageBox.Show("未发现设备!");
return;
}
// 如果设备已经打开则关闭,保证相机在初始化出错情况下能再次打开
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
//打开列表第一个设备
m_objIGXDevice = m_objIGXFactory.OpenDeviceBySN(listGXDeviceInfo[0].GetSN(), GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
m_objIGXFeatureControl = m_objIGXDevice.GetRemoteFeatureControl();
//打开流
if (null != m_objIGXDevice)
{
m_objIGXStream = m_objIGXDevice.OpenStream(0);
m_objIGXStreamFeatureControl = m_objIGXStream.GetFeatureControl();
}
m_objGxBitmap = new GxBitmap(m_objIGXDevice, m_pic_ShowImage);
//开启采集流通道
if (null != m_objIGXStream)
{
//RegisterCaptureCallback第一个参数属于用户自定参数(类型必须为引用
//类型),若用户想用这个参数可以在委托函数中进行使用
m_objIGXStream.RegisterCaptureCallback(this, __CaptureCallbackPro);
m_objIGXStream.StartGrab();
}
//发送开采命令
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
}
}
catch (Exception ex)
{
LOG(ex.Message);
}
}
/// <summary>
/// ch:日志消息 | en:Log message
/// </summary>
private void LOG(string strMessage)
{
this.Invoke(new Action(() => { PrintMessage(strMessage); }));
}
private void PrintMessage(string strMessage)
{
string strMsg = "[" + DateTime.Now + "] " + strMessage;
rtbMessage.AppendText(strMsg);
}
private void btnClearMessage_Click(object sender, EventArgs e)
{
rtbMessage.Clear();
rtbMessage.Refresh();
}
/// <summary>
/// ch:保存信息 | en:Save message
/// </summary>
private void btnSaveMessage_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.RestoreDirectory = true;
sfd.Filter = "Text|*.txt|*.ALL|*.*";
sfd.FileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
foreach (string s in rtbMessage.Lines) sw.WriteLine(s);
sw.Close();
fs.Close();
}
sfd.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void VisionController_Resize(object sender, EventArgs e)
{
float[] scale = (float[])Tag;
if (null == scale)
{
return;
}
int i = 2;
List<Control> ctrls = MyTool.GetCtrls(this);
foreach (Control ctrl in ctrls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
}
}
public int GetMainGPIInputLevel()
{
int nRet = CIOControllerSDK.MV_OK;
try
{
byte[] byteStatus = new byte[1024];
nRet = CIOControllerSDK.MV_IO_GetMainInputLevel_CS(ref byteStatus[0]); // 用字节数组接收动态库传过来的字符串
int nGPIStatus = BitConverter.ToInt32(byteStatus, 0);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Getting the electrical level status failed" + "\r\n");
goto MyExit;
}
//活塞到位检测触发
if ((nGPIStatus & 0x01) == 0x01)
{
timer1.Enabled = false;
lblGPI1.BackColor = Color.Red;
Dalay(1000);
//Thread.Sleep(500);
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
}
else
{
LOG("无法抓取图片" + "\r\n");
}
btnProgRun.PerformClick();
timer1.Enabled = true;
}
else
{
lblGPI1.BackColor = Color.Green;
}
//如果timer2停止抓取,需要按此复位键重新开启timer2
if ((nGPIStatus & 0x04) == 0x04)
{
lblGPI3.BackColor = Color.Red;
timer3.Enabled = false;
LOG("复位报警按钮" + "\r\n");
}
else
{
lblGPI3.BackColor = Color.Green;
}
//气缸伸出到位(防止有漏活的情况)
if ((nGPIStatus & 0x02) == 0x02)
{
lblGPI2.BackColor = Color.Red;
}
else
{
lblGPI2.BackColor = Color.Green;
}
MyExit:;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return nRet;
}
private void btGetGPI_Click(object sender, EventArgs e)
{
try
{
GetMainGPIInputLevel();
}
catch (System.Exception ex)
{
LOG(ex.Message);
}
}
public bool Dalay(int ms)
{
DateTime now = DateTime.Now;
int s;
do
{
TimeSpan spand = DateTime.Now - now;
s = spand.Minutes * 60 * 1000 + spand.Seconds * 1000 + spand.Milliseconds;
Application.DoEvents();
}
while (s < ms);
return true;
}
public int cylinderSensor()
{
int nRet = CIOControllerSDK.MV_OK;
try
{
byte[] byteStatus = new byte[1024];
nRet = CIOControllerSDK.MV_IO_GetMainInputLevel_CS(ref byteStatus[0]); // 用字节数组接收动态库传过来的字符串
int nGPIStatus = BitConverter.ToInt32(byteStatus, 0);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Getting the electrical level status failed"+"\r\n");
goto MyExit;
}
//气缸伸出给气(防止有漏活的情况)
if ((nGPIStatus & 0x02) == 0x02)
{
lblGPI2.BackColor = Color.Red;
}
else
{
lblGPI2.BackColor = Color.Green;
timer1.Enabled = true;
}
MyExit:;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return nRet;
}
public int grab()
{
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
}
else
{
LOG("无法抓取图片" + "\r\n");
}
int nRet = CIOControllerSDK.MV_OK;
try
{
byte[] byteStatus = new byte[1024];
nRet = CIOControllerSDK.MV_IO_GetMainInputLevel_CS(ref byteStatus[0]); // 用字节数组接收动态库传过来的字符串
int nGPIStatus = BitConverter.ToInt32(byteStatus, 0);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Getting the electrical level status failed" + "\r\n");
goto MyExit;
}
//活塞到位检测触发
if ((nGPIStatus & 0x01) == 0x01)
{
timer1.Enabled = false;
Thread.Sleep(1000);
m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
Thread.Sleep(1000);
m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
btnProgRun.PerformClick();
lblGPI1.BackColor = Color.Red;
}
else
{
lblGPI1.BackColor = Color.Green;
}
MyExit:;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return nRet;
}
public int resetAlarm()
{
int nRet = CIOControllerSDK.MV_OK;
try
{
byte[] byteStatus = new byte[1024];
nRet = CIOControllerSDK.MV_IO_GetMainInputLevel_CS(ref byteStatus[0]); // 用字节数组接收动态库传过来的字符串
int nGPIStatus = BitConverter.ToInt32(byteStatus, 0);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Getting the electrical level status failed" + "\r\n");
goto MyExit;
}
if ((nGPIStatus & 0x04) == 0x04)
{
lblGPI3.BackColor = Color.Red;
timer1.Enabled = true;
ckbGPO3.Checked = true;
btGPOLow.PerformClick();
ckbGPO3.Checked = false;
LOG("复位报警按钮" + "\r\n");
}
else
{
lblGPI3.BackColor = Color.Green;
}
MyExit:;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return nRet;
}
private void btGPOLow_Click(object sender, EventArgs e)
{
int nRet = CIOControllerSDK.MV_OK;
bool bSelect = false;
try
{
if (ckbGPO1.Checked)
{
bSelect = true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
//LOG("Sending the command of GPO1 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Green;
//LOG("The command of GPO1 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO2.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
//LOG("Sending the command of GPO2 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO2.ForeColor = Color.Black;
}
else
{
ckbGPO2.ForeColor = Color.Green;
//LOG("The command of GPO2 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO3.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO3 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO3.ForeColor = Color.Black;
}
else
{
ckbGPO3.ForeColor = Color.Green;
//LOG("The command of GPO3 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO4.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_4;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO4 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO4.ForeColor = Color.Black;
}
else
{
ckbGPO4.ForeColor = Color.Green;
LOG("The command of GPO4 Low Level settings is sent" + "\r\n");
}
}
if (cbGPO5.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_5;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO5 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
cbGPO5.ForeColor = Color.Black;
}
else
{
cbGPO5.ForeColor = Color.Green;
LOG("The command of GPO5 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO6.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_6;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO6 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO6.ForeColor = Color.Black;
}
else
{
ckbGPO6.ForeColor = Color.Green;
LOG("The command of GPO6 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO7.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_7;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO7 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO7.ForeColor = Color.Black;
}
else
{
ckbGPO7.ForeColor = Color.Green;
LOG("The command of GPO7 Low Level settings is sent" + "\r\n");
}
}
if (ckbGPO8.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_8;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO8 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO8.ForeColor = Color.Black;
}
else
{
ckbGPO8.ForeColor = Color.Green;
LOG("The command of GPO8 Low Level settings is sent" + "\r\n");
}
}
if (bSelect == false)
{
LOG("Disable failed" + "\r\n");
return;
}
}
catch (Exception)
{
LOG("Sending the command failed. Parameters or operations exception occurs" + "\r\n");
}
}
private void btGPOHigh_Click(object sender, EventArgs e)
{
int nRet = CIOControllerSDK.MV_OK;
bool bSelect = false;
try
{
if (ckbGPO1.Checked)
{
bSelect = true;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1, CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Red;
LOG("气缸伸出" + "\r\n");
}
}
if (ckbGPO2.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO2 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO2.ForeColor = Color.Black;
}
else
{
ckbGPO2.ForeColor = Color.Red;
LOG("气缸回退" + "\r\n");
}
}
if (ckbGPO3.Checked)
{
bSelect = bSelect |true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO3 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO3.ForeColor = Color.Black;
}
else
{
ckbGPO3.ForeColor = Color.Red;
LOG("The command of GPO3 High Level settings is sent" + "\r\n");
}
}
if (ckbGPO4.Checked)
{
bSelect = bSelect |true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_4;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO4 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO4.ForeColor = Color.Black;
}
else
{
ckbGPO4.ForeColor = Color.Red;
LOG("The command of GPO4 High Level settings is sent" + "\r\n");
}
}
if (cbGPO5.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_5;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO5 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
cbGPO5.ForeColor = Color.Black;
}
else
{
cbGPO5.ForeColor = Color.Red;
LOG("The command of GPO5 High Level settings is sent" + "\r\n");
}
}
if (ckbGPO6.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_6;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO6 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO6.ForeColor = Color.Black;
}
else
{
ckbGPO6.ForeColor = Color.Red;
LOG("The command of GPO6 High Level settings is sent" + "\r\n");
}
}
if (ckbGPO7.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_7;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO7 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO7.ForeColor = Color.Black;
}
else
{
ckbGPO7.ForeColor = Color.Red;
LOG("The command of GPO7 High Level settings is sent" + "\r\n");
}
}
if (ckbGPO8.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_8;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO8 High Level settings failed. Error code:" + Convert.ToString(nRet, 16) + "\r\n");
ckbGPO8.ForeColor = Color.Black;
}
else
{
ckbGPO8.ForeColor = Color.Red;
LOG("The command of GPO8 High Level settings is sent" + "\r\n");
}
}
if(bSelect == false)
{
LOG("Enable failed.");
return;
}
}
catch (Exception)
{
LOG("Sending the command failed. Parameters or operations exception occurs" + "\r\n");
}
}
private void btnFilePath_Click(object sender, EventArgs e)
{
//打开本地路径并识别.sol后缀文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "VM Sol File|*.sol*";
DialogResult openFileRes = openFileDialog.ShowDialog();
if (DialogResult.OK == openFileRes)
{
string str = openFileDialog.FileName;
LOG(str+ "\r\n");
LOG("VM程序选择成功" + "\r\n");
led1.GridentColor = Color.Green;
VmSolution.Load(str);
LOG("方案加载成功" + "\r\n");
led2.GridentColor = Color.Green;
btnProgRun.Enabled = true;
setGL();
}
else
{
LOG("文件错误" + "\r\n");
}
}
private void btnCleanC_Click(object sender, EventArgs e)
{
writeCounter(0);
writeCounterNG(0);
readCounter();
readCounterNG();
}
private int readCounter()
{
try
{
//读取text文件中的检测数值
FileStream fs = new FileStream(@" D:\P18-P28顶面检测\1.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
textBox1.Text = sr.ReadLine();
count = Convert.ToInt32(textBox1.Text);
sr.Close();
fs.Close();
}
catch (Exception ex)
{
LOG(ex.Message + "\r\n");
}
return count;
}
private int readCounterNG()
{
try
{
//读取text文件中的检测数值
FileStream fs = new FileStream(@" D:\P18-P28顶面检测\2.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
textBox2.Text = sr.ReadLine();
countNG = Convert.ToInt32(textBox2.Text);
sr.Close();
fs.Close();
}
catch (Exception ex)
{
LOG(ex.Message + "\r\n");
}
return countNG;
}
private void writeCounter(int j)
{
FileStream fs = new FileStream(@" D:\P18-P28顶面检测\1.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(j);
sw.Close();
fs.Close();
}
private void writeCounterNG(int k)
{
FileStream fs = new FileStream(@" D:\P18-P28顶面检测\2.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(k);
sw.Close();
fs.Close();
}
private void IOController_Load(object sender, EventArgs e)
{
limit();
fresh();
resetGL();
resetYL();
resetRL();
led1.GridentColor = Color.Red;
led2.GridentColor = Color.Red;
led3.GridentColor = Color.WhiteSmoke;
led4.GridentColor = Color.WhiteSmoke;
led5.GridentColor = Color.WhiteSmoke;
led6.GridentColor = Color.WhiteSmoke;
//读取计数器数量
readCounter();
readCounterNG();
timer1.Enabled = true;
}
//关闭摄像头
private void __CloseDevice()
{
try
{
//关闭设备
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
}
catch (Exception)
{
}
}
void ImageShowAndSave(IFrameData objIFrameData)
{
try
{
m_objGxBitmap.Show(objIFrameData);
}
catch (Exception)
{
}
string stfFileName = @"D:\P18-P28顶面检测\A.jpg"; // 默认的图像保存名称
m_objGxBitmap.SaveBmp(objIFrameData, stfFileName);
}
/// <summary>
/// 回调函数,用于获取图像信息和显示图像
/// </summary>
/// <param name="obj">用户自定义传入参数</param>
/// <param name="objIFrameData">图像信息对象</param>
private void __CaptureCallbackPro(object objUserParam, IFrameData objIFrameData)
{
try
{
IOController objGxSingleCam = objUserParam as IOController;
objGxSingleCam.ImageShowAndSave(objIFrameData);
}
catch (Exception)
{
}
}
private void __CloseStream()
{
try
{
//关闭流
if (null != m_objIGXStream)
{
m_objIGXStream.Close();
m_objIGXStream = null;
m_objIGXStreamFeatureControl = null;
}
}
catch (Exception)
{
}
}
private void setRL()
{
int nRet = CIOControllerSDK.MV_OK;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO3 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO3.ForeColor = Color.Black;
}
else
{
ckbGPO3.ForeColor = Color.Red;
}
}
private void setGL()
{
int nRet = CIOControllerSDK.MV_OK;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1, CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Red;
}
}
private void setYL()
{
int nRet = CIOControllerSDK.MV_OK;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO2 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO2.ForeColor = Color.Black;
}
else
{
ckbGPO2.ForeColor = Color.Red;
}
}
private void resetYL()
{
int nRet = CIOControllerSDK.MV_OK;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Green;
}
}
private void resetGL()
{
int nRet = CIOControllerSDK.MV_OK;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Green;
}
}
private void resetRL()
{
int nRet = CIOControllerSDK.MV_OK;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Green;
}
}
private void btnProgRun_Click(object sender, EventArgs e)
{
resetRL();
resetYL();
//整体计数器
count++;
writeCounter(count);
try
{
ImageSourceModuleTool imageSource = (ImageSourceModuleTool)VmSolution.Instance["流程1.图像源1"];
imageSource.ModuParams.SetParamValue("AddImage", @"C:\Users\Administrator\Desktop\P18-P28顶面检测new\bin\Debug\PersonImg\A.JPG");
VmProcedure procedure = (VmProcedure)VmSolution.Instance["流程1"];
vmRenderControl1.ModuleSource = procedure;
procedure.Run();
var moduResult = procedure.ModuResult;
//vm输出的out (vmID:8,箭头)
int str1 = moduResult.GetOutputInt("out").pIntVal[0];
if (str1 == 1) led3.GridentColor = Color.Green;
else led3.GridentColor = Color.Red;
//vm输出的out0 (vmID:9,B)
int str2 = moduResult.GetOutputInt("out0").pIntVal[0];
if (str2 == 1) led4.GridentColor = Color.Green;
else led4.GridentColor = Color.Red;
//vm输出的out1 (vmID:10,3A)
int str3 = moduResult.GetOutputInt("out1").pIntVal[0];
if (str3 == 1) led5.GridentColor = Color.Green;
else led5.GridentColor = Color.Red;
//vm输出的out2 (vmID:11,宽度)
float flolimit1 = Convert.ToInt32(txtLimit1.Text);
float flolimit2= Convert.ToInt32(txtLimit2.Text);
float str4 = moduResult.GetOutputFloat("out2").pFloatVal[0];
if (flolimit1 < str4 && str4 < flolimit2) led6.GridentColor = Color.Green;
else led6.GridentColor = Color.Red;
if (str1 == 0 || str2 == 0 || str3 == 0 || str4 < flolimit1 || str4 > flolimit2)
{
countNG++;
writeCounterNG(countNG);
setRL();
//出现异常以后把检测拍照停止,外部按钮按下后再进行触发
timer1.Enabled = false;
timer3.Enabled = true;
LOG("活塞检测参数错误!计数器 ToT 数量记录:"+ textBox1.Text + "\r\n");
}
else
{
setYL();
//选择GPO1
//气缸伸出丢气
ckbGPO1.Checked = true;
btGPOLow.PerformClick();
ckbGPO1.Checked = false;
//如果气缸在后位,并且活塞挡住传感器(红色)
if (lblGPI1.BackColor != Color.Green&& lblGPI2.BackColor == Color.Green)
{
//选择GPO2
ckbGPO2.Checked = true;
//气缸回退动作
btGPOHigh.PerformClick();
//气缸回退位置停止0.5s
Thread.Sleep(500);
Thread.Sleep(2000);
//气缸回退丢气
btGPOLow.PerformClick();
//不选GPO2
ckbGPO2.Checked = false;
//选择GPO1
//气缸伸出丢气
ckbGPO1.Checked = true;
btGPOHigh.PerformClick();
ckbGPO1.Checked = false;
timer1.Enabled = true;
}
else
{
//选择GPO2
ckbGPO2.Checked = true;
//气缸回退动作
btGPOHigh.PerformClick();
//气缸回退位置停止0.5s
Thread.Sleep(500);
//气缸回退丢气
btGPOLow.PerformClick();
//不选GPO2
ckbGPO2.Checked = false;
//选择GPO1
//气缸伸出丢气
ckbGPO1.Checked = true;
btGPOHigh.PerformClick();
ckbGPO1.Checked = false;
timer1.Enabled = true;
}
}
}
catch
{
countNG++;
writeCounterNG(countNG);
LOG("活塞位匹配上基本图形(气门)异常" + textBox1.Text + "\r\n");
led3.GridentColor = Color.Red; led4.GridentColor = Color.Red; led5.GridentColor = Color.Red; led6.GridentColor = Color.Red;
setRL();
timer3.Enabled = true;
}
readCounter();
readCounterNG();
}
private void IOController_FormClosed(object sender, FormClosedEventArgs e)
{
resetGL();
resetYL();
resetRL();
}
private void timer1_Tick(object sender, EventArgs e)
{
grab();
}
private void timer2_Tick(object sender, EventArgs e)
{
cylinderSensor();
}
private void timer3_Tick(object sender, EventArgs e)
{
resetAlarm();
}
private void limit()
{
//读取text文件中的检测数值
FileStream fs1 = new FileStream(@" D:\P18-P28顶面检测\limit1.txt", FileMode.Open);
StreamReader sr1 = new StreamReader(fs1);
//limit
txtLimit1.Text = sr1.ReadLine();
sr1.Close();
fs1.Close();
FileStream fs2 = new FileStream(@" D:\P18-P28顶面检测\limit2.txt", FileMode.Open);
StreamReader sr2 = new StreamReader(fs2);
txtLimit2.Text = sr2.ReadLine();
sr2.Close();
fs2.Close();
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs1 = new FileStream(@" D:\P18-P28顶面检测\limit1.txt", FileMode.Create);
StreamWriter sw1 = new StreamWriter(fs1);
sw1.Write(txtLimit1.Text);
sw1.Close();
fs1.Close();
FileStream fs2 = new FileStream(@" D:\P18-P28顶面检测\limit2.txt", FileMode.Create);
StreamWriter sw2 = new StreamWriter(fs2);
sw2.Write(txtLimit2.Text);
sw2.Close();
fs2.Close();
limit();
}
private void txtLimit1_Leave(object sender, EventArgs e)
{
LOG("宽度下限重新设定,设定值:"+ txtLimit1.Text);
}
private void txtLimit2_Leave(object sender, EventArgs e)
{
LOG("宽度上限重新设定,设定值:" + txtLimit2.Text);
}
}
}
憧憬
希望通过努力能多赚点钱,给孩子一个好的生活环境。我就没有什么了,现在所干的技术我很喜欢,挺快乐😄。