SerialPort // Start character, end character, special end character such as <STX>

Markus Freitag 3,791 Reputation points
2024-02-17T15:26:33.7666667+00:00

Hello,

var port = new SerialPort("COM4");
port.Open();
port.ReadTimeout = 10000; // Set the timeout to 10 seconds

var commandsToSend = new string[] { "AT1", "AT2", "AT3" };

foreach (var command in commandsToSend)
{
	try
	{
		Trace.WriteLine($"Write '{command}' to {port.PortName}");
		port.WriteLine(command);

		Trace.WriteLine("Waiting for response...");

		// This will throw a TimeoutException if no data is received within the timeout
		string response = port.ReadLine();

		Trace.WriteLine($"Got response: {response}")

For understanding no.1

The ReadTimeout only works in combination with ReadLine
Do I understand this correctly?
That would be sufficient for me, as the end criterion is <CR>.
ASCII	13	0D 
If I need the EOT character as an end criterion in the future, would that also work simply?
ASCII	4	04

For understanding no.2

The Readline()
-> Manages this when the answer arrives in pieces. So in two or three packages
How would this have to be created correctly if the end is a different character?

For understanding no.3

<STX>XXXXXXXXX<GS>YYYYYY><GS>AAAAAA<STX>
Now I receive this, however
Package 1
WWWWWW<STX>XXXXXXX
Package 2
XX<GS>YYYYYY><GS>AAAAA
Pacakge 3
A<STX>
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-02-19T10:16:34.21+00:00

    Hi @Markus Freitag

    The ReadTimeout property in combination with ReadLine() allows you to set a timeout for how long the system should wait for data to be received before throwing an exception. If you receive data in multiple packages or if your message format changes in the future (e.g., using different end characters like EOT), you'll need to adapt your code accordingly.

    If your message format changes to include different end characters, such as <EOT>, which is ASCII 4, you would need to modify your code to accommodate this. Instead of relying on ReadLine(), you might read characters one by one until you encounter the <EOT> character or until a timeout occurs.

    string response = "";
    char EOT = (char)4; // ASCII code for EOT character
    
    try
    {
        Trace.WriteLine($"Write '{command}' to {port.PortName}");
        port.WriteLine(command);
    
        Trace.WriteLine("Waiting for response...");
    
        // Read characters until EOT or timeout
        StringBuilder sb = new StringBuilder();
        while (true)
        {
            int nextChar = port.ReadChar();
            if (nextChar == -1) // Timeout occurred
            {
                // Handle timeout error
                throw new TimeoutException("Timeout occurred while waiting for response.");
            }
            char c = (char)nextChar;
            if (c == EOT)
            {
                break; // End of message
            }
            sb.Append(c);
        }
        response = sb.ToString();
    
        Trace.WriteLine($"Got response: {response}");
    }
    catch (TimeoutException ex)
    {
        Trace.WriteLine($"Error: {ex.Message}");
    }
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.