Hi @Goharshenasanesfahani, Sahar , Welcome to Microsoft Q&A,
You can use a dictionary to store the contents of each section, which can avoid data clutter and present the information of each section more clearly.
- When receiving data, separate it by section header:
- Use a dictionary to store the data of each section.
- When receiving a new data row, check whether it is a section header, and if so, start a new section record.
- Extract key-value pairs and store in a dictionary:
- Extract and store the key-value pairs of each section separately by section header
private Dictionary<string, StringBuilder> sectionsData = new Dictionary<string, StringBuilder>();
private StringBuilder currentSectionData = null;
private string currentSectionTitle = string.Empty;
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string receivedData = SerPort.ReadLine();
UpdateReceivedData(receivedData);
// Check if the received line is a section title
if (receivedData.StartsWith("General Device Configuration"))
{
currentSectionTitle = "General Device Configuration";
currentSectionData = new StringBuilder();
sectionsData[currentSectionTitle] = currentSectionData;
}
// If currently in a section, append the line to the section data
if (currentSectionData != null)
{
currentSectionData.AppendLine(receivedData);
// Check for end of section marker
if (receivedData.Contains("****"))
{
currentSectionData = null;
}
}
}
catch (TimeoutException)
{
MessageBox.Show("Timeout occurred while reading from the serial port.");
}
catch (Exception ex)
{
MessageBox.Show($"Error occurred while reading from the serial port: {ex.Message}");
}
}
private void ExtractKeyValuePairs(string sectionTitle, string sectionData)
{
string[] lines = sectionData.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
string[] parts = line.Split(':');
if (parts.Length == 2)
{
string key = parts[0].Trim();
string value = parts[1].Trim();
// Process the key-value pair as needed, here is an example:
if (sectionTitle == "General Device Configuration")
{
if (key == "DeviceID")
{
deviceId = value;
}
else if (key == "Serial Number")
{
serialNo = value;
}
}
}
}
}
private void UpdateReceivedData(string receivedData)
{
// Assuming this method updates the main form's text box with the received data
// For example:
textBoxReceivedData.AppendText(receivedData + Environment.NewLine);
// After updating, check if there are any completed sections to process
foreach (var section in sectionsData)
{
if (section.Value.ToString().Contains("****"))
{
ExtractKeyValuePairs(section.Key, section.Value.ToString());
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.