User Tools

Site Tools


python_code

This is an old revision of the document!


Python Code

Python User String2

Python User String

Python User String
# /// script
# dependencies = ["pyserial>=3.5"]
# ///
import serial

def main() -> None:
    # the input string to send to the controller 
    user_string = "abcdefghij1234567890"
    save_settings = True
    
    # use an empty string for MS2000 (card_address = "")
    card_address = "2"

    # error checking
    if len(user_string) > 20:
        raise Exception("Max stored string length is 20.")

    # open the serial port and send characters to the controller
    with serial.Serial("COM4", 115200, timeout=1) as serial_port:
        # clear the current stored string
        serial_port.write(bytes(f"{card_address}BU Y-\r", encoding="ascii"))
        serial_port.readline()

        # send the input string to the controller
        for character in user_string:
            serial_port.write(bytes(f"{card_address}BU Y={ord(character)}\r", encoding="ascii"))
            serial_port.readline()

        # print the stored string and check to see if it's the same as the input string
        serial_port.write(bytes(f"{card_address}BU Y?\r", encoding="ascii"))
        response = serial_port.readline().decode().rstrip("\r\n")
        if response == user_string:
            print(f"Successfully stored the input string => {response}")
            if save_settings:
                serial_port.write(bytes(f"{card_address}SAVESET Z\r", encoding="ascii"))
                serial_port.readline()
                print("Settings saved to the controller using SAVESET Z.")
        else:
            print(f"Error: expected {user_string} but got {response} instead!")
        
if __name__ == "__main__":
    main()
Address: 29391 W Enid Rd. Eugene, OR 97402, USA | Phone: +1 (541) 461-8181
python_code.1743033331.txt.gz · Last modified: by brandon