/// <summary> /// Parsing of Data packet /// </summary> /// <param name="receivedPacket"></param> public void ParsingData(Packet.Packet receivedPacket) { int responceId = -1; try { receivedPacket.CurrentPos = 0; receivedPacket.RemoveBytes(EnumAndConstants.CONSTANT_VALUE); //SOF receivedPacket.RemoveByte(); //Protocol version receivedPacket.RemoveByte(); //Unique number responceId = receivedPacket.RemoveByte(); //Request ID : 6 int PacketLength = receivedPacket.RemoveByte(); //Packet length if ((responceId == (int)ADCStatus.START_ADC || responceId == (int)ADCStatus.END_ADC) && isNewRequestSend == true) { return; } if (responceId == (int)ADCStatus.START_ADC || responceId == (int)ADCStatus.END_ADC) { GetTriggerModeRequest(receivedPacket, responceId); return; } isNewRequestSend = false; if (PacketLength > 0) { GetConfigurationDetails(receivedPacket); } receivedPacket.RemoveByte(); //CRC waitHandle.Set(); } catch (Exception ex) { m_logger.Error(ex); } }
/// <summary> /// Send packet on serial port /// </summary> /// <param name="port"></param> /// <param name="packet"></param> /// <returns></returns> public ResponseCode sendRequest(SerialPort port, ref Packet.Packet packet) { int retryCount = 0; int WaitInterval = EnumAndConstants.waitInterDataInterval; try { lock (synObj) { port.DiscardInBuffer(); port.DiscardOutBuffer(); packet.DataLength = packet.CurrentPos; string payloadstr = ""; for (int ctr = 0; ctr < packet.DataLength; ctr++) { payloadstr += " " + packet.DataArray[ctr].ToString("X2"); } m_logger.Debug("Send Payload", payloadstr); waitHandle.Reset(); lock (port) { port.Write(packet.DataArray, 0, packet.CurrentPos); //Write on Port m_logger.Debug(DateTime.Now.ToLongTimeString()); //Thread.Sleep(EnumAndConstants.WAIT_NO_DATA_INTERVAL); } bool receivedResponse = false; while (retryCount < EnumAndConstants.RETRY_COUNT) { if ((waitHandle.WaitOne(WaitInterval, false)) == true) { receivedResponse = true; responsePacket.CurrentPos = 0; responsePacket.RemoveBytes(2); //SOF responsePacket.RemoveByte(); //Protocol version responsePacket.RemoveByte(); // DSN int responseId = responsePacket.RemoveByte(); //Responce code if (responseId != (int)ResponseCode.SUCCESS) { return(ResponseCode.INVALID); } else { return(ResponseCode.SUCCESS); } } retryCount++; } if (receivedResponse == false) { return(ResponseCode.TIMEOUT); } else { return(ResponseCode.FAILURE); } } } catch (Exception ex) { m_logger.Error(ex); return(ResponseCode.FAILURE); } }