PYSERIAL WITH ARDUINO(comms with arduino from pc)
In this post we're gonna learn about "pyserial" python language library package to communicate or log data from arduino to pc (or) we can get rid of the serial port of arduino.Hence we do the same as we do in serial monitor.
the module can be installed using pip
for linux:
"sudo python -m pip install pyserial"
for windows :
"pip install pyserial"
here is a small arduino skecth for reading from terminal or cmd from arduino serial port
code:
import serial as sl
#importing the serial lib as name sl
import time
mon = '/dev/ttyACM0'
#declearing a variable for port name
baud = 9600
#defining baud rate
arduino = serial.Serial(mon, baud, timeout=.1)
while True:
mon = arduino.readline()
print('the read data:,mon')
#printing message for reading data from serial
code:
import serial arduino_port = "/dev/ttyACM0" #serial port of Arduino baud = 9600 #arduino uno runs at 9600 baud fileName="analog-data.csv" #name of the CSV file generated ser = serial.Serial(arduino_port, baud) print("Connected to Arduino port:" + arduino_port) file = open(fileName, "a") print("Created file") #display the data to the terminal getData=str(ser.readline()) data=getData[0:][:-2] print(data) #add the data to the file file = open(fileName, "a") #append the data to the file file.write(data + "\\n") #write data with a newline #close out the file file.close() samples = 10 #how many samples to collect print_labels = False line = 0 #start at 0 because our header is 0 (not real data) while line <= samples: # incoming = ser.read(9999) # if len(incoming) > 0: if print_labels: if line==0: print("Printing Column Headers") else: print("Line " + str(line) + ": writing...") getData=str(ser.readline()) data=getData[0:][:-2] print(data) file = open(fileName, "a") file.write(data + "\\n") #write data with a newline line = line+1 print("Data collection complete!") file.close()
Comments
Post a Comment