Serial port blocking of string reading

I am trying to read data from an RS232 scale interface. It sends a continuous stream of ASCII string through the serial port I'm having problems with. I just want to get one row of data that it sends. I suppose I assumed that I would use Readline to get the data, but it just blocks the PC when I start it. I think he is trying to get all the data and will not stop until the data stops? Here is the code I'm using:

private void button1_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = "COM4";
        serialPort1.BaudRate = 9600;
        serialPort1.DataBits = 8;
        serialPort1.Parity = Parity.None;
        serialPort1.StopBits = StopBits.One;

        //opening the serial port
        serialPort1.Open();

        string str = serialPort1.ReadLine();

        MessageBox.Show(str);

        serialPort1.Close();

    }

Can you help me determine how to get only one line of output and close the connection?

+3
source share
2 answers

SerialPort.ReadLine " NewLine", NewLine . ? , (ASCII 0x0A) (ASCII 0x0D), .

NewLine, , , SerialPort.ReadTo. .

, , , SerialPort.Read.

, , , , - HyperTerminal/TeraTerm/ , , , , , .

+2

, ( , , ). , , , . .

0

Source: https://habr.com/ru/post/1766556/


All Articles