Quantcast
Viewing all articles
Browse latest Browse all 1241

Python • Serial communication problem

Hi,

I have an HKW DCF clock (DB9 plug) and I would like to retrieve the time from it on my Raspberry Pi. Unfortunately, I can't use NTP because the Raspberry Pi will be in a location without internet access and power (powered by solar panel). I'm having trouble reading the time from this clock. The operation seems simple because all we need to do is send the character 'o' to the serial port, and in response, we will receive a string containing the date and time. Everything works fine in Windows with PuTTY, but unfortunately, neither on Windows with Python 3 nor on Raspberry Pi with Python 3 can I get the correct response from the clock. The transmission configuration in PuTTY and Python is the same, yet there seems to be an error somewhere. One of my recent test programs looks like this (on Windows):

Code:

import serialimport time# Serial port settingsport = 'COM11'speed = 300data_bits = 7stop_bits = 1parity = 'N'try:    # Open serial connection    ser = serial.Serial(        port=port,        baudrate=speed,        bytesize=data_bits,        stopbits=stop_bits,        parity=parity,        timeout=1    )        # Check if the serial port is opened successfully    if ser.isOpen():        print("Serial port", port, "has been opened successfully.")    else:        raise Exception("Failed to open serial port", port)    while True:        # Text to send        text_to_send = "o\r"        # Sending data (encoding text to ASCII)        ser.write(text_to_send.encode('ascii'))        # Delay before reading the response        time.sleep(1)        # Read the response        response = ser.readline()        # Display the response        print("Response from serial port:", response.decode('ascii'))        # If you want to exit the loop, you can use the break statement        # break    # Close the serial connection    ser.close()except Exception as e:    print("An error occurred:", e)
Maybe someone has an idea why the clock doesn't respond properly in Python. In PuTTY, I simply open the port, type 'o' in the console, and press Enter, and shortly after, I receive the desired string. The necessary transmission settings are entered in the program, which are 300 baud, 7 data bits, and 1 stop bit. Unfortunately, I'm out of ideas why it's not working Image may be NSFW.
Clik here to view.
:(


Best regards
Jack

Statistics: Posted by dojnikowski — Thu Jun 06, 2024 10:05 am



Viewing all articles
Browse latest Browse all 1241

Trending Articles