/// <summary> /// PLC Client send message to server /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { PLCClient client = (PLCClient)ar.AsyncState; try { if (client.NetWork.Connected) { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; if (recdata.Length > 0) { Array.Copy(client.buffer, recdata, recdata.Length); if (MessageReceived != null) { MessageReceived.BeginInvoke(client.Name, MainPLCMessage.ConvertByteToMessage(recdata), null, null);//async data output } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { client.DisConnect(); LogerHelper2.ToLog("plc is disconnected now", 2); plcHeartBeatTimer.Stop(); RobotPLCStatus.UpdateRobotPLCStatus("Disconnected"); isPLConLine = false; lstClient.Remove(client); } } } catch (Exception ex) { LogerHelper2.ToLog("testmothod TCPCallBack catch exception " + ex.Message, 3); RobotPLCStatus.UpdateRobotPLCStatus("Disconnected"); plcHeartBeatTimer.Stop(); client.DisConnect(); isPLConLine = false; lstClient.Remove(client); } }
private bool Send(byte[] data, PLCClient client) { try { client.NetWork.GetStream().Write(data, 0, data.Length); } catch (Exception ex) { LogerHelper2.ToLog("methond PLCServer.Send catch exception :" + client.Name + ex.Message, 3); client.DisConnect(); return(false); } return(true); }