Reading Serial Port data and saving to .csv Python - python

I'm having success reading in and printing some Serial Port data in a python script. I'm having difficulty with what tools/functions to use to change the Serial port data into a table or continuously append to a .csv. So after spending many hours researching everything about Serial Ports and python on stack overflow and other sites I'm reaching out for a hand as I don't know what steps to take next and I can't find information that helps answer my question.
Here is my script so far:
ser = serial.Serial('com4',
baudrate=38400,
timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
while True:
rawdata = (ser.readline().decode('ascii'))
print(rawdata)
Ok....great we have the Sensor data printing in the console looking like this:
$PDLMA,+11.91,+20.7,-0.1,-0.4*6C
$PDLMH,134829.844,+2644.8,+81.46,+3094.7,+21.99*6F
$PDLM1,134829.844,+1824.1,+127.69,+3276.7,+36.82*26
Now here's how the data from the sensor is structured:
$PDLMH 134829.844 2644.8 81.46 3094.7 21.99 *6F
Hhmmss.sss HCP05 HCPI PRP05 PRP05I Checksum
$PDLM1 134829.844 1824.1 127.69 3727.7 36.82 *26
Hhmmss.sss HCP10 HCP10I PRP10 PRP10I Checksum
$PDLMA 11.91 20.7 -0.1 -0.4
Voltage Temperature Pitch Roll
Now my goal is to get the final output to look like this:
Time HCP05 HCP05I PRP05 PRP05I HCP10 HCP10I PRP10 PRP10I Voltage Temp Pitch
(now) data data data data data data data data Data Data Data
So what should I do to take the serial data and transform it to what I need above as well as continuously reading in the data? I have tried dataframes, lists, etc but clearly I'm missing something here. In the .csv output file I would expect there to be about 120000 lines of data on average in our use case.
The Newbie of the day.

I achieved my desired output with the following code:
while dser.inWaiting() == 0:
pass
NMEA1 = dser.readline().decode("ascii")
while dser.inWaiting() == 0:
pass
NMEA2 = dser.readline().decode("ascii")
while dser.inWaiting() == 0:
pass
NMEA3 = dser.readline().decode("ascii")
while dser.inWaiting() == 0:
pass
NMEA4 = dser.readline().decode("ascii")
while dser.inWaiting() == 0:
pass
NMEA1_array = NMEA1.split(',')
NMEA2_array = NMEA2.split(',')
NMEA3_array = NMEA3.split(',')
NMEA4_array = NMEA4.split(',')
if NMEA1_array[0] == '$PDLMH':
HCP05 = NMEA1_array[2]
PRP05 = NMEA1_array[4]
if NMEA1_array[0] == '$PDLM1':
HCP10 = NMEA1_array[2]
PRP10 = NMEA1_array[4]
if NMEA1_array[0] == '$PDLM2':
HCP20 = NMEA1_array[2]
PRP20 = NMEA1_array[4]
if NMEA1_array[0] == '$PDLMA':
Voltage = NMEA1_array[1]
Temperature = NMEA1_array[2]
Pitch = NMEA1_array[3]
Roll = NMEA1_array[4].partition("*")[0]
if NMEA2_array[0] == '$PDLMH':
HCP05 = NMEA2_array[2]
PRP05 = NMEA2_array[4]
if NMEA2_array[0] == '$PDLM1':
HCP10 = NMEA2_array[2]
PRP10 = NMEA2_array[4]
if NMEA2_array[0] == '$PDLM2':
HCP20 = NMEA2_array[2]
PRP20 = NMEA2_array[4]
if NMEA2_array[0] == '$PDLMA':
Voltage = NMEA2_array[1]
Temperature = NMEA2_array[2]
Pitch = NMEA2_array[3]
Roll = NMEA2_array[4].partition("*")[0]
if NMEA3_array[0] == '$PDLMH':
HCP05 = NMEA3_array[2]
PRP05 = NMEA3_array[4]
if NMEA3_array[0] == '$PDLM1':
HCP10 = NMEA3_array[2]
PRP10 = NMEA3_array[4]
if NMEA3_array[0] == '$PDLM2':
HCP20 = NMEA3_array[2]
PRP20 = NMEA3_array[4]
if NMEA3_array[0] == '$PDLMA':
Voltage = NMEA3_array[1]
Temperature = NMEA3_array[2]
Pitch = NMEA3_array[3]
Roll = NMEA3_array[4].partition("*")[0]
if NMEA4_array[0] == '$PDLMH':
HCP05 = NMEA4_array[2]
PRP05 = NMEA4_array[4]
if NMEA4_array[0] == '$PDLM1':
HCP10 = NMEA4_array[2]
PRP10 = NMEA4_array[4]
if NMEA4_array[0] == '$PDLM2':
HCP20 = NMEA4_array[2]
PRP20 = NMEA4_array[4]
if NMEA4_array[0] == '$PDLMA':
Voltage = NMEA4_array[1]
Temperature = NMEA4_array[2]
Pitch = NMEA4_array[3]
Roll = NMEA4_array[4].partition("*")[0]
if () is not None:
return (float(HCP05), float(PRP05), float(HCP10),
float(PRP10), float(HCP20), float(PRP20),
float(Voltage), float(Temperature), float(Pitch),
float(Roll))
Not to sure if this is the best result but it is now working.

Related

How do I add a daterange to open trades during when backtesting with backtrader?

I am trying to backtest a strategy where trades are only opened during 8.30 to 16.00 using backtrader.
Using the below attempt my code is running but returning no trades so my clsoing balane is the same as the opening. If I remove this filter my code is running correctly and trades are opening and closing so it is definitely the issue. Can anyone please help?
I have tried adding the datetime column of the data to a data feed using the below code:
` def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close
self.datatime = mdates.num2date(self.datas[0].datetime)
self.datatsi = self.datas[0].tsi
self.datapsar = self.datas[0].psar
self.databbmiddle = self.datas[0].bbmiddle
self.datastlower = self.datas[0].stlower
self.datastupper = self.datas[0].stupper
# To keep track of pending orders
self.order = None`
I then used the following code to try filter by this date range:
# Check if we are in the market
if not self.position:
current_time = self.datatime[0].time()
if datetime.time(8, 30) < current_time < datetime.time(16, 0):
if self.datatsi < 0 and self.datastupper[0] > self.dataclose[0] and self.datastlower[1] < self.dataclose[1] and self.dataclose[0] < self.databbmiddle[0] and self.datapsar[0] > self.dataclose[0]:
self.log('SELL CREATE, %.2f' % self.dataclose[0])
# Keep track of the created order to avoid a 2nd order
os = self.sell_bracket(size=100,price=sp1, stopprice=sp2, limitprice=sp3)
self.orefs = [o.ref for o in os]
else:
o1 = self.buy(exectype=bt.Order.Limit,price=bp1,transmit=False)
print('{}: Oref {} / Buy at {}'.format(self.datetime.date(), o1.ref, bp1))
o2 = self.sell(exectype=bt.Order.Stop,price=bp2,parent=o1,transmit=False)
print('{}: Oref {} / Sell Stop at {}'.format(self.datetime.date(), o2.ref, bp2))
o3 = self.sell(exectype=bt.Order.Limit,price=bp3,parent=o1,transmit=True)
print('{}: Oref {} / Sell Limit at {}'.format(self.datetime.date(), o3.ref, bp3))
self.orefs = [o1.ref, o2.ref, o3.ref] # self.sell(size=100, exectype=bt.Order.Limit, price=self.data.close[0]+16, parent=self.order, parent_bracket=bt.Order.Market)

text files are empty after I run my script

TL;DR
When I run my program it deletes all my txt file data, I do not understand why.
I'm making a waypoint editor/saver for my robot arm, and this program is meant to allow me to choose from a 'set' which is just a txt file, and then change or set a new point in that 'set' and it works to an extent. when I run the program I can enter a point, change values and save them, and if I choose to set another point my old point is still there, and if I end the program I can check the txt file and it has all the values. the problem is when I rerun the program, even if I don't get past the first input command, and I stop it immediately it will clear all my text files, and I haven't even chosen witch txt file to edit, I understand that it will run all imported modules when I run the program, but none of those subprograms were giving me grief in my other version of my robot arm control system so I am at a loss as to what I'm doing wrong, I've been at this problem for 2 days now and haven't gotten anywhere. I apologize for my total lack of commenting, I'm trying to work on that but I was excited to make this program and got carried away.
from machine import Pin, PWM, ADC, I2C
from time import sleep
import Move
from pico_i2c_lcd import I2cLcd
def EDITMODE():
analog_value = machine.ADC(28)
i2c = I2C(0, sda=Pin(0), scl =Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
stv=0
base = PWM(Pin(18))
base.freq(50)
lcdt = 1
j2 = PWM(Pin(19))
j2.freq(50)
j3 = PWM(Pin(20))
j3.freq(50)
claw = PWM(Pin(21))
claw.freq(50)
buttonmove = 20000
b1 = Pin(2, Pin.IN, Pin.PULL_DOWN)
b2 = Pin(3, Pin.IN, Pin.PULL_DOWN)
b3= Pin(4, Pin.IN, Pin.PULL_DOWN)
b4= Pin(5, Pin.IN, Pin.PULL_DOWN)
b5= Pin(6, Pin.IN, Pin.PULL_DOWN)
b6= Pin(7, Pin.IN, Pin.PULL_DOWN)
b7= Pin(8, Pin.IN, Pin.PULL_DOWN)
b8= Pin(9, Pin.IN, Pin.PULL_DOWN)
b9= Pin(10, Pin.IN, Pin.PULL_DOWN)
b10= Pin(11, Pin.IN, Pin.PULL_DOWN)
b11= Pin(12, Pin.IN, Pin.PULL_DOWN)
b12= Pin(13, Pin.IN, Pin.PULL_DOWN)
bshift= Pin(14, Pin.IN, Pin.PULL_DOWN)
l1led = Pin(16, Pin.OUT)
l2led= Pin(17, Pin.OUT)
global gha
gha = 0
buttonmove = 5000
j1v =1222500
j4v =1282500
j2v =1312500
j3v=1762500
break1 = 0
while True:
if break1 ==1:
break
#decides which waypoint to edit
eprog = int(input('Which Set would you like to edit (1-5) '))
if eprog == 1:
curentprogram = 'Prog1.txt'
elif eprog == 2:
curentprogram = 'Prog2.txt'
elif eprog == 3:
curentprogram = 'Prog3.txt'
elif eprog == 4:
curentprogram = 'Prog4.txt'
elif eprog == 5:
curentprogram = 'Prog5.txt'
file = open(curentprogram, "r")
WPL = file.readlines()
file.close()
x = len(WPL)
print(WPL)
print('There are ', str(x/6), ' Waypoints currently set')
epoint1 = int(input('Which waypoint would you like to set? '))
epoint = (epoint1*6)
if epoint >0:
epoint = epoint+1
print('Move robot to desired position then save waypoint')
sleepvalue = 0
while True:
b1v = b1.value()
b2v = b2.value()
b3v = b3.value()
b4v = b4.value()
b5v = b5.value()
b6v = b6.value()
b7v = b12.value()
b8v = b11.value()
b9v = b10.value()
b10v = b9.value()
b11v = b8.value()
b12v = b7.value()
shiftb = bshift.value()
if shiftb == 1:
l2led(0)
if b1v ==1:
j1v = j1v+buttonmove
if b12v ==1:
j1v = j1v-buttonmove
if b2v ==1:
j2v = j2v+buttonmove
if b11v ==1:
j2v = j2v-buttonmove
if b3v ==1:
j4v = j4v - buttonmove
if b10v ==1:
j4v = j4v + buttonmove
if b4v ==1:
j3v = j3v+buttonmove
if b9v ==1:
j3v = j3v-buttonmove
else:
l2led(1)
if b6v == 1:
sleepvalue = sleepvalue+1
print('sleep time =', sleepvalue)
sleep(.5)
if b7v == 1:
sleepvalue = sleepvalue-1
if sleepvalue < 0:
sleepvalue = 0
print('sleep time =', sleepvalue)
sleep(.5)
if b1v == 1:
print(epoint, x)
if epoint+.01 > (x/6):
print('value not written, new waypoint value = ', str((x/6)+1))
WPL.append(j1v)
WPL.append(j2v)
WPL.append(j3v)
WPL.append(j4v)
WPL.append(x/6)
WPL.append(sleepvalue)
else:
print('Saving Point')
print(epoint)
epoint = int(epoint)
WPL[epoint] = j1v
WPL[epoint+1] = j2v
WPL[epoint+2] = j3v
WPL[epoint+3] = j4v
WPL[epoint+4] = (epoint/6)
WPL[epoint+5] = sleepvalue
print(WPL)
file = open(curentprogram, 'w')
file.write('')
file.close
file = open(curentprogram, 'a')
print(WPL)
for item in WPL:
print(item, 'bannana')
file.write((str(item))+'\n')
file.close()
restart = input('would you like to set another point? (y/n) ')
if restart == 'n' or restart == 'N':
return
else:
break
sleep(.025)
print(j1v, j2v, j3v, j4v)
#base.duty_ns(int(j1v))
#j2.duty_ns(int(j2v))
#j3.duty_ns(int(j3v))
#claw.duty_ns(int(j4v))
if __name__ == '__main__':
print('hello')
EDITMODE()
The issue comes from the following line:
file = open(curentprogram, 'w')
When appending data to a file, you must use mode a for append. Otherwise, when using w, if a file with that name exists, it is removed.
According to the documentation:
[...] mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.
EDIT
Since I cannot run your code, I would suggest you to start with a simpler script and start building up yours from there. For instance, lets some.txt be a text file with he following content:
$ cat some.txt
foo
Then you can experiment with the following code:
with open('some.txt', 'r') as file:
lines = file.readlines()
print(lines)
# this prints: ['foo\n']
with open('some.txt', 'w') as file:
file.write('')
with open('some.txt', 'a') as file:
file.write('bar\n')
then:
$ cat some.txt
bar
So, this logic seems to work fine. From here, try to see what you are doing and how it might affect the i/o operations.

Read data from a DDSU666 using RS485

I have tried my luck with these codes to read the data from the DDSU666-H and it seems that I am able to read data, but I don't know what to do with it. In theory it is a list of data, but I am not able to understand what I am receiving or how to translate it into something I can use
import serial # import the module
def banner_bottom():
print(' +-------------------------------------------+')
print(' | SPACE |')
print(' +-------------------------------------------+')
COM_Port = serial.Serial('COM3')
COM_Port.baudrate = 9600 # set Baud rate
COM_Port.bytesize = 8 # Number of data bits = 8
COM_Port.parity = 'N' # No parity
COM_Port.stopbits = 1 # Number of Stop bits = 1
COM_Port.setRTS(1) #RTS=1,~RTS=0 so ~RE=0,Receive mode enabled for MAX485
COM_Port.setDTR(1) #DTR=1,~DTR=0 so DE=0,(In FT232 RTS and DTR pins are inverted)
#~RE and DE LED's on USB2SERIAL board will be off
RxedData = COM_Port.readline()
print(' ',RxedData, '\n')
COM_Port.close() # Close the Serial port
banner_bottom()# Display the bottom banner
Output:
b'\x7f~\xbb\xff\xfb\xe3=\x7f~_cuI_\x0e\x7f~\xbb\xff\xfb\xe3=\x7f~_]\xd3V\'\t\x7f~\xbb\xff\xfb\xe3=\x7f~__\x07\xf5tU\x7f~\xbb\xff\xfb\xe3=\x7f~_[y\x8d\xba\x00\x7f~\xbb\xff\xfb\xe3=\x7f~_YsGe\x18\x7f~\xbb\xff\xfb\xe3=\x7f~_Y\xe3\x06n\x16\x7f~\xbb\xff\xfb\xe3=\x7f~__O]I\x1d\x7f~\xbb\xff\xfb\xe3=\x7f~_ao\xff\xf0\x05\x7f~\xbb\xff\xfb\xe3=\x7f~_co-O\x10\x7f~\xbb\xff\xfb\xe3=\x7f~__\x15\x117\x03\x7f~\xbb\xff\xfb\xe3=\x7f~__!-\xc9/\x7f~\xbb\xff\xfb\xe3=\x7f~__}\xed|\x00\x7f~\xbb\xff\xfb\xe3=\x7f~_c\xb7+\xda\x00\x7f~\xbb\xff\xfb\xe3=\x7f~_e\x8f"U\x01\x7f~\xfd\xff\xfb]\x1a\x7f~_W\xaf\x08\x975\x00\x7f~\xfd\xff\xfb]\x1a\x7f~_W\xaf\x08\x975\x00\x7f~\xfd\xff\xfb]\x1a\x7f~_W\xaf\x08\x975\x00\x7f~\xbb\xff\xfb\xe3=\x7f~_]\xd3V\'\t\x7f~\xbb\xff\xfb\xe3=\x7f~__\xd9\xadb\x08\x7f~\xbb\xff\xfb\xe3=\x7f~__\xc5[P\x05\x7f~\xbb\xff\xfb\xe3=\x7f~__\x91\xfef\x00\x7f~\xbb\xff\xfb\xe3=\x7f~_c3\xb3T\x0f\x7f~\xbb\xff\xfb\xe3=\x7f~_e\xbd\x01\xb0\x03\x7f~\xbb\xff\xfb\xe3=\x7f~_e?y\x84\x19\x7f~\xbb\xff\xfb\xe3=\x7f~_\xe349\xac\n'
Code 2:
#!/usr/bin/env python3
import serial
port = 'COM3'
ComPort = serial.Serial(port)
ComPort.baudrate = 9600
ComPort.bitesize = 8
ComPort.parity = 'N'
ComPort.stopbits = 1
dataIn=ComPort.read(6)
print(dataIn)
ComPort.close()
Ouput:
b'\x7f~\xbb\xff\xfb\xe3'
I've tried to translate the output into something I can understand, but I haven't been able to see anything either.
binary_data = b'\x7f~\xbb\xff\xfb\xe3'
aa = binary_data.hex()
print(aa)
#OUT: 7f7ebbfffbe3
bb = ''.join(['%02x' % b for b in binary_data])
print(bb)
#OUT: 7f7ebbfffbe3
s = binary_data.decode('cp855')
print(s)
#OUT:~╗ чÑ
What can I try to resolve this?

Python - calculations with more than one splited serial data

I use Arduino to receive data from sensors (4 types of data : humidity, temperature, photocell and milliseconds)
Datas comes like this : xx xx xx xxxx in the serial buffer. (data space data space etc...)
I split this line in order to isolate each data because I want to make individual calculations for each sensor.
Calculation for each sensor consist on : ((latest_data) - (data_of_previous_line), latest_data) in order to get a tuple for each sensor. I want all the sensors tuples appearing in the same line.
Doing this with 1 sensor and 1 method (calculate()) is working fine but it doesn't work if I add a second sensor in sensors() object !
QUESTION : how to make all this working with at least 2 sensors data ?
(the code below is working perfectly with 1 "splited" sensor data).
Thanks in advance.
class _share:
def __init__(self):
self.last_val = [0 for i in range(2)]
def calculate(self, val):
self.last_data = val
self.last_val = [self.last_data] + self.last_val[:-1]
diff = reduce(operator.__sub__, self.last_val)
print (diff, val)
return (diff, val)
share = _share()
ser = serial.Serial('/dev/ttyS1', 9600, timeout=0.1)
def sensors():
while True:
try:
time.sleep(0.01)
ser.flushInput()
reception = ser.readline()
receptionsplit = reception.split()
sensor_milli = receptionsplit[3]
sensor_pho_1 = receptionsplit[2]
sensor_tem_1 = receptionsplit[1]
sensor_hum_1 = receptionsplit[0]
int_sensor_milli = int(sensor_milli)
int_sensor_pho_1 = int(sensor_pho_1)
int_sensor_tem_1 = int(sensor_tem_1)
int_sensor_hum_1 = int(sensor_hum_1)
a = int_sensor_milli
b = int_sensor_pho_1
c = int_sensor_tem_1
d = int_sensor_hum_1
return str(share.calculate(b))
except:
pass
time.sleep(0.1)
f = open('da.txt', 'ab')
while 1:
arduino_sensor = sensors()
f.write(arduino_sensor)
f.close()
f = open('da.txt', 'ab')
You need to use different share instance for each sensor otherwise the calculations will be wrong. So use share_a, share_b, share_c and share_d for a, b, c and d respectively for example. Now if I understand this correctly, you can return all the sensors at once by changing your return to:
return [ str(share_a.calculate(a)), str(share_b.calculate(b)), str(share_c.calculate(c)), str(share_d.calculate(d)) ]
The above would return a list containing all 4 sensors and then in your main method you can change to:
arduino_sensor = sensors()
sensor_string ="a:%s b:%s c:%s d:%s"%( arduino_sensor[0], arduino_sensor[1], arduino_sensor[2], arduino_sensor[3] )
print sensor_string # single line screen print of all sensor data
f.write( sensor_string )
I hope that is helpful.

Python (3) readline returns empty string regardless of what is in the file

I have a Python (3) program that reads some data in from a file, gets the relevant parts and spits it out to various other places. The problem is that file.readline() has started refusing to get any lines from any file. It just returns an empty string as if it was at the end of the file. The files are not the problem, I checked that with a quick script (read file, print line by line). The possible solution (although I don't know why) is I used 2to3 to convert the code to python3, but looking at the diff it printed I don't see any modifications to the file I/O stuff.
Here is the program:
import threading
import time
def runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs):
timer0 = threading.Timer(.0001, turnOffLED, [0, 0, 0, 1])
numEvents = getNumEvents(eventFile)
eventCount = 0
while 1:
linein = eventFile.readline()
#if linein == '' or '\n' or '\r\n' or '\r': break
#above checks if the end of file has been reached, below is debugging
if linein == '': print('EOF')
if linein == '\n': print('linefeed')
if linein == '\r\n': print('carrige return/ linefeed')
if linein == '\r' : print('carrige return')
if linein == 'New Event\n':
print('New Event', eventCount, ': file is',
(eventCount/numEvents)*100, '% done.')
eventCount += 1
#insert event sleep time here
continue
linein = linein.split(' ')
del(linein[::2])
linein[2] = linein[2][:-1]
linein[1] = float(linein[1])
if linein[1] < 0: linein[1] = linein[1] * (-1)
channel = channelLookup(linein[0], channelRefFileName)
#serialPorts[channel[0]].write(str(channel[1],linein[2]))
if timer0.isAlive:
#use backup timer
timer1 = threading.Timer(0.005, turnOffLED, [serialPorts, channel[0], channel[1], 0])
timer1.start()
else:
#use primary timer
timer0 = threading.Timer(0.005, turnOffLED, [serialPorts, channel[0], channel[1], 0])
timer0.start()
print('Sleeping:', linein[1]*timeScale)
time.sleep(linein[1]*timeScale)
def channelLookup(channel, channelRefFile):
channelRef = open(channelRefFile, 'r')
for line in channelRef:
if channel in line:
linein = line
break
linein = linein.split('-')
channelRef.close()
return linein[1:]
def turnOffLED(serialPorts, arduino, pmt, bs):
if bs: return -1
#serialPorts[arduino].write(str(pmt,0))
def getNumEvents(eventFile):
i = 0
for lines in eventFile:
if lines == 'New Event\n':
i += 1
return i
The above is run by:
import hawc
import datetime
timeScale = 0.001
#timeScale of 0.000001 results in runtime of 0:06:52.858136
#a timeScale of 0.001 is 9:28:34.837992
eventSleep = 0
maxPEs = 1000
serialPorts = [0, 1, 2, 3, 4]
channelRefFileName = 'Data/hawc-arduino_channel_lookup'
st = datetime.datetime.now()
print('Start time is', st)
eventFile = open('Data/events.txt', 'r')
hawc.runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs)
eventFile.close()
et = datetime.datetime.now()
print('End time is', et)
print('File has run for', et-st)
print('--------Done---------')
And here is some sample data that the program might handle (one file might be about 500000 lines):
Channel: 447 Time: 121.263 PEs: 2263.38
Channel: 445 Time: 118.556 PEs: 1176.54
Channel: 448 Time: 120.384 PEs: 1159.59
Channel: 446 Time: 122.798 PEs: 983.949
Channel: 499 Time: 129.983 PEs: 762.07
getNumEvents consumes the entire file, so when you go around reading it, its pointer is already at the end. Instead, reset the file pointer with seek, like this:
def runFile(eventFile, channelRefFileName, serialPorts, timeScale, maxPEs):
timer0 = threading.Timer(.0001, turnOffLED, [0, 0, 0, 1])
numEvents = getNumEvents(eventFile)
eventFile.seek(0, 0)
eventCount = 0
for linein in eventFile:
if linein == '': print('EOF')
....

Categories

Resources