Yuxiang Wang
Yuxiang Wang

Reputation: 8423

Python non-block reading from a serial port: continuously dump the data to a file

I am sending continuous stream of data from Arduino to my serial port at a high speed. I would like to dump those data to my hard drive continuously.

At low speed, a simple and inefficient code would do:

import serial
ser = serial.Serial('COM4', baudrate=9600)
f = open('data.dat', 'wb')
for i in range(10000):
    data = ser.read()
    f.write(data)
    f.flush()
ser.close()
f.close()

At higher speed, we can change data = ser.read() to data = ser.read(10000) so it would buffer more data in each function call, and therefore more efficient.

However, I am thinking: should there be a better way? Conceptually, I imagine that there is a way to buffer 10000 bytes of data, and in another thread/process to start saving this data to hard drive, and then go back to the main thread/process to keep receiving data.

Would that be reasonable/possible? To be more specific, the questions are:

1) Should I use multiple threads or processes?

2) Where should the data be stored and how should it be passed between threads/processes?

Upvotes: 0

Views: 1313

Answers (1)

David Schwartz
David Schwartz

Reputation: 182763

There's no need. Disk writes are already dispatched. It has to be this way because disk devices don't have a way to write one byte to disk.

Upvotes: 2

Related Questions