This commit is contained in:
James 2024-04-26 11:53:15 +01:00
parent 482b6b28e5
commit ad93fb5bdb

View File

@ -1,4 +1,3 @@
#/bin/python
import socket
import sys
import time
@ -37,14 +36,18 @@ def send_to_graphite(data, host='10.6.0.1', port=2003):
while data:
message = data.popleft()
sock.sendall(message.encode('utf-8'))
#print(f"Sent to Graphite: {message.strip()}")
except (socket.error, socket.timeout) as e:
print(f"Failed to send data, will drop if buffer is full. Error: {e}", file=sys.stderr)
def main():
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
if len(sys.argv) < 2:
print("Usage: python script.py <serial_device>")
sys.exit(1)
serial_device = sys.argv[1]
ser = serial.Serial(serial_device, 115200, timeout=1)
data_buffer = deque(maxlen=100) # Buffer up to 100 messages
print("Reading data from /dev/ttyUSB0, press CTRL+C to quit:")
print(f"Reading data from {serial_device}, press CTRL+C to quit:")
try:
while True:
@ -56,13 +59,9 @@ def main():
for data in graphite_data:
if len(data_buffer) < data_buffer.maxlen:
data_buffer.append(data + '\n')
#print(f"Added to buffer: {data.strip()}")
else:
print("Buffer full, dropping data", file=sys.stderr)
# Try to send data if the buffer is not empty
if data_buffer:
#print("Attempting to send data to Graphite...")
send_to_graphite(data_buffer)
except KeyboardInterrupt: