Trouble writing lines into file with line feeds- Python Newb

D

daniel.t.healy

Overview: I'm attempting to read strings from a serial port. Each string ends with a carriage return and line feed. I want to write those strings to afile, like a log file. So, if I send P1 and the P2 on a new line, I would expect to open this file and find (line 1) P1 (line 2) P2.

Problem: The file only contains P2. It always overwrites the first line. I can send 20 strings and the file will always contain the last string received.

Code:

#Import the serial module
import serial

#Open the serial port w/ settings
ser=serial.Serial(
port="/dev/ttyUSB0",
baudrate=9600,
timeout=None)

#Establish a placeholder for the variable line
line=[]

#Print data received on the serial port after removing the CR and LF characters
while True:
rawcode=ser.readline()
codelog=open('/home/pi/avdms/codes.log','w')
codelog.write(rawcode)
codelog.close()
 
J

Jussi Piitulainen

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top