|
| 1 | +# NAME: serial_write.py |
| 2 | +# AUTHOR: Matthew Miller |
| 3 | +# PURPOSE: Records data from iMet sensor into a file called IMETDATAXX.CSV. |
| 4 | +# If the program detects that an ADC has been attached, the program |
| 5 | +# will send ADC data, immediately followed by iMet data through the |
| 6 | +# serial port. This is intended to be used with an xBee in coordination |
| 7 | +# with a ground station computer. The ground station computer will be |
| 8 | +# set up to display a live graph of the data. |
| 9 | + |
| 10 | +#NOTE: all xbee communication lines have been commented out for the time being (serial initialization and a single write inside the while loop |
| 11 | +# This was done on 6/7/21 for testing purposes) |
| 12 | + |
| 13 | +import os |
| 14 | +import time |
| 15 | +import serial |
| 16 | +import sys |
| 17 | +from gpiozero import LED, Button |
| 18 | + |
| 19 | +#check if Arduino ADC is connected via USB |
| 20 | +#if not used, program continues with only iMET recording |
| 21 | +try: |
| 22 | + ser_ADC = serial.Serial( |
| 23 | + port='/dev/ARD', |
| 24 | + baudrate = 9600, |
| 25 | + parity=serial.PARITY_NONE, |
| 26 | + stopbits=serial.STOPBITS_ONE, |
| 27 | + bytesize=serial.EIGHTBITS, |
| 28 | + timeout=2 |
| 29 | + ) |
| 30 | + use_ADC = 1 |
| 31 | +except: #error if no adc attached. |
| 32 | + print("No ADC attached. Not using.\n") |
| 33 | + use_ADC = 0 |
| 34 | + |
| 35 | + |
| 36 | +#ser_xBee = serial.Serial( |
| 37 | +# port='/dev/ttyS0', |
| 38 | +# baudrate = 9600, |
| 39 | +# parity=serial.PARITY_NONE, |
| 40 | +# stopbits=serial.STOPBITS_ONE, |
| 41 | +# bytesize=serial.EIGHTBITS, |
| 42 | +# timeout=1 |
| 43 | +#) |
| 44 | + |
| 45 | +#check if iMet attached |
| 46 | +try: |
| 47 | + ser_SAMA = serial.Serial( |
| 48 | + port='/dev/ttyUSB1', |
| 49 | + baudrate = 9600, |
| 50 | + parity=serial.PARITY_NONE, |
| 51 | + stopbits=serial.STOPBITS_ONE, |
| 52 | + bytesize=serial.EIGHTBITS, |
| 53 | + timeout=1 |
| 54 | + ) |
| 55 | +except: |
| 56 | + print("no iMET attached, cannot continue. \n") |
| 57 | + exit() |
| 58 | + |
| 59 | +try: |
| 60 | + collect_ADC = LED(27) |
| 61 | + stop_button = Button(25, pull_up=False) |
| 62 | + |
| 63 | + counter=0#potentially unneccessary |
| 64 | + |
| 65 | +#open iMET file |
| 66 | +#first get filename |
| 67 | + config = "/home/pi/BC6B/CONFIG" |
| 68 | + location = sys.argv[1] |
| 69 | + init_SAMA_filename = "SAMADATA" |
| 70 | + for i in range(100): |
| 71 | + j = i-1 |
| 72 | + to_append = str(j)+".CSV" #TODO was i |
| 73 | + config_to_append = str(i) + ".YML" |
| 74 | + true_config = config + config_to_append |
| 75 | + SAMA_appended = location + init_SAMA_filename + to_append |
| 76 | + file_exists = os.path.isfile(true_config) |
| 77 | + if file_exists == 0: #if file doesn't exist exit loop and create it |
| 78 | + #file is created in the following while loop |
| 79 | + print("file created") |
| 80 | + break |
| 81 | + |
| 82 | + print("launching while loop \n") |
| 83 | + |
| 84 | + temp = 1 |
| 85 | + while(True): |
| 86 | + if use_ADC == 1: |
| 87 | + SAMA_file = open(SAMA_appended, "a+") #open new iMET file appending |
| 88 | + data_SAMA = ser_SAMA.readline().decode() |
| 89 | + collect_ADC.on() #tell arduino to grab data |
| 90 | + collect_ADC.off() |
| 91 | + data_ADC = ser_ADC.readline().decode() |
| 92 | + #data_all = data_ADC + data_iMET |
| 93 | + #ser_xBee.write(data_all) |
| 94 | + SAMA_file.write(data_SAMA) |
| 95 | + SAMA_file.write("\n") |
| 96 | + SAMA_file.close() |
| 97 | + |
| 98 | + else: |
| 99 | + SAMA_file = open(SAMA_appended, "a+") #open new iMET file appending |
| 100 | + data_SAMA = ser_SAMA.readline().decode() |
| 101 | + SAMA_file.write(data_SAMA) |
| 102 | + SAMA_file.write("\n") |
| 103 | + SAMA_file.close() |
| 104 | + time.sleep(1) |
| 105 | + |
| 106 | +#stop_button.wait_for_press() |
| 107 | +#print("aye bro the button was pressed") |
| 108 | + """ |
| 109 | + time.sleep(1) |
| 110 | + for x in range (10): |
| 111 | + iMET_file = open(iMET_appended, "a+") #open new iMET file appending |
| 112 | + data_iMET = ser_iMET.readline().decode() |
| 113 | + print(data_iMET) |
| 114 | + iMET_file.write(data_iMET) |
| 115 | + iMET_file.write("\n") |
| 116 | + iMET_file.close() |
| 117 | + |
| 118 | +
|
| 119 | + print("done\n") |
| 120 | + """ |
| 121 | +except: |
| 122 | + print("exception thrown") |
| 123 | + if not SAMA_file.closed: |
| 124 | + SAMA_file.close() |
| 125 | + |
| 126 | + |
0 commit comments