private void sendHeartBeat() { try { //Byte[] bytes = new Byte[1]; //bytes[0] = ENQ; //stream.Write(bytes, 0, bytes.Length); SendCadCommand(stream, CADHelpers.GetSeq(), "ENQ"); lastHBSent = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void sendAck() { try { //Byte[] bytes = new Byte[1]; //bytes[0] = ACK; //stream.Write(bytes, 0, bytes.Length); SendCadCommand(stream, CADHelpers.GetSeq(), "ACK"); } catch (Exception ex) { //throw new Exception("ERROR Acking CAD: " + ex.Message); string err = ex.ToString(); } }
public void sendMessage(string command) { if (ConnectStatus()) { if (command.ToUpper() == "ACK") { sendAck(); //return "ACKED"; } else { SendCadCommand(stream, CADHelpers.GetSeq(), command); //return ret; } } }
public void RunListener() //this is the actual listener process { const Byte ETX = 0x03; const Byte ENQ = 0x05; const Byte ACK = 0x06; try { // buffer for message from FSP to CAD Byte[] fsp2cadBytes = new Byte[1024]; // buffer for message from CAD to FSP Byte[] cad2fspBytes = new Byte[256]; // buffer to read 1 byte from FSP Byte[] charBuf = new Byte[1]; tcpListener = new TcpListener(IPAddress.Any, port); tcpListener.Start(); //Socket socket = tcpListener.AcceptSocket(); socket = tcpListener.AcceptSocket(); //while (exitThreadEvent.WaitOne(0)) while (bRunServer) { //using (socket = tcpListener.AcceptSocket()) //{ if (socket.Connected) { ConnectStatus = true; using (stream = new NetworkStream(socket)) { int nRead, msgLength = 0; for (int i = 0; i < fsp2cadBytes.Length; i++) { nRead = stream.Read(charBuf, 0, 1); if (nRead == 0) { //null from stream, shut down return; } if (charBuf[0] == ETX) { //end transmission break; } if (charBuf[0] == ENQ) { //Heartbeat message gotHeartbeat(); break; } fsp2cadBytes[i] = charBuf[0]; msgLength++; } if (msgLength != 0) { string cadText = System.Text.Encoding.ASCII.GetString(fsp2cadBytes, 1, msgLength - 1); //cad2fspBytes[0] = ACK; byte[] cadAck = new byte[1]; cadAck[0] = ACK; stream.Write(cadAck, 0, 1); //msgData = cadText; //msgData = MessageParser.parseIncoming(cadText); DataClasses.GlobalData.cadReceived.Add(DateTime.Now.ToString() + " " + cadText); SQL.SQLCode mySQL = new SQL.SQLCode(); cadMsgID = (Guid)mySQL.logCADMessage("REC", cadText); mySQL = null; //send accept message if (!cadText.Contains("\\AM.") && !cadText.Contains("\\RM.")) { if (cadText.Contains("II")) { //see if the truck is available. bool avail = CADHelpers.truckAvailable(cadText); if (avail == false) { string msg = CADHelpers.makeRM(cadText); //Global.cSender.sendMessage(msg); } else { MessageParser.processMessage(cadText, cadMsgID); string msg = CADHelpers.makeAM(cadText); Global.cSender.sendMessage(msg); } } else { MessageParser.processMessage(cadText, cadMsgID); string msg = CADHelpers.makeAM(cadText); Global.cSender.sendMessage(msg); } } gotMessage(EventArgs.Empty); } stream.Flush(); } } } } catch (Exception ex) { string err = ex.ToString(); } }