use commplc interface for main thread
This commit is contained in:
@@ -15,7 +15,7 @@ namespace LibComm
|
||||
public event OnConnectStatus OnConnectStatus;
|
||||
public event OnTrigCamera OnTrigCamera;
|
||||
public event OnCameraStatus OnCameraStatus;
|
||||
public void Initialize()
|
||||
public void Start()
|
||||
{
|
||||
cc24 = new CC24();
|
||||
|
||||
@@ -26,9 +26,12 @@ namespace LibComm
|
||||
cc24.NotifyCamAcqEnabled += CC24_NotifyCamAcqEnabled;
|
||||
cc24.NotifyCamAcqDisabled += CC24_NotifyCamAcqDisabled;
|
||||
|
||||
cc24.Initialize();
|
||||
cc24.NotifyCamAcqEnable(0);
|
||||
cc24.NotifyCamAcqEnable(1);
|
||||
Task.Run(() =>
|
||||
{
|
||||
cc24.Initialize();
|
||||
cc24.NotifyCamAcqEnable(0);
|
||||
cc24.NotifyCamAcqEnable(1);
|
||||
});
|
||||
}
|
||||
public void Stop()
|
||||
{
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace LibComm
|
||||
event OnDataReceived OnDataReceived;
|
||||
event OnConnectStatus OnConnectStatus;
|
||||
event OnTrigCamera OnTrigCamera;
|
||||
void Initialize();
|
||||
event OnCameraStatus OnCameraStatus;
|
||||
void Start();
|
||||
void Stop();
|
||||
void NoticeCamComplete(int index, byte[] datax);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ using System.Windows.Forms;
|
||||
using ThridLibray;
|
||||
using LibDataBase;
|
||||
using LibReadTetraExcel;
|
||||
using LibComm;
|
||||
|
||||
namespace TetraPackOCR
|
||||
{
|
||||
@@ -56,10 +57,7 @@ namespace TetraPackOCR
|
||||
|
||||
//创建字段log
|
||||
ILog log = LogManager.GetLogger(typeof(Form1));
|
||||
/// <summary>
|
||||
/// 声明一个cc24通讯对象
|
||||
/// </summary>
|
||||
CC24 cc24;
|
||||
ICommPLC commPLC;
|
||||
/// <summary>
|
||||
/// 声明一个PaddleOCR对象
|
||||
/// </summary>
|
||||
@@ -134,7 +132,7 @@ namespace TetraPackOCR
|
||||
myJobManagerDET = null;
|
||||
}
|
||||
log.Info("定位VPP文件加载完成");
|
||||
InitializeCC24();
|
||||
InitializeComm();
|
||||
|
||||
log.Info("模型文件加载完成");
|
||||
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\ON.png");
|
||||
@@ -199,7 +197,7 @@ namespace TetraPackOCR
|
||||
myJobManagerDET.Shutdown();
|
||||
}
|
||||
|
||||
CloseCC24();
|
||||
StopComm();
|
||||
|
||||
if (myJobManagerDET == null)
|
||||
System.Environment.Exit(0);
|
||||
@@ -917,7 +915,6 @@ namespace TetraPackOCR
|
||||
m_GotoZero = false;
|
||||
m_startTime = DateTime.Now;
|
||||
m_textWidth = 18 - (config.NumberOfLanes - 5);
|
||||
//m_textWidth = Convert.ToDouble(textBox1.Text);
|
||||
ocrAcc.Clear();
|
||||
autorunFlag = check_Autorun.Checked;
|
||||
btn_StarDet_manual.Enabled = false;
|
||||
@@ -932,20 +929,6 @@ namespace TetraPackOCR
|
||||
m_BeginOCR = true;
|
||||
}
|
||||
private bool m_BeginOCR = false;
|
||||
private List<string> ExecelGetOcrText(string design, string layer)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
string[] ll = layer.Replace("[", "").Replace("]", "").Split(',');
|
||||
for (int i = 0; i < ll.Length; i++)
|
||||
{
|
||||
ll[i] = ll[i].Replace(" ", "");
|
||||
string[] str = ll[i].Split('-');
|
||||
if (str.Length < 2)
|
||||
continue;
|
||||
result.Add(design + str[1] + str[0]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 清空上次订单数据
|
||||
/// </summary>
|
||||
@@ -1353,19 +1336,12 @@ namespace TetraPackOCR
|
||||
private void NoticePLCCompleteOCR()
|
||||
{
|
||||
if (autorunFlag == false)
|
||||
{
|
||||
byte[] datax = DataConverter.FloatToByte(0.0f, true);
|
||||
cc24?.NotifyCamInspectionComplete(1, datax);
|
||||
cc24?.NotifyCamAcqComplete(1);
|
||||
}
|
||||
commPLC?.NoticeCamComplete(1, DataConverter.FloatToByte(0.0f, true));
|
||||
}
|
||||
private void NoticePLCCompleteDet(byte[] datax)
|
||||
{
|
||||
if (autorunFlag == false)
|
||||
{
|
||||
cc24?.NotifyCamInspectionComplete(0, datax);
|
||||
cc24?.NotifyCamAcqComplete(0);
|
||||
}
|
||||
commPLC?.NoticeCamComplete(0, datax);
|
||||
}
|
||||
private OCRTextResult OCRBytes(byte[] ocrimagebyte)
|
||||
{
|
||||
@@ -1662,38 +1638,16 @@ namespace TetraPackOCR
|
||||
#region 通讯相关
|
||||
|
||||
#region 通讯初始化
|
||||
void InitializeCC24()
|
||||
void InitializeComm()
|
||||
{
|
||||
try
|
||||
{
|
||||
cc24 = new CC24();
|
||||
|
||||
cc24.VisionReceivedNewUserData += CC24_NewUserDataReceived;
|
||||
cc24.PlcConnectionStatusChanged += CC24_PlcConnectionStatusChanged;
|
||||
cc24.PlcTriggerCamAcqStart += CC24_PlcTriggerCamAcqStart;
|
||||
cc24.PlcTriggerCamAcqStop += CC24_PlcTriggerCamAcqStop;
|
||||
cc24.NotifyCamAcqEnabled += CC24_NotifyCamAcqEnabled;
|
||||
cc24.NotifyCamAcqDisabled += CC24_NotifyCamAcqDisabled;
|
||||
|
||||
|
||||
|
||||
cc24.Initialize();
|
||||
cc24.NotifyCamAcqEnable(0);
|
||||
cc24.NotifyCamAcqEnable(1);
|
||||
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
if (cc24.SignalState.PlcConnectionStatus)
|
||||
{
|
||||
ttls_PCLStatusShow.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ttls_PCLStatusShow.Visible = false;
|
||||
|
||||
}
|
||||
}));
|
||||
|
||||
commPLC = new CommWithCC24();
|
||||
commPLC.OnConnectStatus += CommPLC_OnConnectStatus;
|
||||
commPLC.OnTrigCamera += CommPLC_OnTrigCamera;
|
||||
commPLC.OnDataReceived += CommPLC_OnDataReceived;
|
||||
commPLC.OnCameraStatus += CommPLC_OnCameraStatus;
|
||||
commPLC.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1701,52 +1655,28 @@ namespace TetraPackOCR
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 相机采集使能状态
|
||||
private void CC24_NotifyCamAcqEnabled(int cameraIndex, bool isEnabled)
|
||||
{
|
||||
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
if (isEnabled)
|
||||
{
|
||||
ttls_AcqEnableShow.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ttls_AcqEnableShow.Visible = false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private void CC24_NotifyCamAcqDisabled(int cameraIndex, bool isEnabled)
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
if (isEnabled)
|
||||
{
|
||||
ttls_AcqEnableShow.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ttls_AcqEnableShow.Visible = false;
|
||||
}
|
||||
}));
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 接收PLC数据
|
||||
void CC24_NewUserDataReceived(object sender, CogNdmNewUserDataEventArgs e)
|
||||
private void CommPLC_OnCameraStatus(int index, bool enable)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(new CogNdmNewUserDataEventHandler(CC24_NewUserDataReceived), new object[] { sender, e });
|
||||
BeginInvoke(new Action<int, bool>(CommPLC_OnCameraStatus), index, enable);
|
||||
return;
|
||||
}
|
||||
if (enable)
|
||||
ttls_AcqEnableShow.Visible = true;
|
||||
else
|
||||
ttls_AcqEnableShow.Visible = false;
|
||||
}
|
||||
|
||||
private void CommPLC_OnDataReceived(byte[] data)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
BeginInvoke(new Action<byte[]>(CommPLC_OnDataReceived), data);
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] data = cc24.ReadBytesFromPLC(0, 4);
|
||||
float mMatchingStrf = DataConverter.ByteToFloat(data, true);
|
||||
mMatchingStr = Convert.ToInt32(mMatchingStrf);
|
||||
|
||||
@@ -1786,6 +1716,55 @@ namespace TetraPackOCR
|
||||
log.Info("当前检测合格,正常结束");
|
||||
}
|
||||
}
|
||||
|
||||
private void CommPLC_OnTrigCamera(int index)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
BeginInvoke(new Action<int>(CommPLC_OnTrigCamera), index);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (index == 1)
|
||||
{
|
||||
Ocr_picBox.BackgroundImage = null;
|
||||
Thread.Sleep(1000);
|
||||
OcrCamTriger();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Error($"Trigger AcqStart Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void CommPLC_OnConnectStatus(bool connected)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
BeginInvoke(new Action<bool>(CommPLC_OnConnectStatus), connected);
|
||||
return;
|
||||
}
|
||||
if (connected)
|
||||
{
|
||||
PlcContinueFlag = true;
|
||||
log.Info("PLC已连接PC,可以进行相关操作");
|
||||
ttls_PCLStatusShow.Visible = true;
|
||||
button2.Enabled = true;
|
||||
button2.BackColor = Color.LimeGreen;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlcContinueFlag = false;
|
||||
log.Info("PLC已断开PC,请查看相关设备是否连接");
|
||||
ttls_PCLStatusShow.Visible = false;
|
||||
button2.Enabled = false;
|
||||
button2.BackColor = Color.LightGray;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private bool m_GotoZero = false;
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
@@ -1805,68 +1784,8 @@ namespace TetraPackOCR
|
||||
log.Info("零点坐标已发送");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 接收触发相机信号
|
||||
void CC24_PlcTriggerCamAcqStart(object sender, CogNdmTriggerAcquisitionEventArgs e)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(new CogNdmTriggerAcquisitionEventHandler(CC24_PlcTriggerCamAcqStart), new object[] { sender, e });
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (e.CameraIndex == 1)
|
||||
{
|
||||
Ocr_picBox.BackgroundImage = null;
|
||||
Thread.Sleep(1000);
|
||||
OcrCamTriger();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Error($"Trigger AcqStart Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 相机停止
|
||||
private void CC24_PlcTriggerCamAcqStop(object sender, CogNdmTriggerAcquisitionStopEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 通讯状态发生改变
|
||||
private void CC24_PlcConnectionStatusChanged(object sender, CogNdmProtocolStatusChangedEventArgs e)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(new CogNdmProtocolStatusChangedEventHandler(CC24_PlcConnectionStatusChanged), new object[] { sender, e });
|
||||
return;
|
||||
}
|
||||
if (e.ProtocolStatus == CogNdmConnectionStatusConstants.Connected)
|
||||
{
|
||||
PlcContinueFlag = true;
|
||||
log.Info("PLC已连接PC,可以进行相关操作");
|
||||
ttls_PCLStatusShow.Visible = true;
|
||||
button2.Enabled = true;
|
||||
button2.BackColor = Color.LimeGreen;
|
||||
}
|
||||
else if (e.ProtocolStatus != CogNdmConnectionStatusConstants.Connecting)
|
||||
{
|
||||
PlcContinueFlag = false;
|
||||
log.Info("PLC已断开PC,请查看相关设备是否连接");
|
||||
ttls_PCLStatusShow.Visible = false;
|
||||
button2.Enabled = false;
|
||||
button2.BackColor = Color.LightGray;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 通讯发送坐标
|
||||
#region 通讯发送坐标
|
||||
private void SendToPLC(double x, double y, double r)
|
||||
{
|
||||
try
|
||||
@@ -1965,17 +1884,17 @@ namespace TetraPackOCR
|
||||
#endregion
|
||||
|
||||
#region 通讯关闭
|
||||
private void CloseCC24()
|
||||
private void StopComm()
|
||||
{
|
||||
try
|
||||
{
|
||||
cc24.VisionReceivedNewUserData -= CC24_NewUserDataReceived;
|
||||
cc24.PlcConnectionStatusChanged -= CC24_PlcConnectionStatusChanged;
|
||||
cc24.PlcTriggerCamAcqStart -= CC24_PlcTriggerCamAcqStart;
|
||||
cc24.PlcTriggerCamAcqStop -= CC24_PlcTriggerCamAcqStop;
|
||||
cc24.NotifyCamAcqEnabled -= CC24_NotifyCamAcqEnabled;
|
||||
cc24.NotifyCamAcqDisabled -= CC24_NotifyCamAcqDisabled;
|
||||
cc24.Shutdown();
|
||||
if (commPLC == null)
|
||||
return;
|
||||
commPLC.OnConnectStatus -= CommPLC_OnConnectStatus;
|
||||
commPLC.OnTrigCamera -= CommPLC_OnTrigCamera;
|
||||
commPLC.OnDataReceived -= CommPLC_OnDataReceived;
|
||||
commPLC.OnCameraStatus -= CommPLC_OnCameraStatus;
|
||||
commPLC.Stop();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user