13walkec
Posts: 5
Joined: Wed Aug 10, 2016 4:04 am
Location: Central Wisconsin, USA

Timestamp not updating on serial read

Wed Aug 10, 2016 4:18 am

Hello, So I'm pretty new to raspberry pi and python but anyway: I have my Pi reading data from the serial port on my Arduino I have the Pi printing the serial data to the shell/monitor and I have it so every time it prints it puts in the time as a time stamp (well it's supposed to). It is currently printing the time but each time a new input comes in from the Arduino the time do not update. Below is the code on my Pi...

Code: Select all

import time
from datetime import datetime
import serial
now = datetime.now()
ser = serial.Serial('/dev/ttyACM0',9600)
while True:
     print (ser.readline())
     print ("%s:%s:%s" % (now.hour, now.minute, now.second))
     time.sleep(2)
I run the code and it prints like this....

b'Hello Mr.Pi\r\n'
22:58:53
b'testing 2nd line\r\n'
22:58:53
b'Hello Mr.Pi\r\n'
22:58:53
b'testing 2nd line\r\n'
22:58:53

("The Hello Mr.Pi" and "testing 2nd line" is being printed from the Arduino this is just for testing purposes later on they will be changed)

As you can see the time stamp doesn't update. Also it is annoying that there is all of that extra stuff (b'.....\r\n') but that is a different problem I have.

If I stop the program ctrl + c and then start the program again the time does update. Anyone have any ideas?

Additional Info:
Raspberry Pi 2
Python 3.4.2 language I do have access to Python 2
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"You know you're in the wrong class when more than 3 students are female, and they're all hot."

User avatar
davidcoton
Posts: 5028
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Timestamp not updating on serial read

Wed Aug 10, 2016 7:32 am

Move "now=datetime.now()" into the loop (after "while True:")
Signature retired

13walkec
Posts: 5
Joined: Wed Aug 10, 2016 4:04 am
Location: Central Wisconsin, USA

Re: Timestamp not updating on serial read

Thu Aug 11, 2016 12:23 pm

I did actually find a fix to this problem as well. I found that if I didn't set datetime.now() as a variable and type it out every time it would update every time. I will give your suggestion a try though so I don't have to keep typing it out! Thanks
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"You know you're in the wrong class when more than 3 students are female, and they're all hot."

Return to “Troubleshooting”