It is necessary to read data from the COM port. It is usually recommended to use the ReadLine () method. However, on my device, this method often returns an empty string. I tried to use ReadExisting () in combination with Thread.Sleep () almost 100% positive result.

Thread.Sleep(100); var response = comPort.ReadExisting(); 

That's the question: where's the catch? Why everywhere and in the Internet, and in the literature, and MSDN including, recommend using ReadLine ()?

    1 answer 1

    ReadExisting gets all its buffer data. ReadLine expects a line ending value, then returns a string. If data was received, but there was no line end value, ReadLine will not return anything. Therefore, if you are working with text, use ReadLine. If you are working with non-textual information, use ReadExisting.

    • Not quite clear. What does working with textual information mean? I have a device for dispensing liquids. When I work, I send the appropriate commands via COM-Port and I need to get the result. This, for example, can be the status of a device or the weight of a container with liquid, etc. Of course, I receive this data as a text string from the device. - S_Schmal
    • You get the lines, they are somehow separated. For example, through \n . Then do so serialPort.NewLine="\n"; and everything should work through ReadLine (). The second option is to read through ReadExisting () and break into lines itself. If you are working in one question mode - one answer, and the answer does not end with a newline character, then, I think, ReadExisting () is an advantage. - RusArt
    • Now clearer. Usually all my commands end with "\ n \ r \ n", but I usually get answers in the form of a single line. In general, the direction is clear, I will try. Thanks for the help! - S_Schmal