I have been trying to write a python script that prints the first 10 lines of the CAN Bus data that is coming from a data pipe. The code works well when there is data in the pipe, but doesn't break out of the loop when no data.
#!/usr/bin/python2.7
import os
import re
with open('/var/run/regen/can0/rx', 'r') as pse: #/var/run/regen/can0/rx is the data pipe
count = 0
for line in pse:
if " " in line:
break #want to exit the loop when there is no data
else:
while count < 10:
for line in pse:
words = list(filter(None, re.split(r"[()\[\]\s]\s*", line)))
print(words)
count += 1
break
break
Summary --- Works - Prints first 10lines when data is coming through the pipe Doesn't work - The loop doesn't break when there is no data in the pipe
I tried using a timer to exit the loop, but it doesn't work as well. The loop doesn't exit when the time given is complete.
#!/usr/bin/python2.7
import os
import time
t_end = time.time() + 0.2
while time.time() < t_end:
with open('/var/run/regen/can0/rx', 'r') as pse:
# --- remaining code ----
**Can anyone tell me how to check if a data pipe has any data incoming or not? **