You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
while True:
filepath = os.path.join(DATA_FOLDERPATH, f'results_{datetime.now().date()}.csv')
result = read_input_registers(instrument, 0, 27)
if result is not None:
result.append(datetime.now().timestamp())
df = pd.DataFrame([result])
df.to_csv(filepath, mode='a', index=False, header=not os.path.exists(filepath))
sleep(3)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
-- coding: utf-8 --
import os
from datetime import datetime
from time import sleep
import pandas as pd
import minimalmodbus
SERIAL_PORT = '/dev/ttyUSB0'
BASE_FOLDERPATH = os.path.dirname(os.path.abspath(file))
DATA_FOLDERPATH = os.path.join(BASE_FOLDERPATH, 'data')
Function to read input registers
def read_input_registers(instrument, start_address, count):
try:
# Read input registers. Registers are 16 bits.
registers = instrument.read_registers(start_address, count, 4) # Function code 4
return registers
except minimalmodbus.NoResponseError as e:
print(f"No response from device: {e}")
except minimalmodbus.ModbusException as e:
print(f"Modbus error: {e}")
except Exception as e:
print(f"Error: {e}")
Configure the serial connection
instrument = minimalmodbus.Instrument(SERIAL_PORT, 1) # Port name, Slave address (in decimal)
instrument.serial.baudrate = 9600 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = minimalmodbus.serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout = 1 # seconds
while True:
filepath = os.path.join(DATA_FOLDERPATH, f'results_{datetime.now().date()}.csv')
result = read_input_registers(instrument, 0, 27)
if result is not None:
result.append(datetime.now().timestamp())
df = pd.DataFrame([result])
df.to_csv(filepath, mode='a', index=False, header=not os.path.exists(filepath))
sleep(3)
Beta Was this translation helpful? Give feedback.
All reactions