private void cmdCam3_Click(object sender, EventArgs e) { string str = "c3;"; testMsg.Text = "无"; testMsg.BackColor = Color.Silver; SocketUtils.sendCmdToTarget(inspectSocket, str); //MessageBox.Show("向" + inspectIp.Text + ":" + inspectPort.Text + "发送消息:" + str); var receiveData = SocketUtils.receiveDataFromTarget(inspectSocket, resByteArr); //MessageBox.Show("从" + inspectIp.Text + ":" + inspectPort.Text + "接收到消息:" + receiveData); if (receiveData == "1") { setPlcCmd(plcSocket3, cam3ResAds, " 0001\r\n"); setPlcCmd(plcSocket3, cam3CmdAds, " 0000\r\n"); testMsg.Text = "相机3 OK"; testMsg.BackColor = Color.LimeGreen; } else if (receiveData == "2") { setPlcCmd(plcSocket3, cam3ResAds, " 0002\r\n"); setPlcCmd(plcSocket3, cam3CmdAds, " 0000\r\n"); testMsg.Text = "相机3 NG"; testMsg.BackColor = Color.Red; } }
/// <summary> /// 界面上的相相机触发按钮 /// 在已连接状态下点击即可使对应相机拍照 /// 可以用于测试相机通信 /// 拍照成功后会弹窗显示返回的值,可据此判断连接程序通信格式是否正确 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdCam1_Click(object sender, EventArgs e) { string str = "c1;"; testMsg.Text = "无"; testMsg.BackColor = Color.Silver; SocketUtils.sendCmdToTarget(inspectSocket, str); var receiveData = SocketUtils.receiveDataFromTarget(inspectSocket, resByteArr); if (receiveData == "1") { setPlcCmd(plcSocket1, cam1ResAds, " 0001\r\n"); Thread.Sleep(30); setPlcCmd(plcSocket1, cam1CmdAds, " 0000\r\n"); testMsg.Text = "相机1 OK"; testMsg.BackColor = Color.LimeGreen; } else if (receiveData == "2") { setPlcCmd(plcSocket1, cam1ResAds, " 0002\r\n"); Thread.Sleep(30); setPlcCmd(plcSocket1, cam1CmdAds, " 0000\r\n"); testMsg.Text = "相机1 NG"; testMsg.BackColor = Color.Red; } }
private string readInspect(int camId) { /*InspectUtils.sendCmdToTarget(localSocket, "san;"); * string recStr = InspectUtils.receiveDataFromTarget(localSocket, resBytes); * return recStr;*/ string str = ""; if (camId == 1) { str = "c1;"; } else if (camId == 2) { str = "c2;"; } else if (camId == 3) { str = "c3;"; } SocketUtils.sendCmdToTarget(localSocket, str); var receiveData = SocketUtils.receiveDataFromTarget(localSocket, resBytes); return(receiveData); }
private void timer_Tick(object sender, EventArgs e) { if (inspectOK) { SocketUtils.sendCmdToTarget(plcSocket, readCmd + camCmdAds + "\r\n"); var cmd = SocketUtils.receiveDataFromTarget(plcSocket, new byte[1024]); var indexOf = cmd.IndexOf('\r'); if (indexOf != -1) { cmd = cmd.Substring(0, indexOf); } if (cmd == "11OK0001") { if (camCmdAds == plc1CmdAds) { plcCmd = 1; } if (camCmdAds == plc2CmdAds) { plcCmd = 2; } if (camCmdAds == plc3CmdAds) { plcCmd = 3; } } SocketUtils.sendCmdToTarget(localSocket, "c" + plcCmd + ";"); var receiveData = SocketUtils.receiveDataFromTarget(localSocket, resBytes); if (receiveData == "1") { setPlcCmd(plcSocket, camResAds, " 0001\r\n"); triggerState1.BackColor = Color.LimeGreen; triggerState1.Text = "相机" + plcCmd + " OK*"; } else if (receiveData == "2") { triggerState1.BackColor = Color.Red; triggerState1.Text = "相机" + plcCmd + " NG*"; setPlcCmd(plcSocket, camResAds, " 0002\r\n"); } else { triggerState1.BackColor = Color.Silver; triggerState1.Text = "无*"; } } Thread.Sleep(30); setPlcCmd(plcSocket, camCmdAds, " 0000\r\n"); inspectOK = false; result = ""; plcCmd = 0; }
private int getPlcCmd(Socket socket, string plcAddress) { int enCamId = 0; SocketUtils.sendCmdToTarget(socket, readCmd + plcAddress + "\r\n"); var cmd = SocketUtils.receiveDataFromTarget(socket, new byte[1024]); var indexOf = cmd.IndexOf('\r'); if (indexOf != -1) { cmd = cmd.Substring(0, indexOf); } //更新界面触发状态指示,启动触发超时定时器 if ("11OK0001".Equals(cmd)) { triggerState.BackColor = Color.LimeGreen; inspectOK = true; //开启触发超时定时器 timer.Start(); } else if ("11OK0000".Equals(cmd)) { triggerState.BackColor = Color.Yellow; inspectOK = false; //关闭触发超时定时器 timer.Stop(); timer.Enabled = false; } if (cmd == "11OK0001" && cmd != lastCmd) { if (plcAddress == plc1CmdAds) { enCamId = 1; } if (plcAddress == plc2CmdAds) { enCamId = 2; } if (plcAddress == plc3CmdAds) { enCamId = 3; } } lastCmd = (string)cmd.Clone(); return(enCamId); }