Reading From Serial Port

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean

    #1

    Reading From Serial Port

    I'm trying to read data from the Serial Port but I'm running in to a
    strange problem. The data parses correctly in a Console Application,
    but when I try to read the same way in a Windows Form Application it
    drops data randomly.

    In the windows form application I read data using the following code:
    public Form1()
    {
    InitializeCompo nent();
    serialPort1.Ope n();
    }

    private void Form1_FormClosi ng(object sender,
    FormClosingEven tArgs e)
    {
    if (serialPort1.Is Open) serialPort1.Clo se();
    }

    private void serialPort1_Dat aReceived(objec t sender,
    SerialDataRecei vedEventArgs e)
    {
    this.Invoke(new EventHandler(Di splayText));
    }

    private void DisplayText(obj ect sender, EventArgs e)
    {
    textBox1.Append Text(serialPort 1.ReadExisting( ));
    }

    The output in the textbox is:
    :E= PERSONAL MESSAGEMF
    =$To:Sean Sims=:sean.si ms@gmail.com= Sep 24 2008
    11:43=Subject :Test
    = text>

    In the console application the same text appears as:
    :E= PERSONAL MESSAGEMF
    =$To:Sean Sims
    =$From:sean.s ims@gmail.com
    =_Time:Sep 24 2008 11:43
    =*Subject:Tes t
    =#<Empty text>

    It should appear like this:
    PERSONAL MESSAGE
    To:Sean Sims
    From:sean.sims@ gmail.com
    Time:Sep 24 2008 11:43
    Subject:Test
    <Empty text>

    First, what can I do to troubleshoot and/or prevent the lost data in
    the middle of the message? Second, what can I do to get rid of the
    strange characters that appear before each line? Thanks in advance for
    any help you can offer.
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: Reading From Serial Port

    Data is not dropping. The only difference is the returns. If you ensure you
    have both a CR and an LF, you should be fine here. You also have a couple of
    characters that do not want to display, if that is what you mean by lost
    data.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Sean" <sean.sims@gmai l.comwrote in message
    news:c7b24835-f6dc-4cf0-9fe4-d5f129d61fe7@l6 4g2000hse.googl egroups.com...
    I'm trying to read data from the Serial Port but I'm running in to a
    strange problem. The data parses correctly in a Console Application,
    but when I try to read the same way in a Windows Form Application it
    drops data randomly.

    In the windows form application I read data using the following code:
    public Form1()
    {
    InitializeCompo nent();
    serialPort1.Ope n();
    }

    private void Form1_FormClosi ng(object sender,
    FormClosingEven tArgs e)
    {
    if (serialPort1.Is Open) serialPort1.Clo se();
    }

    private void serialPort1_Dat aReceived(objec t sender,
    SerialDataRecei vedEventArgs e)
    {
    this.Invoke(new EventHandler(Di splayText));
    }

    private void DisplayText(obj ect sender, EventArgs e)
    {
    textBox1.Append Text(serialPort 1.ReadExisting( ));
    }

    The output in the textbox is:
    :E= PERSONAL MESSAGEMF
    =$To:Sean Sims=:sean.si ms@gmail.com= Sep 24 2008
    11:43=Subject :Test
    = text>

    In the console application the same text appears as:
    :E= PERSONAL MESSAGEMF
    =$To:Sean Sims
    =$From:sean.s ims@gmail.com
    =_Time:Sep 24 2008 11:43
    =*Subject:Tes t
    =#<Empty text>

    It should appear like this:
    PERSONAL MESSAGE
    To:Sean Sims
    From:sean.sims@ gmail.com
    Time:Sep 24 2008 11:43
    Subject:Test
    <Empty text>

    First, what can I do to troubleshoot and/or prevent the lost data in
    the middle of the message? Second, what can I do to get rid of the
    strange characters that appear before each line? Thanks in advance for
    any help you can offer.

    Comment

    • Sean

      #3
      Re: Reading From Serial Port

      I don't have any control over the output. I can only parse it. Why
      is the text displayed correctly in the console application, but when I
      shove it into a textbox the same data does not display properly?
      Also, is there any way to filter special characters (I'm getting a lot
      of symbols and other strange characters that won't display here)?

      -Sean

      On Sep 24, 10:14 pm, "Cowboy \(Gregory A. Beamer\)"
      <NoSpamMgbwo... @comcast.netNoS pamMwrote:
      Data is not dropping. The only difference is the returns. If you ensure you
      have both a CR and an LF, you should be fine here. You also have a coupleof
      characters that do not want to display, if that is what you mean by lost
      data.
      >
      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA
      >
      Subscribe to my bloghttp://feeds.feedburne r.com/GregoryBeamer#
      >
      or just read it:http://feeds.feedburner.com/GregoryBeamer
      >
      *************** *************** **************
      | Think outside the box!                               |
      *************** *************** **************" Sean" <sean.s...@gmai l.comwrote in message
      >
      news:c7b24835-f6dc-4cf0-9fe4-d5f129d61fe7@l6 4g2000hse.googl egroups.com...
      I'm trying to read data from the Serial Port but I'm running in to a
      strange problem.  The data parses correctly in a Console Application,
      but when I try to read the same way in a Windows Form Application it
      drops data randomly.
      >
      In the windows form application I read data using the following code:
              public Form1()
              {
                  InitializeCompo nent();
                  serialPort1.Ope n();
              }
      >
              private void Form1_FormClosi ng(object sender,
      FormClosingEven tArgs e)
              {
                  if (serialPort1.Is Open) serialPort1.Clo se();
              }
      >
              private void serialPort1_Dat aReceived(objec t sender,
      SerialDataRecei vedEventArgs e)
              {
                  this.Invoke(new EventHandler(Di splayText));
              }
      >
              private void DisplayText(obj ect sender, EventArgs e)
              {
                  textBox1.Append Text(serialPort 1.ReadExisting( ));
              }
      >
      The output in the textbox is:
       : E  =    PERSONAL MESSAGE M F
        =$To:Sean Sims  =:sean.s...@gma il.com  =Sep 24 2008
      11:43  =Subject:Test
        = text>
      >
      In the console application the same text appears as:
       : E  =    PERSONAL MESSAGE M F
        =$To:Sean Sims
        =$From:sean.s.. .@gmail.com
        =_Time:Sep 24 2008 11:43
        =*Subject:Test
        =#<Empty text>
      >
      It should appear like this:
      PERSONAL MESSAGE
      To:Sean Sims
      From:sean.s...@ gmail.com
      Time:Sep 24 2008 11:43
      Subject:Test
      <Empty text>
      >
      First, what can I do to troubleshoot and/or prevent the lost data in
      the middle of the message? Second, what can I do to get rid of the
      strange characters that appear before each line? Thanks in advance for
      any help you can offer.

      Comment

      • Pete Kane

        #4
        Re: Reading From Serial Port

        Sean wrote:
        I'm trying to read data from the Serial Port but I'm running in to a
        strange problem. The data parses correctly in a Console Application,
        but when I try to read the same way in a Windows Form Application it
        drops data randomly.
        >
        In the windows form application I read data using the following code:
        public Form1()
        {
        InitializeCompo nent();
        serialPort1.Ope n();
        }
        >
        private void Form1_FormClosi ng(object sender,
        FormClosingEven tArgs e)
        {
        if (serialPort1.Is Open) serialPort1.Clo se();
        }
        >
        private void serialPort1_Dat aReceived(objec t sender,
        SerialDataRecei vedEventArgs e)
        {
        this.Invoke(new EventHandler(Di splayText));
        }
        >
        private void DisplayText(obj ect sender, EventArgs e)
        {
        textBox1.Append Text(serialPort 1.ReadExisting( ));
        }
        >
        The output in the textbox is:
        :E= PERSONAL MESSAGEMF
        =$To:Sean Sims=:sean.si ms@gmail.com= Sep 24 2008
        11:43=Subject :Test
        = text>
        >
        In the console application the same text appears as:
        :E= PERSONAL MESSAGEMF
        =$To:Sean Sims
        =$From:sean.s ims@gmail.com
        =_Time:Sep 24 2008 11:43
        =*Subject:Tes t
        =#<Empty text>
        >
        It should appear like this:
        PERSONAL MESSAGE
        To:Sean Sims
        From:sean.sims@ gmail.com
        Time:Sep 24 2008 11:43
        Subject:Test
        <Empty text>
        >
        First, what can I do to troubleshoot and/or prevent the lost data in
        the middle of the message? Second, what can I do to get rid of the
        strange characters that appear before each line? Thanks in advance for
        any help you can offer.
        Hi there, have you set the AcceptsReturn property of the textbox to true
        ? ( worth a try )

        Comment

        Working...