private void SaveResponse(string response, bool IsManualCmd) { try { strTmpRequest = response.Split(",".ToCharArray())[1]; if (strTmpRequest == "ALARMS") { strTmpResponse = response.Split(",".ToCharArray())[2]; strTmpResponse = DataStructures.Converter.HexaToString(strTmpResponse, strTmpRequest); if (UI_Enabled_Process) { if (!int.TryParse(strTmpResponse, out Main.lastAlarmValue)) { Main.lastAlarmValue = 0; } } else { if (!int.TryParse(strTmpResponse, out Main_noUI.lastAlarmValue)) { Main_noUI.lastAlarmValue = 0; } } } else if (strTmpRequest == "A") { strTmpResponse = response.Split(",".ToCharArray())[2]; strTmpResponse = DataStructures.Converter.HexaToString(strTmpResponse, strTmpRequest); if (System.Text.RegularExpressions.Regex.IsMatch(strTmpResponse, @"^[a-zA-Z0-9 ]+$")) { Logger.Log("*** ALARM : " + strTmpResponse + " ****"); } } if (UI_Enabled_Process) { FormRef.SaveResponse(response, IsManualCmd); } else { objNonUIProcessRef.SaveResponse(response, IsManualCmd); } } catch (Exception aex) { Logger.Log("SaveResponse Error:" + aex.Message); } }
private void SaveGPSResponse(string response) { try { if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { FormRef.SaveGPSResponse(response); } else { objNonUIRef.SaveGPSResponse(response); } } catch (Exception aex) { UpdateLogWindow("SaveGPSResponse Error:" + aex.Message); } }
private void DataReceiver(object sender, SerialDataReceivedEventArgs e) { try { var SentCmd = SentQueue.Dequeue(); _receiving = true; //LR //Logger.Log("DataReceiver:"); GotResponseForPrevCmd = true; if (!SentCmd.Item3) { if (UI_Enabled_Process) { Main.cmdCounter++; } else { Main_noUI.cmdCounter++; } } string response = string.Empty; //LR Logger.Log("CommunicationMode:" + SentCmd.Item2 + "\n"); //if(SentCmd.Item4 == "A" || SentCmd.Item4 == "RUNNINGHR") // for (int j = 0; j <= 1000000; j++) { /* give a small delay */ } //else // for (int j = 0; j <= 300000; j++) { /* give a small delay */ } switch (SentCmd.Item2) { case CommunicationMode.TEXT: response = serialPort.ReadExisting(); CmdReceivedTime = DateTime.Now; break; case CommunicationMode.HEXA: { _receivedcmd = SentCmd.Item4.Split(',')[1]; if (_receivedcmd == "RUNNINGHR") { _minHexaLength = 9; _WaitTime = 200; } else if (_receivedcmd == "A") { _minHexaLength = 59; _WaitTime = 200; } else { _minHexaLength = 7; _WaitTime = 50; } int bytesToRead = serialPort.BytesToRead; RunHrWait.Restart(); while ((response.Trim().Split(" ".ToCharArray()).Length < _minHexaLength || bytesToRead > 0) && RunHrWait.ElapsedMilliseconds < _WaitTime) { byte[] buffer2 = new byte[bytesToRead]; serialPort.Read(buffer2, 0, bytesToRead); CmdReceivedTime = DateTime.Now; response += ByteToHex(buffer2); bytesToRead = serialPort.BytesToRead; } RunHrWait.Stop(); //if (!(_receivedcmd == "RUNNINGHR" || _receivedcmd == "A")) //&& !SentCmd.Item3 //{ // int bytesToRead = serialPort.BytesToRead; // while (bytesToRead > 0 || ) // { // byte[] buffer2 = new byte[bytesToRead]; // serialPort.Read(buffer2, 0, bytesToRead); // CmdReceivedTime = DateTime.Now; // response += ByteToHex(buffer2); // bytesToRead = serialPort.BytesToRead; // } //} //else //{ // // For running hour and A, we will require / receive more hex pairs than usual // // For manual commands also we are not sure about the return values so we wait for more pairs -- commented // int bytesToRead = serialPort.BytesToRead; // _minHexaLength = (_receivedcmd == "RUNNINGHR") ? 9 : 59; // || SentCmd.Item3 // RunHrWait.Start(); // while ((response.Trim().Split(" ".ToCharArray()).Length < _minHexaLength || bytesToRead > 0) && RunHrWait.ElapsedMilliseconds < 200) // { // byte[] buffer2 = new byte[bytesToRead]; // serialPort.Read(buffer2, 0, bytesToRead); // CmdReceivedTime = DateTime.Now; // response += ByteToHex(buffer2); // bytesToRead = serialPort.BytesToRead; // } // RunHrWait.Stop(); // Logger.Log("Waited for " + RunHrWait.ElapsedMilliseconds + ". Hex "+ response.Split(" ".ToCharArray()).Length ); // RunHrWait.Reset(); //} break; } } if (!CommandSent) { if (UI_Enabled_Process) { Logger.Log("[ Request: " + SentCmd.Item4 + " ][ Response: " + response + " ] [NOTSAVED][" + Main.cmdCounter.ToString() + "]"); } else { Logger.Log("[ Request: " + SentCmd.Item4 + " ][ Response: " + response + " ] [NOTSAVED][" + Main_noUI.cmdCounter.ToString() + "]"); } return; }// TO avoid saving response if a command is considered to be waited enough with no response if (UI_Enabled_Process) { Logger.Log("[ Request: " + SentCmd.Item4 + " ][ Response: " + response + " ][" + Main.cmdCounter.ToString() + "][" + SentQueue.Count + "][" + RunHrWait.ElapsedMilliseconds + "]"); } else { Logger.Log("[ Request: " + SentCmd.Item4 + " ][ Response: " + response + " ][" + Main_noUI.cmdCounter.ToString() + "][" + SentQueue.Count + "][" + RunHrWait.ElapsedMilliseconds + "]"); } SaveResponse(SentCmd.Item4 + "," + response, SentCmd.Item3); //if(SentCmd.Item4.IndexOf("ENGINESTATE") > 0 && int.Parse(response) == 1) //{ //} if (SentCmd.Item3) { if (UI_Enabled_Process) { if (!FormRef.SwapRequired) { FormRef.SwapTo = Metadata.ActiveHexaSet; FormRef.SwapRequired = true; } } else { if (!objNonUIProcessRef.SwapRequired) { objNonUIProcessRef.SwapTo = Metadata.ActiveHexaSet; objNonUIProcessRef.SwapRequired = true; } } } if (UI_Enabled_Process) { FormRef.Process(); } else { objNonUIProcessRef.Process(); } //Logger.Log("DataReceiver:Completed"); } catch (Exception ex) { Logger.Log("Receiver Error:" + ex.Message + "\n"); } finally { _receiving = false; } }
private void DataReceiver(object sender, SerialDataReceivedEventArgs e) { try { UpdateLogWindow("GPS DataReceiver"); var SentCmd = SentQueue.Dequeue(); _receiving = true; GotResponseForPrevCmd = true; if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { Main.GPSCmdCounter++; } else { Main_noUI.GPSCmdCounter++; } string response = string.Empty; switch (SentCmd.Item2) { case CommunicationMode.TEXT: response = GPSSerialPort.ReadExisting(); CmdReceivedTime = DateTime.Now; break; case CommunicationMode.HEXA: { _receivedcmd = SentCmd.Item3.Split(',')[1]; int bytesToRead = GPSSerialPort.BytesToRead; RunHrWait.Restart(); while ((response.Trim().Split(" ".ToCharArray()).Length < _minHexaLength || bytesToRead > 0) && RunHrWait.ElapsedMilliseconds < _WaitTime) { byte[] buffer2 = new byte[bytesToRead]; GPSSerialPort.Read(buffer2, 0, bytesToRead); CmdReceivedTime = DateTime.Now; response += ByteToHex(buffer2); bytesToRead = GPSSerialPort.BytesToRead; } RunHrWait.Stop(); break; } } if (!string.IsNullOrEmpty(response)) { response = DataStructures.Converter.GPSHexaToString(response); } if (!CommandSent) { if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { UpdateLogWindow("[ Request: " + SentCmd.Item3 + " ][ Response: " + response + " ] [NOTSAVED][" + Main.GPSCmdCounter.ToString() + "]"); } else { UpdateLogWindow("[ Request: " + SentCmd.Item3 + " ][ Response: " + response + " ] [NOTSAVED][" + Main_noUI.GPSCmdCounter.ToString() + "]"); } return; }// TO avoid saving response if a command is considered to be waited enough with no response if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { UpdateLogWindow("[ Request: " + SentCmd.Item3 + " ][ Response: " + response + " ][" + Main.GPSCmdCounter.ToString() + "][" + SentQueue.Count + "][" + RunHrWait.ElapsedMilliseconds + "]"); if (Main.GPSCmdCounter >= Metadata.InputDictionaryCollection["GPS"].Count) { Main.GPSCmdCounter = 0; UpdateLogWindow("[ Saving : " + "GPS," + response); response = SentCmd.Item3 + "," + (response.Substring(response.IndexOf(":") + 2, response.IndexOf("OK") - response.IndexOf(":") - 6).Replace(",", "~")); SaveGPSResponse(response); UpdateLogWindow("Saved " + response); } else { FormRef.ProcessGPSCommands(); } } else { UpdateLogWindow("[ Request: " + SentCmd.Item3 + " ][ Response: " + response + " ][" + Main_noUI.GPSCmdCounter.ToString() + "][" + SentQueue.Count + "][" + RunHrWait.ElapsedMilliseconds + "]"); if (Main_noUI.GPSCmdCounter >= Metadata.InputDictionaryCollection["GPS"].Count) { Main_noUI.GPSCmdCounter = 0; UpdateLogWindow("[ Saving : " + "GPS," + response); response = SentCmd.Item3 + "," + (response.Substring(response.IndexOf(":") + 2, response.IndexOf("OK") - response.IndexOf(":") - 6).Replace(",", "~")); SaveGPSResponse(response); UpdateLogWindow("Saved " + response); } else { objNonUIRef.ProcessGPSCommands(); } } } catch (Exception ex) { UpdateLogWindow("GPS Receiver Error:" + ex.Message + "\n"); } finally { _receiving = false; } }
private void BwReceiver_DoWork(object sender, DoWorkEventArgs e) { try { Logger.Log("Server Receiver Started!"); while (Client.Connected) { try { if (!ServerStream.DataAvailable) { continue; } //byte[] inStream = new byte[Client.ReceiveBufferSize]; //int recb = ServerStream.Read(inStream, 0, Client.ReceiveBufferSize); //if (recb == 0) return; //string returndata = System.Text.Encoding.ASCII.GetString(inStream); //if (returndata.Length == 0) //{ // continue; //} //cmd = JsonConvert.DeserializeObject<DataPacket>(returndata); List <DataPacket> cmds = ReceiveMessage(); foreach (DataPacket cmd in cmds) { if (cmd.SenderID != string.Empty) { //to write a logic to find hex from multiple dic if (Metadata.InputDictionaryCollection[Metadata.ActiveHexaSet].Where(_ => _.Name.Equals(cmd.Message)).Count() == 0) { // send invalid cmd response to server if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { FormRef.SendManualCommand(cmd.Message); } else { objNonUIRef.SendManualCommand(cmd.Message); } } else { if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { FormRef.SendManualCommand(Metadata.InputDictionaryCollection[Metadata.ActiveHexaSet].FirstOrDefault(x => x.Name == cmd.Message).Hexa); } else { objNonUIRef.SendManualCommand(Metadata.InputDictionaryCollection[Metadata.ActiveHexaSet].FirstOrDefault(x => x.Name == cmd.Message).Hexa); } } } } } catch (Exception ex) { Logger.Log("Error while receiving. " + ex.Message); } } if (!Client.Connected) { if (ConfigurationManager.AppSettings["UI_ENABLED"] != null && ConfigurationManager.AppSettings["UI_ENABLED"].ToString().ToLower() == "true") { FormRef.bInitAll = false; } else { objNonUIRef.bInitAll = false; } if (!InitInProgress) { IsServerConnected = InitiateConnection(); } } Logger.Log("Server connection lost!"); } catch (Exception ex) { Logger.Log("Receiver Error" + ex.Message, ex); } finally { Logger.Log("Server Receiver Closed!"); } }