Mikale37963
Posts: 8
Joined: Mon Feb 04, 2019 5:16 pm

Getting data from py serial

Tue Jul 02, 2019 12:10 am

I'm running code to use two webcams and a raspberry pi to take a series of photos and save them with ascending names. what I'm trying to do is get either the increasing picture names to show on putty or anything related to the pictures since for this project we are using pyserial to prove that the program is working and since we are using pictures we need the picture titles or file size to show on putty. I'm also using fswebcam if that helps

what I have already tried is directly just getting data to send directly to putty using the example codes I have also tried encoding and decoding the data but the best I can get it that it displays /dev/ttyAMA0 in thonny or that the program is running
Code I'm using:

import os
import datetime
import serial
import time
from threading import Thread
def picFunc():
os.system("fswebcam -d /dev/video0 -r 1920x1080 -S 25
/home/pi/Documents/Pictures/%s.jpeg"
%datetime.datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"))
os.system("fswebcam -d /dev/video1 -r 1920x1080 -S 25
/home/pi/Documents/Pictures2/%s.jpeg"
%datetime.datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"))
t=5 # initialise the pause between pictures in seconds
count=10 # initialise the number of pictures to be taken
i=1 # initialise (reset) the counting sequence
totalTime=(t*count) # Calculate the time in seconds
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
ser.xonxoff =False
ser.rtscts= False
ser.dsrdtr = False
try:
ser.isOpen()
except Exception:
print('error open serial port: ' + str(Exception))
exit()
print(ser.portstr)
serialcmd = ('')
print(serialcmd)

#ser.write(bytes(str(serialcmd).encode(encoding='utf-8',
errors='ignore')))
#out = ser.read(125).decode(encoding='ascii', errors='ignore')
#print('recevied:' + out)
# Take a series of pictures one every t seconds
while (i<=count):
# initialise variables
leftTimeH=0
leftTimeM=0
leftTimeS=0

# taking a picture by calling a command line prompt
x=Thread(target=picFunc)
x.start()
totalTime=(t*(count-i)) # Calculate the time in seconds
print (i) # print the current count value to show progress
while (totalTime>=3600):
leftTimeH=leftTimeH+1
totalTime=totalTime-3600
while (totalTime>=60):
leftTimeM=leftTimeM+1
totalTime=totalTime-60
leftTimeS=totalTime
percentDone=((i/count)*100)
percentDone=round(percentDone,2)
message1=("Time left to finish " +repr(leftTimeH) +" Hours " +
repr(leftTimeM) +" Minutes and " + repr(leftTimeS) +" Seconds")
message2=(""+repr(percentDone) + "% Completed!")
print (message1)
print (message2)
ser.write(bytes(str(message1).encode(encoding='utf-8',
errors='ignore')))
out = ser.read(125).decode(encoding='ascii', errors='ignore')
print('recevied:' + out)
i=i+1
time.sleep(t)
print ("Finished!") # print to show when finished

Any ideas on why it's getting hung and not taking photos or sending over to putty the data?

PhatFil
Posts: 1309
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Getting data from py serial

Tue Jul 02, 2019 12:12 am

Mikale37963 wrote:
Tue Jul 02, 2019 12:10 am
I'm running code to use two webcams and a raspberry pi to take a series of photos and save them with ascending names. what I'm trying to do is get either the increasing picture names to show on putty or anything related to the pictures since for this project we are using pyserial to prove that the program is working and since we are using pictures we need the picture titles or file size to show on putty. I'm also using fswebcam if that helps

what I have already tried is directly just getting data to send directly to putty using the example codes I have also tried encoding and decoding the data but the best I can get it that it displays /dev/ttyAMA0 in thonny or that the program is running
Code I'm using:

Code: Select all

import os
import datetime
import serial
import time
from threading import Thread
def picFunc():
    os.system("fswebcam -d /dev/video0 -r 1920x1080 -S 25 
/home/pi/Documents/Pictures/%s.jpeg" 
%datetime.datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"))
    os.system("fswebcam -d /dev/video1 -r 1920x1080 -S 25 
 /home/pi/Documents/Pictures2/%s.jpeg" 
 %datetime.datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"))
 t=5      # initialise the pause between pictures in seconds           
 count=10 # initialise the number of pictures to be taken
 i=1       # initialise (reset) the counting sequence
 totalTime=(t*count) # Calculate the time in seconds
ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate = 1200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
    )
ser.xonxoff =False
ser.rtscts= False
ser.dsrdtr = False
try:
    ser.isOpen()
except Exception:
        print('error open serial port: ' + str(Exception))
         exit()
print(ser.portstr)
serialcmd = ('')
print(serialcmd)

#ser.write(bytes(str(serialcmd).encode(encoding='utf-8', 
errors='ignore')))
#out = ser.read(125).decode(encoding='ascii', errors='ignore')
#print('recevied:' + out)
# Take a series of pictures one every t seconds
while (i<=count):
    # initialise variables
    leftTimeH=0
    leftTimeM=0
    leftTimeS=0

    # taking a picture by calling a command line prompt
x=Thread(target=picFunc)
x.start()
totalTime=(t*(count-i))     # Calculate the time in seconds
print (i)       # print the current count value to show progress
while (totalTime>=3600):
        leftTimeH=leftTimeH+1
        totalTime=totalTime-3600
while (totalTime>=60):
        leftTimeM=leftTimeM+1
        totalTime=totalTime-60
leftTimeS=totalTime
percentDone=((i/count)*100)
percentDone=round(percentDone,2)
message1=("Time left to finish " +repr(leftTimeH) +" Hours " + 
repr(leftTimeM) +" Minutes and " + repr(leftTimeS) +" Seconds")
message2=(""+repr(percentDone) + "% Completed!")
print (message1)
print (message2)
ser.write(bytes(str(message1).encode(encoding='utf-8', 
errors='ignore')))
out = ser.read(125).decode(encoding='ascii', errors='ignore')
print('recevied:' + out)
i=i+1
time.sleep(t)  
print ("Finished!") # print to show when finished
Any ideas on why it's getting hung and not taking photos or sending over to putty the data?
using code tags for your cde makes things easier to read..

blimpyway
Posts: 120
Joined: Mon Mar 19, 2018 1:18 pm

Re: Getting data from py serial

Thu Jul 04, 2019 6:15 am

Nice try PhatPhil, there are still indents and line breaks..

Let Mikale put his own code in code tags.

Return to “Advanced users”