Serial port buffer is having old data(data from the previous run)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mayaanu
    New Member
    • May 2010
    • 3

    Serial port buffer is having old data(data from the previous run)

    I have developed a GUI based application in C# for communicating with a 16 bit littleendian microcontroller device .the device has a serial port interface.

    1.my appplication opens a COM port on which the device is connected with proper settings for the device.
    2.It writes a command to the port.
    3.datareceived event of the serial port reads the data in to a byte array.
    3.i have closed the serialport using close() method
    The problem that i m facing is,
    1.Immediately after opening the port (before giving any request to the device)i am getting the data on serialport)this data is the data that i was receiving in the previous run of the application.
    2For any command i am writing to the serial port (to make changes to the reply)There is a delay before getting the modified reply .During this delay i am receiving the old data(without modifications)
    3.After switching off the device i am receiving the data for sometime

    Is this is because of the uncleared buffer?
    I have tried with Basestream.flus h() before writing a command to the port.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    May we see the code you are using to open and communicate with the COM port?

    Comment

    • mayaanu
      New Member
      • May 2010
      • 3

      #3
      Originally posted by tlhintoq
      May we see the code you are using to open and communicate with the COM port?
      the code snippet is as follows
      Code:
      //for opening the port on a button click
      
      if (serialPort3.IsOpen)
           {
               serialPort3.Close();    
           }
           else
           {          
                   serialPort3.Open();
           }
      
      
      private void serialPort3_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           Control.CheckForIllegalCrossThreadCalls = false;    
           int b = serialPort3.Read(bf, 0, 36);
       }
      //for closing the port
      
       if (serialPort3.IsOpen)
           {
               serialPort3.BaseStream.Dispose();
               serialPort3.Close();
           }
               
      
      //button click writing a packet containing my request to the port   
      {
             byte[] packet = Gen.generatePacket(16, header, data, trial);
             serialport3.write(packet,0,packet.length);
      }
      Last edited by tlhintoq; May 26 '10, 08:52 AM. Reason: [CODE] your code here [/CODE] tags added

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Since I have no idea if you are configuring all the properties of a serial port correctly, you might need this:


        You seem to have an event handler for the DataReceived event. But it is only reading in to a single int. It needs to read into a buffer so it can store everything coming in, not just one number.
        I am attempting to create a small application to collect data received from an external sensor attached to COM10. I have successfully created a small C# console object and application that opens the

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          The read call is reading into a buffer bf (and only reading 36 characters at max)
          You need a little better logic there since the datarecieved event is probably being fired after every byte. If you are using a static buffer like that you would need to adjust your starting point index and data you allow to read.
          That said, the serialport object already keeps the data buffered in the stream, so you don't need to read it off as fast as it comes in (unless you are going to overflow the serialport's buffer.)

          Comment

          Working...