开发者

pyserial/python and real time data acquisition

I have an infrared camera/tracker with which I am communicating via the serial port. I'm using the pyserial module to do this at the moment. The camera updates the position开发者_StackOverflow中文版 of a tracked object at the rate of 60 Hz. In order to get the position of the tracked object I execute one pyserial.write() and then listen for an incoming reply with pyserial.read(serialObj.inWaiting()). Once the reply/position has been received the while loop is reentered and so on. My question has to do with the reliability and speed of this approach. I need the position to be gotten by the computer at the rate of at least 60Hz (and the position will then be sent via UDP to a real-time OS). Is this something that Pyserial/Python are capable of or should I look into alternative C-based approaches?

Thanks, Luke


This is more a matter of latency than speed.

Python always performs memory allocation and release, but if the data is reused, the same memory will be reused by the C library. So the OS (C library / UDP/IP stack) will have more impact than Python itself.

I really think you should use a serial port on your RTOS machine and use C code and pre-allocated buffers.


Python should keep up fine, but the best thing to do is make sure you monitor how many reads per second you are getting. Count how many times the read completed each second, and if this number is too low, write to a performance log or similar. You should also consider decoupling the I/O part from the rest of your python program (if there is one) as pyserial read calls are blocking.


I would suspect that Python will keep up with the data just fine. My advice would be to try it, and if Python appears to lag, then try PyPy instead — an implementation of Python that compiles most of your inner loops down to machine code for speed close so that of C.

http://pypy.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜