0

I have got myself a little lost with this Windows forms application:(

I have a stream of data arriving at a serial port in the format of \W0987654321\L555666444\W3456789900\L9842429009 and so it repeats with different values for \L and \W

I need to split out the \W and \L values and add them to an array which is to expand as needed. and then plot them on a graph in real time.

Where i'm lost is how to read the port to get each complete value once it has arrived and not truncate it. I just can't get my brain around this so any help would be great...

I open the port thus:

port.PortName = "COM9";
port.BaudRate = 38400;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.DtrEnable = false;
port.Handshake = Handshake.None;

port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
port.Open();

textBox1.Clear();
port.DiscardInBuffer();
port.DtrEnable = true;

Currently i have it all writing to a text box:

private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
    InputData = port.ReadExisting();

    if (InputData != String.Empty)
    {
        this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
    }
}

private void SetText(string text)
{
    this.textBox1.AppendText(text);
}

which shows all the data OK

7
  • You probably dont want port.DiscardInBuffer() then. Commented Jan 23, 2015 at 11:56
  • Are the L and W value lengths consistent? If they are, just split on length, if not, you will only know when a complete value has been received when you see the start of the next one - meaning that you can never be fully 'up-to-date'. As long as it's OK for your application to be potentially one value behind, just check and delimit whenever you encounter a '/' Commented Jan 23, 2015 at 11:58
  • @Leppie I added that the first few characters can be corrupt when i open the port, it hasn't helped. Commented Jan 23, 2015 at 12:03
  • @Paul no the length can change, how do i know when the buffer contains \W1234567890\ so i can read only the data without losing the next part? Commented Jan 23, 2015 at 12:04
  • Do not invoke anything in DataReceived straight away, instead combine input into something what you can parse (parse means find text between '\', remove first 'L'/'W' and do Int64.Parse), parse it and only invoke results (event for receiving either or both of values?). Commented Jan 23, 2015 at 12:22

1 Answer 1

1
var readQueue = string.Empty;

private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
    readQueue += port.ReadExisting();

    while (readQueue.Substring(1).Contains(@"\"))
    {
        var slashPos = readQueue.IndexOf(@"\",1);

        var completeEntry = readQueue.Substring(0, slashPos);

        Console.WriteLine(completeEntry);

        readQueue = readQueue.Substring(slashPos);
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.