Hi all,
Not really sure where to post this, so general seems appropriate.
is there a tips/tricks section for 'maybe useful ideas' ?
This post:
https://forum.arduino.cc/t/copy-paste-serial-monitor-not-possible-options/1058964
Has lead me to explore the options of getting Arduino serial output into Python.
Often I find myself outputting to the serial monitor, copy-pasting data into excel, formatting, doing calculations...
But wouldn't it be nice to read the data into Python and process it?
I certainly think so
This is by no means a complete guide but it should get you started if you want to explore.
Requirements:
-- Python and a bit of Python skills
-- Installation of pyserial: https://pypi.org/project/pyserial/
The very basic code.
On the Arduino side:
#include <Streaming.h>
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial << "Current millis: " << millis() << endl;
delay(1000);
}
Python code:
import serial
def readserial(comport, baudrate):
ser = serial.Serial(comport, baudrate, timeout=0.1) # 1/timeout is the frequency at which the port is read
while True:
data = ser.readline().decode().strip()
if data:
print(data)
if __name__ == '__main__':
readserial('COM28', 115200)
Python code with timestamp:
import serial
import time
def readserial(comport, baudrate, timestamp=False):
ser = serial.Serial(comport, baudrate, timeout=0.1) # 1/timeout is the frequency at which the port is read
while True:
data = ser.readline().decode().strip()
if data and timestamp:
timestamp = time.strftime('%H:%M:%S')
print(f'{timestamp} > {data}')
elif data:
print(data)
if __name__ == '__main__':
readserial('COM28', 115200, True) # COM port, Baudrate, Show timestamp