3 Replies - 3460 Views - Last Post: 17 February 2011 - 07:02 AM Rate Topic: -----

#1 lcruz007  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-July 10

Reading from serial and sending info to a text file?

Posted 16 February 2011 - 08:08 PM

Can you check my python code?? It is not working for me, info is not recorded to the text file.
import serial
serial = serial.Serial("/dev/ttyUSB0", 115200, timeout=1)
f = open("eog.txt", "w")
while True:
 data = serial.read()
 if data == "1": f.write("1")
 f.close()

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from serial and sending info to a text file?

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Reading from serial and sending info to a text file?

Posted 16 February 2011 - 08:15 PM

What data are you getting back? That is, if you print(data) after line 5, what does it show?

Are you getting any error messages? Post them in full here if you are.

On line 7, you are closing the file; this means you will not be able to write to it again until you re-open it. You probably don't want to do this inside your loop.
Was This Post Helpful? 0
  • +
  • -

#3 lcruz007  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-July 10

Re: Reading from serial and sending info to a text file?

Posted 16 February 2011 - 08:52 PM

Well I've tried voiding that "f.close()" line but it won't work still.... The information read from the serial port is either a "1", a "2", or a "0"... It comes from a microcontroller. I don't get any errors, it just won't record on the text file, it will remain in blank.
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Reading from serial and sending info to a text file?

Posted 17 February 2011 - 07:02 AM

Are you sure that the microcontroller is sending the ASCII value "1" (0x31) and not the integer value 1 (0x01)?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1