private void btnCalcCRC_Click(object sender, EventArgs e) { String input_str = txtDataforCRC.Text; List <Byte> crc_input_data = ConvertInputString2ByteList(input_str); UInt16 CRC_Value; // Display input data //foreach (byte byte_data in crc_input_data) //{ // rtbSignalData.AppendText(byte_data.ToString("X") + " "); //} //rtbSignalData.AppendText("\n"); // Calculate CRC byte-by-byte CRC_CCITT16.InitCRCValue(); foreach (byte byte_data in crc_input_data) { CRC_Value = CRC_CCITT16.GetNextCRC(byte_data); //rtbSignalData.AppendText(CRC_Value.ToString("X") + " "); } CRC_Value = CRC_CCITT16.GetCRCAfterFinalCRC(); // Calculate CRC all at once CRC_Value = CRC_CCITT16.CalculateCRC4ByteList(crc_input_data); rtbSignalData.AppendText(CRC_Value.ToString("X") + " "); // Base64-encoded before sending String EncodedString = Convert.ToBase64String(crc_input_data.ToArray()); rtbSignalData.AppendText(EncodedString + "\n"); // Prepare to send via UART EncodedString += "\n"; MySerialPort.SendToSerial(Encoding.ASCII.GetBytes(EncodedString)); }
private void Tmr_FetchingUARTInput_Tick(object sender, EventArgs e) { // Regularly polling request message while (MySerialPort.KLineBlockMessageList.Count() > 0) { // Pop 1st KLine Block Message BlockMessage in_message = MySerialPort.KLineBlockMessageList[0]; MySerialPort.KLineBlockMessageList.RemoveAt(0); // Display debug message on RichTextBox String raw_data_in_string = MySerialPort.KLineRawDataInStringList[0]; MySerialPort.KLineRawDataInStringList.RemoveAt(0); DisplayKLineBlockMessage(rtbKLineData, "raw_input: " + raw_data_in_string); DisplayKLineBlockMessage(rtbKLineData, "In - " + in_message.GenerateDebugString()); // Process input Kline message and generate output KLine message KWP_2000_Process kwp_2000_process = new KWP_2000_Process(); BlockMessage out_message = new BlockMessage(); //Use_Random_DTC(kwp_2000_process); // Random Test //Use_Fixed_DTC_from_HQ(kwp_2000_process); // Simulate response from a ECU device Scan_DTC_from_UI(kwp_2000_process); // Scan Checkbox status and add DTC into queue // Generate output block message according to input message and DTC codes kwp_2000_process.ProcessMessage(in_message, ref out_message); // Convert output block message to List<byte> so that it can be sent via UART List <byte> output_data; out_message.GenerateSerialOutput(out output_data); // NOTE: because we will also receive all data sent by us, we need to tell UART to skip all data to be sent by SendToSerial MySerialPort.Add_ECU_Filtering_Data(output_data); MySerialPort.Enable_ECU_Filtering(true); // Send output KLine message via UART (after some delay) Thread.Sleep((KWP_2000_Process.min_delay_before_response - 1)); MySerialPort.SendToSerial(output_data.ToArray()); // Show output KLine message for debug purpose DisplayKLineBlockMessage(rtbKLineData, "Out - " + out_message.GenerateDebugString()); } }