-1

I'm trying to read the data weighing scale to my computer using c# serial port.

in putty output like this :

60KG

60KG

60KG

60KG

then i display it in richtextbox using the script below:

private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
        {
            if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
                BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
            else
            {
                while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
                {
                    String tampung = _serialPort.ReadExisting();

                    String tampungy = Regex.Replace(tampung, @"[^\d]", "").Trim();

                    richTextBox2.AppendText(tampungy + System.Environment.NewLine);
                    richTextBox2.ScrollToCaret();
        }
            }
        }

but displays like this

6

0

6

0

6

0

Is there something wrong ?

Egi
  • 513
  • 2
  • 6
  • 16

1 Answers1

0

It seems like you are reading the data as each character arrives. To demonstrate this, you could do this:

var data = new byte[_serialPort.BytesToRead];
_serialPort.Read(data, 0, data.Length);
tampungy = string.Join(" ", data.Select(b => b.ToString("X2"));
richTextBox2.AppendText(tampungy + System.Environment.NewLine);
richTextBox2.ScrollToCaret();

It should print out the hex encoding of each byte read. 0D 0A (CR LF) and 0A (LF) are linebreaks. 30 or 39 are digits (0-9).

You should buffer the input until a newline is read.

private StringBuilder _buffer = new StringBuilder();

private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
    if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
        BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
    else
    {
        while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
        {
            _buffer.Append(_serialPort.ReadExisting());
        }

        // Look for the first linebreak in the buffer
        int index = Enumerable.Range(0, _buffer.Length).FirstOrDefault(i => _buffer[i] == '\n'); // 0 if not found
        while (index > 0) {
            // Extract and remove the first line
            string tampung = _buffer.ToString(0, index);
            _buffer.Remove(0, index + 1);

            String tampungy = Regex.Replace(tampung, @"\D+", "");

            richTextBox2.AppendText(tampungy + System.Environment.NewLine);
            richTextBox2.ScrollToCaret();

            // Look for the next linebreak, if any
            index = Enumerable.Range(0, _buffer.Length).FirstOrDefault(i => _buffer[i] == '\n'); // 0 if not found
        }
    }
}
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138