use commplc interface for main thread
This commit is contained in:
@@ -15,7 +15,7 @@ namespace LibComm
|
|||||||
public event OnConnectStatus OnConnectStatus;
|
public event OnConnectStatus OnConnectStatus;
|
||||||
public event OnTrigCamera OnTrigCamera;
|
public event OnTrigCamera OnTrigCamera;
|
||||||
public event OnCameraStatus OnCameraStatus;
|
public event OnCameraStatus OnCameraStatus;
|
||||||
public void Initialize()
|
public void Start()
|
||||||
{
|
{
|
||||||
cc24 = new CC24();
|
cc24 = new CC24();
|
||||||
|
|
||||||
@@ -26,9 +26,12 @@ namespace LibComm
|
|||||||
cc24.NotifyCamAcqEnabled += CC24_NotifyCamAcqEnabled;
|
cc24.NotifyCamAcqEnabled += CC24_NotifyCamAcqEnabled;
|
||||||
cc24.NotifyCamAcqDisabled += CC24_NotifyCamAcqDisabled;
|
cc24.NotifyCamAcqDisabled += CC24_NotifyCamAcqDisabled;
|
||||||
|
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
cc24.Initialize();
|
cc24.Initialize();
|
||||||
cc24.NotifyCamAcqEnable(0);
|
cc24.NotifyCamAcqEnable(0);
|
||||||
cc24.NotifyCamAcqEnable(1);
|
cc24.NotifyCamAcqEnable(1);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ namespace LibComm
|
|||||||
event OnDataReceived OnDataReceived;
|
event OnDataReceived OnDataReceived;
|
||||||
event OnConnectStatus OnConnectStatus;
|
event OnConnectStatus OnConnectStatus;
|
||||||
event OnTrigCamera OnTrigCamera;
|
event OnTrigCamera OnTrigCamera;
|
||||||
void Initialize();
|
event OnCameraStatus OnCameraStatus;
|
||||||
|
void Start();
|
||||||
void Stop();
|
void Stop();
|
||||||
void NoticeCamComplete(int index, byte[] datax);
|
void NoticeCamComplete(int index, byte[] datax);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ using System.Windows.Forms;
|
|||||||
using ThridLibray;
|
using ThridLibray;
|
||||||
using LibDataBase;
|
using LibDataBase;
|
||||||
using LibReadTetraExcel;
|
using LibReadTetraExcel;
|
||||||
|
using LibComm;
|
||||||
|
|
||||||
namespace TetraPackOCR
|
namespace TetraPackOCR
|
||||||
{
|
{
|
||||||
@@ -56,10 +57,7 @@ namespace TetraPackOCR
|
|||||||
|
|
||||||
//创建字段log
|
//创建字段log
|
||||||
ILog log = LogManager.GetLogger(typeof(Form1));
|
ILog log = LogManager.GetLogger(typeof(Form1));
|
||||||
/// <summary>
|
ICommPLC commPLC;
|
||||||
/// 声明一个cc24通讯对象
|
|
||||||
/// </summary>
|
|
||||||
CC24 cc24;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 声明一个PaddleOCR对象
|
/// 声明一个PaddleOCR对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -134,7 +132,7 @@ namespace TetraPackOCR
|
|||||||
myJobManagerDET = null;
|
myJobManagerDET = null;
|
||||||
}
|
}
|
||||||
log.Info("定位VPP文件加载完成");
|
log.Info("定位VPP文件加载完成");
|
||||||
InitializeCC24();
|
InitializeComm();
|
||||||
|
|
||||||
log.Info("模型文件加载完成");
|
log.Info("模型文件加载完成");
|
||||||
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\ON.png");
|
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\ON.png");
|
||||||
@@ -199,7 +197,7 @@ namespace TetraPackOCR
|
|||||||
myJobManagerDET.Shutdown();
|
myJobManagerDET.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseCC24();
|
StopComm();
|
||||||
|
|
||||||
if (myJobManagerDET == null)
|
if (myJobManagerDET == null)
|
||||||
System.Environment.Exit(0);
|
System.Environment.Exit(0);
|
||||||
@@ -917,7 +915,6 @@ namespace TetraPackOCR
|
|||||||
m_GotoZero = false;
|
m_GotoZero = false;
|
||||||
m_startTime = DateTime.Now;
|
m_startTime = DateTime.Now;
|
||||||
m_textWidth = 18 - (config.NumberOfLanes - 5);
|
m_textWidth = 18 - (config.NumberOfLanes - 5);
|
||||||
//m_textWidth = Convert.ToDouble(textBox1.Text);
|
|
||||||
ocrAcc.Clear();
|
ocrAcc.Clear();
|
||||||
autorunFlag = check_Autorun.Checked;
|
autorunFlag = check_Autorun.Checked;
|
||||||
btn_StarDet_manual.Enabled = false;
|
btn_StarDet_manual.Enabled = false;
|
||||||
@@ -932,20 +929,6 @@ namespace TetraPackOCR
|
|||||||
m_BeginOCR = true;
|
m_BeginOCR = true;
|
||||||
}
|
}
|
||||||
private bool m_BeginOCR = false;
|
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>
|
||||||
/// 清空上次订单数据
|
/// 清空上次订单数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1353,19 +1336,12 @@ namespace TetraPackOCR
|
|||||||
private void NoticePLCCompleteOCR()
|
private void NoticePLCCompleteOCR()
|
||||||
{
|
{
|
||||||
if (autorunFlag == false)
|
if (autorunFlag == false)
|
||||||
{
|
commPLC?.NoticeCamComplete(1, DataConverter.FloatToByte(0.0f, true));
|
||||||
byte[] datax = DataConverter.FloatToByte(0.0f, true);
|
|
||||||
cc24?.NotifyCamInspectionComplete(1, datax);
|
|
||||||
cc24?.NotifyCamAcqComplete(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private void NoticePLCCompleteDet(byte[] datax)
|
private void NoticePLCCompleteDet(byte[] datax)
|
||||||
{
|
{
|
||||||
if (autorunFlag == false)
|
if (autorunFlag == false)
|
||||||
{
|
commPLC?.NoticeCamComplete(0, datax);
|
||||||
cc24?.NotifyCamInspectionComplete(0, datax);
|
|
||||||
cc24?.NotifyCamAcqComplete(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private OCRTextResult OCRBytes(byte[] ocrimagebyte)
|
private OCRTextResult OCRBytes(byte[] ocrimagebyte)
|
||||||
{
|
{
|
||||||
@@ -1662,38 +1638,16 @@ namespace TetraPackOCR
|
|||||||
#region 通讯相关
|
#region 通讯相关
|
||||||
|
|
||||||
#region 通讯初始化
|
#region 通讯初始化
|
||||||
void InitializeCC24()
|
void InitializeComm()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cc24 = new CC24();
|
commPLC = new CommWithCC24();
|
||||||
|
commPLC.OnConnectStatus += CommPLC_OnConnectStatus;
|
||||||
cc24.VisionReceivedNewUserData += CC24_NewUserDataReceived;
|
commPLC.OnTrigCamera += CommPLC_OnTrigCamera;
|
||||||
cc24.PlcConnectionStatusChanged += CC24_PlcConnectionStatusChanged;
|
commPLC.OnDataReceived += CommPLC_OnDataReceived;
|
||||||
cc24.PlcTriggerCamAcqStart += CC24_PlcTriggerCamAcqStart;
|
commPLC.OnCameraStatus += CommPLC_OnCameraStatus;
|
||||||
cc24.PlcTriggerCamAcqStop += CC24_PlcTriggerCamAcqStop;
|
commPLC.Start();
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -1701,52 +1655,28 @@ namespace TetraPackOCR
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region 相机采集使能状态
|
private void CommPLC_OnCameraStatus(int index, bool enable)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (InvokeRequired)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = cc24.ReadBytesFromPLC(0, 4);
|
|
||||||
float mMatchingStrf = DataConverter.ByteToFloat(data, true);
|
float mMatchingStrf = DataConverter.ByteToFloat(data, true);
|
||||||
mMatchingStr = Convert.ToInt32(mMatchingStrf);
|
mMatchingStr = Convert.ToInt32(mMatchingStrf);
|
||||||
|
|
||||||
@@ -1786,6 +1716,55 @@ namespace TetraPackOCR
|
|||||||
log.Info("当前检测合格,正常结束");
|
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 bool m_GotoZero = false;
|
||||||
private void button2_Click(object sender, EventArgs e)
|
private void button2_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
@@ -1805,66 +1784,6 @@ namespace TetraPackOCR
|
|||||||
log.Info("零点坐标已发送");
|
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)
|
private void SendToPLC(double x, double y, double r)
|
||||||
@@ -1965,17 +1884,17 @@ namespace TetraPackOCR
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 通讯关闭
|
#region 通讯关闭
|
||||||
private void CloseCC24()
|
private void StopComm()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cc24.VisionReceivedNewUserData -= CC24_NewUserDataReceived;
|
if (commPLC == null)
|
||||||
cc24.PlcConnectionStatusChanged -= CC24_PlcConnectionStatusChanged;
|
return;
|
||||||
cc24.PlcTriggerCamAcqStart -= CC24_PlcTriggerCamAcqStart;
|
commPLC.OnConnectStatus -= CommPLC_OnConnectStatus;
|
||||||
cc24.PlcTriggerCamAcqStop -= CC24_PlcTriggerCamAcqStop;
|
commPLC.OnTrigCamera -= CommPLC_OnTrigCamera;
|
||||||
cc24.NotifyCamAcqEnabled -= CC24_NotifyCamAcqEnabled;
|
commPLC.OnDataReceived -= CommPLC_OnDataReceived;
|
||||||
cc24.NotifyCamAcqDisabled -= CC24_NotifyCamAcqDisabled;
|
commPLC.OnCameraStatus -= CommPLC_OnCameraStatus;
|
||||||
cc24.Shutdown();
|
commPLC.Stop();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user