Vb.net串口CPU使用问题 [英] Vb.net serialport CPU usage problem

查看:108
本文介绍了Vb.net串口CPU使用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从连接到COM端口的GPS设备读取数据。但是在打开端口后,CPU使用率始终保持在4-5%甚至更高,即使我根本不处理数据!以下是代码:



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





对于较旧的计算机,CPU使用率为50%,因此这是一个真正的问题。这有什么好办法吗?



在Putty打开端口时,CPU大部分时间保持0%,随机峰值1%!



我尝试了什么:



我搜索了很多而且没有发现任何好的解决方案我知道有办法解决这个问题,但我不够熟练。



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.

推荐答案

你必须使用一个基于事件的接收函数,它在它是自己的线程。只有在有新数据可用时才会运行(使用CPU)。



您可以在网上搜索类似vb.net serialport tutorial的内容。



一些链接:

VB.Net中的串口通信 - VB.NET教程| Dream.In.Code [ ^ ]

Windows 8 C#中的VB.NET(C#)中的SerialPort示例,Visual Studio 2008的VB.NET [ ^ ]
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[^]


问题解决了。真正的问题是GPS设备向我的PC发送了大量不必要的数据。通过使用NMEA命令禁用它,我能够将CPU使用率降低到0-1%。此外,我开始使用DataReceived - 事件,如Jochen Arndt链接的示例中所示。谢谢您的提示!
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!


这篇关于Vb.net串口CPU使用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆