Last active
August 29, 2015 14:19
-
-
Save RaspPiGuy/06e5e8cd88717d6997cc to your computer and use it in GitHub Desktop.
This is a program fragment of python program running on my Raspberry Pi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---This is a program fragment. The entire program can be seen at: https://github.com/RaspPiGuy/DS18B20TempSensor-Arduino-Pi/blob/master/Python%20Programs%20Running%20On%20The%20Pi/GraphRemoteTemperature_V1R3.py--- | |
import serial | |
ser = serial.Serial('/dev/ttyAMA0', 115200, timeout = 5) | |
frame_length = 20 # One frame is all the data for one device. | |
global start_time | |
def retrieve_serial(): | |
""" | |
Called by number_of_sensors() | |
First we flush anything that might be on the serial port | |
We write the character "s" to the serial port. | |
The Gertboard checks to see if this character is present | |
If so it will write the last series of measurements to the serial port | |
This function reads the data on the serial port every second | |
until it finds the number of bytes to match a multiple of the | |
number of bytes for each device. | |
If the serial data is not found in 10 seconds, the function exits | |
Returns the length of the message it has received. | |
""" | |
ser.flushInput() # Make sure no lingering bytes on serial port | |
ser.write("s") # send "s" to trigger Gertboard to transmit data | |
start_time = time.time() | |
while time.time() - start_time < 10: | |
time.sleep(1) | |
recv_length = ser.inWaiting() # finds how many bytes on serial port | |
if recv_length > 0 and recv_length % frame_length == 0: | |
return recv_length # good result | |
return 0 # failure | |
def number_of_sensors(): | |
""" | |
Called from get_stored_data() and get_measurements() | |
This calls retrieve_serial() | |
retrieve_serial() returns the length of the receive data | |
This function calculates the number of sensors from the | |
received data and populates recv_data[] with all the | |
data retrieved from the Gertboard. | |
Returns the number of sensors | |
""" | |
global recv_data | |
recv_data = [] # Where Gertboard data will go | |
sensors = 0 | |
recv_length = retrieve_serial() # will be 0 if failed | |
if recv_length: | |
sensors = recv_length / frame_length | |
in_coming = ser.read(size = recv_length) # get the serial data here | |
for i in range(recv_length): | |
recv_data.append(ord(in_coming[i])) | |
return sensors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment