as I mentioned in the title i want to read out temperature values and stream it via serial port. The Problem is, that i get for every sensor port, without having a sensor connected, a strange negative value (-127.3...)
My code for reading out the sensors is:
import time
from sys import stdout
import serial
import serial.tools.list_ports
from daqhats import HatError, HatIDs, TcTypes, mcc134
from lib.daqhats_utils import select_hat_device
# Constants
CURSOR_BACK_2 = "\x1b[2D"
ERASE_TO_END_OF_LINE = "\x1b[0K"
def read_temp():
"""
Reads temperature values from multiple channels of an MCC 134 hat device.
Returns:
list: A list of temperature values read from each channel.
Raises:
HatError: If there is an error with the hat device.
ValueError: If there is an invalid value.
"""
# Rest of the code...
tc_type = TcTypes.TYPE_K # change this to the desired thermocouple type
channels = (0, 1, 2, 3)
try:
# Get an instance of the selected hat device object.
address = select_hat_device(HatIDs.MCC_134)
hat = mcc134(address)
for channel in channels:
hat.tc_type_write(channel, tc_type)
try:
temp_values = []
for channel in channels:
temp_values.append(hat.t_in_read(channel))
stdout.flush()
return temp_values
except KeyboardInterrupt:
# Clear the '^C' from the display.
print(CURSOR_BACK_2, ERASE_TO_END_OF_LINE, "\n")
except (HatError, ValueError) as error:
print("\n", error)
def find_serial_port() -> serial.Serial:
"""Find the first available serial port."""
ports = serial.tools.list_ports.comports()
for port in ports:
if port.description != "n/a":
return port.device
raise Exception("No valid serial port found")
def send_floats(floats: list[float]) -> None:
"""Send a list of floats over the specified serial port."""
# "/dev/ttyAMA0" is the default serial port on Raspberry Pi
with serial.Serial("/dev/ttyAMA0", 9600, timeout=1) as ser:
for f in floats:
ser.write(f"{f:.2f};".encode())
print(f"sent: {f}")
ser.write(b"\n")
if __name__ == "__main__":
try:
while True:
send_floats(read_temp())
time.sleep(2)
except KeyboardInterrupt:
print("\nExiting...")
Would be great if someone could help me or maybe have/had the same issue.
Question
bbfjumper
Hi,
as I mentioned in the title i want to read out temperature values and stream it via serial port. The Problem is, that i get for every sensor port, without having a sensor connected, a strange negative value (-127.3...)
My code for reading out the sensors is:
import time from sys import stdout import serial import serial.tools.list_ports from daqhats import HatError, HatIDs, TcTypes, mcc134 from lib.daqhats_utils import select_hat_device # Constants CURSOR_BACK_2 = "\x1b[2D" ERASE_TO_END_OF_LINE = "\x1b[0K" def read_temp(): """ Reads temperature values from multiple channels of an MCC 134 hat device. Returns: list: A list of temperature values read from each channel. Raises: HatError: If there is an error with the hat device. ValueError: If there is an invalid value. """ # Rest of the code... tc_type = TcTypes.TYPE_K # change this to the desired thermocouple type channels = (0, 1, 2, 3) try: # Get an instance of the selected hat device object. address = select_hat_device(HatIDs.MCC_134) hat = mcc134(address) for channel in channels: hat.tc_type_write(channel, tc_type) try: temp_values = [] for channel in channels: temp_values.append(hat.t_in_read(channel)) stdout.flush() return temp_values except KeyboardInterrupt: # Clear the '^C' from the display. print(CURSOR_BACK_2, ERASE_TO_END_OF_LINE, "\n") except (HatError, ValueError) as error: print("\n", error) def find_serial_port() -> serial.Serial: """Find the first available serial port.""" ports = serial.tools.list_ports.comports() for port in ports: if port.description != "n/a": return port.device raise Exception("No valid serial port found") def send_floats(floats: list[float]) -> None: """Send a list of floats over the specified serial port.""" # "/dev/ttyAMA0" is the default serial port on Raspberry Pi with serial.Serial("/dev/ttyAMA0", 9600, timeout=1) as ser: for f in floats: ser.write(f"{f:.2f};".encode()) print(f"sent: {f}") ser.write(b"\n") if __name__ == "__main__": try: while True: send_floats(read_temp()) time.sleep(2) except KeyboardInterrupt: print("\nExiting...")
Would be great if someone could help me or maybe have/had the same issue.
BR
Link to comment
Share on other sites
3 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now