def identify_meter(ser):
“”“Send the identification request and adjust baud rate if needed.”“”
response = send_command(ser, “/?”, “Meter Identification”)
if response:
print(“Meter response for identification received.”)
if “9600” in response:
ser.baudrate = 9600
print(“Switching baud rate to 9600.”)
return response
else:
print(“No response from meter.”)
return None
def main():
port = “COM3” # Update with the correct port
initial_baud = 300 # Initial baud rate for IEC62056-21 communication
try:
ser = serial.Serial(port, baudrate=initial_baud, bytesize=7, parity='E', stopbits=1, timeout=15)
print(f"Connecting to meter on port {port}...")
# Step 1: Identify and adjust baud rate
identify_meter(ser)
# Step 2: Enter transparent mode
raw_response = send_command(ser, "\x06050\r\n", "Enter Transparent Mode")
# Step 3: Parse OBIS data
obis_data = parse_obis_data(raw_response)
if obis_data:
print("OBIS Data Captured:")
for key, value in obis_data.items():
print(f"{key}: {value}")
else:
print("No OBIS data captured.")
except serial.SerialException as e:
print(f"Serial communication error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'ser' in locals() and ser.is_open:
ser.close()
print("Serial connection closed.")
input("Press Enter to exit...")