Giter Club home page Giter Club logo

Comments (6)

Neutree avatar Neutree commented on May 22, 2024

在C++中可能会设置串口中断回调函数,这样串口数据到达后就可以立马获得数据;

但是python中我使用了一个比较简单粗暴的方法,就是开了一个线程死循环读取串口,这样做的好处就是简单可靠,但是缺点就是因为是死循环,性能会有问题,CPU占用会过高,所以在这里延时了一下下释放一下CPU占用,至于为啥是0.009,是我看着CPU使用率以及不停地给串口发数据综合的情况下取的一个值2333333

如果你有什么更好的解决办法就更好了

from comtool.

Neutree avatar Neutree commented on May 22, 2024

其实也许可以用read函数阻塞等待更好
http://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.read

from comtool.

jackgreentemp avatar jackgreentemp commented on May 22, 2024

感谢回复,pyserial这个没有中断回调好坑...

使用read阻塞等待应该能减少while循环次数,但是这儿有一个疑问:如果不用线程,而是用QTimer做一个定时器,比如每隔100ms去读一下串口的数据,然后数据处理逻辑不变,这种使用定时器的方式和线程+while死循环比较,哪种方式更合理一点?

貌似官方示例都是线程+while死循环的方式,感觉使用定时器的方式在某些应用场景是可以的,比如某些要实时显示传感器数据的应用,这种应用数据发送频率应该是固定的

最近遇到的问题就是这种应用里遇到的,应用大概是这么样:硬件那边通过串口不停地发送数据,上位机这边要接收数据并实时显示曲线,现在上位机程序跑一会儿就死了...任务管理器中是程序无响应...控制台还不报任何错

from comtool.

Neutree avatar Neutree commented on May 22, 2024

是的,pyserial没有事件,都是死循环读取。

两种方式都可以的,看你具体的要求了,反正串口助手为了实时性死循环更好,你的程序没有响应可能引起的原因很多了,多半是你的UI显示的问题,这个得你自己多调试调试找问题了

from comtool.

jackgreentemp avatar jackgreentemp commented on May 22, 2024

嗯,非常感谢
特别诡异的是,接收数据线程不加time.sleep,程序就能多跑一会儿...

from comtool.

Neutree avatar Neutree commented on May 22, 2024

加sleep界面会死掉多半就是你的sleep是在UI线程调用的了~~

from comtool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.