I also run the program on a Windows laptop which is more powerful, the cpu usage is about 12 and the reading speed on RPi is slower than the speed on my laptop.I used cProfile to take a look at the method call time and found this: 1 0.000 0.000 2.000 2.000 method readall of io.RawIOBase objects.
![]() Of course the data after n needs to be saved as it may be part of another line. ![]() Using ser.read(1) in combination with ser.read(ser.inwaiting) would give me a throughput of about 660kBsec. If a timeout occurs, self.s.read(i) returns an empty string and you might want to throw an exception to indicate the timeout. Do you have a complete working example of the class that you could shaare. Python Serial Port Event Driven How To Read LinesFor other people struggling with how to read lines faster in python, please use this ReadLine class that skoehler provided. You can check ser.inwaiting attribute and you will observe that its always 0 with this approach. But if you use built-in ser.readline() you will see that ser.inwaiting keeps increasing which means that the input buffer keeps becoming bigger because the readline() was not able to read it fast enough. Some users find that if we use readline it will cause significant delay, as this issue. Therefore, we change the readline with the method proposed in that issue. Also, we add a new function: getvelocity, that will get velocity from arduino. Python Serial Port Event Driven Serial Port InHave you ever worked with TCP connections In many ways, TCP connection are much like a serial port in the sense that you dont know how many bytes will make up a line. So any TCP API will behave like this: you call read(bufsize) and the call will return if at least 1 byte is available and it will return at most bufsize bytes. If read(bufsize) would wait until exactly bufsize bytes are read, that would be catastrophic You could never read a line efficiently, because you would have to use bufsize1. So having read(bufsize) return as soon as at least 1 byte is available allows you to read from a TCP stream until you have received at least one line. This is also how pythons API for TCP streams behaves as well, by the way. Why doesnt pyserial implement TCP like read semantics That is the fking state of the art since the 1980s Based on proper read semantics, one could implement a proper readline function that isnt broken in such an obvious way. Python Serial Port Event Driven Manual Fix ForThe authors repeatedly use serial.read(self.serial.inwaiting or 1) which is a manual fix for the broken read semantics. So the pyserial authors know what proper read semantics should look like. It reads the stream byte by byte That is inefficient as hell. A serial port could be a virtual one (USB) which can deliver megabytes per second Reading byte by byte will increase the overhead to a level that the CPU will become a bottleneck.
0 Comments
Leave a Reply. |