Background
To capture data from a logic controller, I'm using screen as a terminal emulator and connecting my MacBook via the KeySpan USA-19HS USB Serial Adapter. I've created the following bash script, so that I can type talk2controller <filename>
where filename is the name of the data file.
#!/bin/bash
if [ -z "$1" ]; then
echo Please provide the filename to save the logfile
exit
fi
LOGFILE=$1
echo "logfile $1" > screenrc # Set the logfile filename
echo "logfile flush 1" >> screenrc # Wait 1 sec before flushing buffer to filesystem
screen -L -c screenrc /dev/tty.KeySerial1 19200
I've changed the filename for the logfile and changed from the default of 10 seconds to 1 second for waiting before flushing the logfile buffer to the filesystem. I save those commands to screenrc
. Then I call screen with:
-L
— logging enabled-c screenrc
— override the default configuration file/dev/tty.KeySerial1 19200
— talk to the serial port using a baud rate of 19200
Each test that I log takes about 3–6 minutes and contains speed, acceleration, and position information. I'll know that the test was valid based on the acceleration rate. Currently, I'm waiting until after the test to then run a Python matplotlib script to plot the speed, acceleration, and position to see if the test was valid before moving on to the next test.
To save time, I would prefer to plot the data about halfway through the test, while data is still being captured.
Questions
In my mind there are two options to plotting the data while more data is still being captured:
- Option 1: Use screen to log the data and have the Python matplotlib script read the partial logfile.
- Question 1: What concerns are there if the Python script reads the logfile, while screen is still writing data to it?
- Option 2: Switch from using screen to using pySerial. However, plotting the data during the test is a lower priority than simply capturing the data during the test. I can't afford for an exception in the plotting portion of the code to cause the data logging to fail. That's what's great about screen—it just dumps the data and doesn't try to do anything else.
- Question 2: If I were to switch to pySerial, could I run two threads to reduce the chance that the plotting portion of the code doesn't impact the data capture code? Does this buy me anything?
Question 3: Is there a better option that I haven't thought of?