4 Replies - 11355 Views - Last Post: 19 December 2011 - 07:06 PM Rate Topic: -----

#1 fb69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-December 11

Read com ports continuously in c#

Posted 19 December 2011 - 02:52 AM

I'm able to read the data from my COM port and display it on the Listbox. However, only the first data is being read and display on the listbox. And when I check the number of bytes, it displayed 155 instead of 125. How do I read the subsequent data from the COM ports, such that it'll read every 5 seconds from the COM ports.

        private delegate void setTextDeleg(string text);
        public delegate void myDelegate();

        SerialPort serialPort = new SerialPort();
        List<byte> bBuffer = new List<byte>(125);

        
        void datareceived(object sender, SerialDataReceivedEventArgs e)
        {
            myDelegate d = new myDelegate(update);
            listBox1.BeginInvoke(d, new object[] { });                 
                     
        }

        public void update()
        {       

            int numberOfBytesToRead;
               
            // Get the number of bytes available to read.

            numberOfBytesToRead = serialPort.BytesToRead;

            // Create a byte array large enough to hold the bytes to be read.

            Byte[] newReceivedData = new Byte[numberOfBytesToRead];

            // Read the bytes into the byte array.

            serialPort.Read(newReceivedData, 0, numberOfBytesToRead);

            // Add the bytes to the end of the list.

            bBuffer.AddRange(newReceivedData);

            // Call a routine to process the data.
            ProcessBuffer(bBuffer);

            Console.WriteLine("Number of bytes: " + numberOfBytesToRead );
        }

        private void ProcessBuffer(List<byte> bBuffer)
        {

            int numberOfBytesToRead = 125;
            if (bBuffer.Count >= numberOfBytesToRead)
            {
                // Show the user the incoming data // Display mode
                for (int i = 0; i < numberOfBytesToRead; i++)
                {
                    ////SpO2-D: 4-beat SpO2 Average / HR-D: 4-beat Pulse Rate Average
                    listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()) + " Time: ");

                }
}





Please refer to the screenshot for the image.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Read com ports continuously in c#

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Read com ports continuously in c#

Posted 19 December 2011 - 03:28 AM

Maybe take a look at this tutorial:

Serial Port Communication in C#

Some more info:

SerialPort (RS-232 Serial COM Port) in C# .NET (Some blog)
SerialPort Class (MSDN Article)

This post has been edited by RexGrammer: 19 December 2011 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 fb69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-December 11

Re: Read com ports continuously in c#

Posted 19 December 2011 - 06:19 AM

View PostRexGrammer, on 19 December 2011 - 03:28 AM, said:

Maybe take a look at this tutorial:

Serial Port Communication in C#

Some more info:

SerialPort (RS-232 Serial COM Port) in C# .NET (Some blog)
SerialPort Class (MSDN Article)


Hi, appreciate the link given. I've already loo through most the the serial comm tutorial including the ones you've given me before. However, I'm still unable to resolve my issues. I would appreciate if you could highlight the codes that I've provided here. Thanks much
Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Read com ports continuously in c#

Posted 19 December 2011 - 09:12 AM

I actually think this code is ok. These lines should permit any more than 125 bytes:
if (bBuffer.Count >= numberOfBytesToRead)
	         {
	                // Show the user the incoming data // Display mode
	                for (int i = 0; i < numberOfBytesToRead; i++)
	                {
	                    ////SpO2-D: 4-beat SpO2 Average / HR-D: 4-beat Pulse Rate Average
	                    listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()) + " Time: ");
	 
	                }
                 }



And I did understand you didn't I? You want only 125 bytes to be read, but it reads 155?

In the case I misunderstood you and you want all data to be read, you should use:

 private void ProcessBuffer(List<byte> bBuffer)
	        {
	                // Show the user the incoming data // Display mode
	                for (int i = 0; i < numberOfBytesToRead; i++)
	                {
	                    ////SpO2-D: 4-beat SpO2 Average / HR-D: 4-beat Pulse Rate Average
	                    listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()) + " Time: ");
	 
	                }
               }



This way you won't limit the amount of bytes read.

Also I refactored three lines of your code to make it more readable
Spoiler

This post has been edited by RexGrammer: 19 December 2011 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 fb69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-December 11

Re: Read com ports continuously in c#

Posted 19 December 2011 - 07:06 PM

Thanks for your help Rex! It works now! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1