Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the Python code in the first part of the posting below so that it outputs

ID: 3890648 • Letter: M

Question

Modify the Python code in the first part of the posting below so that it outputs over 100 cycles:

https://stackoverflow.com/questions/19865137/how-to-exp ort-pyserial-reading-data-into-text-file-or-matlab

This is the code I am looking to modify:

import serial ser = serial.Serial('/dev/ttyACM0',115200)
f = open('dataFile.txt','a')

while 1 :
   f.write(ser.readline())
     f.close()
     f = open('dataFile.txt','a')

Here is what I tried, but it gives me Syntax Error: EOS while scanning string literal

import serial

#this connects python with arduino board, Blink, at a baud rate of 115200
ser = serial.Serial('/dev/cu.usbmodem1421,115200)

#This opens the Blink file
f = open('Blink.txt') #'a' was removed from orginal post

x = 0
while x <= 100:
f.write(ser.readline())
f.close()
f = open('Blink.txt')
if x > 100:
break

Explanation / Answer

Notes:

You have a syntax error in your code, try below code and it'll work like.

Code:

import serial

ser = serial.Serial('/dev/ttyACM0',115200)

# Open file with append to keep appending.
# set buffer size to zero,
# this will write the data straight to the disk, you no longer need to open and
# close.
with open('Blink.txt', 'a', 0) as f:
for x in xrange(100):
f.write(ser.readline())
f.flush()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote