Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm reading data from GPS device which is connected to COM port. However after opening the port CPU usage stays constantly 4-5% or even more, even when I'm not processing the data at all! Here's the code:

Using comPort As New SerialPort

              comPort.PortName = "COM3"
              comPort.BaudRate = 4800
              comPort.Parity = Parity.None
              comPort.StopBits = StopBits.One
              comPort.DataBits = 8
              comPort.Handshake = Handshake.None
              comPort.Encoding = System.Text.Encoding.Default

              comPort.Open()

              Thread.Sleep(100000000) '<--- CPU usage very high during this


           End Using


With older computers the CPU usage is 50% so this is a real problem. Is there any good way around this?

When opening the port in Putty, the CPU stays 0% most of the time, peaking 1% randomly!!

What I have tried:

I have googled a lot and not found any good solution. I know there is a way to fix this but I'm not skilled enough to do that.
Posted
Updated 18-Aug-17 2:41am
Comments
Patrice T 18-Aug-17 4:51am    
This code is not what eat cpu.
Give details.

You have to use an event based receive function which is executed in it's own thread. That will only run (use the CPU) when new data are available.

You may search the web for something like "vb.net serialport tutorial".

Some links:
Serial Port Communication In VB.Net - VB.NET Tutorials | Dream.In.Code[^]
Windows 8 SerialPort Sample in VB.NET (C#) in C#, VB.NET for Visual Studio 2008[^]
 
Share this answer
 
Comments
Member 4605890 18-Aug-17 7:26am    
Thanks for the reply. I tested those examples and the problem is still there because all the examples use the same SerialPort class and Open - method. I'm looking for some kind of different solution.
Jochen Arndt 18-Aug-17 7:35am    
Do you have removed the Thread.Sleep() call?

These examples show how it is done with VB.Net (ignoring the old but still supported MSComm Control method). The problem is not in the SerialPort class but in your code by using it the wrong way.

When you remove the Sleep call there should be no significant CPU load because you are not receiving anything.
Problem solved. The real problem was the GPS device that was sending lots of unnecessary data to my PC. By disabling them with NMEA commands I was able to cut down the CPU usage down to 0-1%. Also I started using the DataReceived - event as in the examples linked by Jochen Arndt. Thanks for the tip!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900