using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Security.Cryptography; using System.Text; using WatsonWebsocket; namespace Project { public partial class fMain { Byte sendseq = 0; DateTime[] cameraconntimechk = new DateTime[] { DateTime.Now, DateTime.Now }; private void bwConn_DoWork(object sender, DoWorkEventArgs e) { PUB.log_[0].Add("Device connection thread start"); PUB.log_[1].Add("Device connection thread start"); var logIdx = 0; System.Diagnostics.Stopwatch wat = new System.Diagnostics.Stopwatch(); int idx = 0; while (bRunConnection && this.IsDisposed == false && this.Disposing == false) { //Check listener status if (idx == 0) { //left socket listening if (PUB.setting.CameraIndexL >= 0) { logIdx = 0; if (PUB.wsock_[logIdx].IsListening == false) { var tsli = DateTime.Now - PUB.Time_WS_Listen_Try[PUB.setting.CameraIndexL]; if (tsli.TotalSeconds > 5) { //Server retry PUB.log_[logIdx].AddAT($"Server listen retry port:{PUB.setting.listenPortL}"); try { PUB.wsock_[logIdx] = new Class.WebSocket("localhost", PUB.setting.listenPortL); PUB.wsock_[logIdx].Start(); PUB.wsock_[logIdx].MessageReceived += Ws_DataArrival; PUB.wsock_[logIdx].ClientConnected += Ws_Connected; PUB.wsock_[logIdx].ClientDisconnected += Ws_Disconnected; } catch (Exception ex) { PUB.wsock_[logIdx].MessageReceived -= Ws_DataArrival; PUB.wsock_[logIdx].ClientConnected -= Ws_Connected; PUB.wsock_[logIdx].ClientDisconnected -= Ws_Disconnected; PUB.log_[logIdx].AddE("Server retry listen failed:" + ex.Message); } PUB.Time_WS_Listen_Try[PUB.setting.CameraIndexL] = DateTime.Now; } } } idx += 1; } else if (idx == 1) { //right socket listening if (PUB.setting.CameraIndexR >= 0) { logIdx = 1; if (PUB.wsock_[logIdx].IsListening == false) { var tsli = DateTime.Now - PUB.Time_WS_Listen_Try[PUB.setting.CameraIndexR]; if (tsli.TotalSeconds > 5) { //Server retry PUB.log_[logIdx].AddAT($"Server listen retry port:{PUB.setting.listenPortR}"); try { PUB.wsock_[logIdx] = new Class.WebSocket("localhost", PUB.setting.listenPortR); PUB.wsock_[logIdx].Start(); PUB.wsock_[logIdx].MessageReceived += Ws_DataArrival; PUB.wsock_[logIdx].ClientConnected += Ws_Connected; PUB.wsock_[logIdx].ClientDisconnected += Ws_Disconnected; } catch (Exception ex) { PUB.wsock_[logIdx].MessageReceived -= Ws_DataArrival; PUB.wsock_[logIdx].ClientConnected -= Ws_Connected; PUB.wsock_[logIdx].ClientDisconnected -= Ws_Disconnected; PUB.log_[logIdx].AddE("Server retry listen failed:" + ex.Message); } PUB.Time_WS_Listen_Try[PUB.setting.CameraIndexR] = DateTime.Now; } } } idx += 1; } else if (idx == 2) { //Initialization process try { var status = PUB._virtualFG40.InitSystem(); if (status != Crevis.VirtualFG40Library.VirtualFG40Library.MCAM_ERR_SUCCESS) { PUB.log_[0].AddE($"Crevis:System Initialize failed : {status}"); PUB.log_[1].AddE($"Crevis:System Initialize failed : {status}"); } else Console.WriteLine("Camera init ok"); } catch (Exception ex) { Console.WriteLine($"Camera init error {ex.Message}"); } idx += 1; } else if (idx == 3) { //open camera if (camNum > 0 && PUB.setting.CameraIndexL >= 0 && PUB.setting.CameraIndexL < camNum) { bool initok = false; PUB._virtualFG40.IsInitSystem(ref initok); if (initok) { var camIdx = PUB.setting.CameraIndexL; if (camIdx >= camNum) { //Card corresponding to specified index does not exist } else if (PUB._hDevice[camIdx] < 0) { //Camera index is 0,1 but vision index uses 0,2 as before. 1 is for central Keyence PUB.log_[logIdx].Add($"Camera {camIdx} connection in progress"); var Piv = camIdx == PUB.setting.CameraIndexL ? this.pivLeft : this.pIvRight; if (CrevisOpen(camIdx)) PUB.CrevisGrab(camIdx, true, Piv); //Try to capture one image cameraconntimechk[camIdx] = DateTime.Now; } else { //Has been connected before. var ts = DateTime.Now - cameraconntimechk[camIdx]; if (ts.TotalSeconds > 5) { bool iscon = false; var rlt = PUB._virtualFG40.IsOpenDevice(PUB._hDevice[camIdx], ref iscon); if (rlt == Crevis.VirtualFG40Library.VirtualFG40Library.MCAM_ERR_SUCCESS) { var status = PUB._virtualFG40.GetAvailableCameraNum(ref camNum); if (iscon == false) { var Piv = camIdx == PUB.setting.CameraIndexL ? this.pivLeft : this.pIvRight; if (CrevisOpen(camIdx)) PUB.CrevisGrab(camIdx, true, Piv); //Try to capture one image } else { //Already connected. } } else if (rlt == Crevis.VirtualFG40Library.VirtualFG40Library.MCAM_ERR_NO_DEVICE) { //Device not found, refreshing list. PUB._virtualFG40.UpdateDevice(); var status = PUB._virtualFG40.GetAvailableCameraNum(ref camNum); if (camNum <= camIdx) { PUB._virtualFG40.FreeSystem(); PUB.log_[logIdx].AddE("The camera can not be connected."); } //else //{ PUB._hDevice[camIdx] = -1; //Induce reconnection work //} } cameraconntimechk[camIdx] = DateTime.Now; } } } } //Pub.flag.set(eFlag.CHECKCAMERA, false, "LOAD"); idx += 1; } else if (idx == 4) { //open camera bool initok = false; try { PUB._virtualFG40.IsInitSystem(ref initok); } catch (Exception ex) { Console.WriteLine($"Error _virtualFG40.IsInitSystem : {ex.Message}"); initok = false; } if (initok) { var camIdx = PUB.setting.CameraIndexR; if (camIdx >= camNum) { //지정한 인덱스에 해당하는 카드가 존재하지 않음 } else if (camIdx != PUB.setting.CameraIndexL) { if (PUB._hDevice[camIdx] < 0) { //Camera index is 0,1 but vision index uses 0,2 as before. 1 is for central Keyence PUB.log_[logIdx].Add($"Camera {camIdx} connection in progress"); var Piv = camIdx == PUB.setting.CameraIndexL ? this.pivLeft : this.pIvRight; if (CrevisOpen(camIdx)) PUB.CrevisGrab(camIdx, true, Piv); //Try to capture one image cameraconntimechk[camIdx] = DateTime.Now; } else { //최소 셋팅된 번호와 현재 카메라 번호가 다르면 초기화를 해준다. if (FirstCrevisIndex[camIdx] != -1 && FirstCrevisIndex[camIdx] != PUB._hDevice[camIdx]) { PUB.log_[logIdx].AddE($"Camera Index({camIdx}) error"); PUB._hDevice[camIdx] = -1; } else if(PUB._hDevice[0] == PUB._hDevice[1]) //error condition { PUB.log_[logIdx].AddE($"Camera Index({camIdx}) error init"); PUB._hDevice[0] = -1; PUB._hDevice[1] = -1; FirstCrevisIndex[0] = -1; FirstCrevisIndex[1] = -1; } else { //Has been connected before. var ts = DateTime.Now - cameraconntimechk[camIdx]; if (ts.TotalSeconds > 5) { bool iscon = false; var rlt = PUB._virtualFG40.IsOpenDevice(PUB._hDevice[camIdx], ref iscon); if (rlt == Crevis.VirtualFG40Library.VirtualFG40Library.MCAM_ERR_SUCCESS) { var status = PUB._virtualFG40.GetAvailableCameraNum(ref camNum); if (iscon == false) { var Piv = camIdx == PUB.setting.CameraIndexL ? this.pivLeft : this.pIvRight; if (CrevisOpen(camIdx)) PUB.CrevisGrab(camIdx, true, Piv); //Try to capture one image } else { //Already connected. } } else if (rlt == Crevis.VirtualFG40Library.VirtualFG40Library.MCAM_ERR_NO_DEVICE) { //Device not found, refreshing list. PUB._virtualFG40.UpdateDevice(); var status = PUB._virtualFG40.GetAvailableCameraNum(ref camNum); if (camNum <= camIdx) { PUB._virtualFG40.FreeSystem(); PUB.log_[logIdx].AddE("The camera can not be connected."); } //else //{ PUB._hDevice[camIdx] = -1; //Induce reconnection work //} } cameraconntimechk[camIdx] = DateTime.Now; } } } } } // panMiniDisplay.Invalidate(); //Pub.flag.set(eFlag.CHECKCAMERA, false, "LOAD"); idx += 1; } else { idx = 0; } if (sendseq == 0) { if (PUB.setting.CameraIndexL >= 0) SendStatus(eTarget.Left); sendseq += 1; } else if (sendseq == 1) { if (PUB.setting.CameraIndexR >= 0 && PUB.setting.CameraIndexR != PUB.setting.CameraIndexL) SendStatus(eTarget.Right); sendseq += 1; } else sendseq = 0; if (idx > 4) idx = 0; //heartbeat if (this.IsDisposed == false) { this.Invoke(new Action(() => { try { if (this.sbConnState.ForeColor == Color.LimeGreen) this.sbConnState.ForeColor = Color.Green; else this.sbConnState.ForeColor = Color.LimeGreen; } catch { } })); } System.Threading.Thread.Sleep(250); } } } }