Recently our customer purchased one of our USB controlled power outlets.

After purchasing the item, the customer asked us how they could script under windows the control of the device.  We suggested the use of python and pointed the customer to some example python scripts that write to a virtual com port.  The customer stated they never used python and was not comfortable with this solution.  At this point, we thought about two options and provided these options to the customer:

  1. We write a python wrapper for the device and use something like pyinstaller to make a windows executable they could simply run
  2. We entered some questions to chatGPT, showed them the result, and suggested they could continue asking some more questions and get the solution they were looking for.

Here's our conversation with chatGPT:

write a python program to send "ON" to the serial port

To send the string "ON" to a serial port using Python, you can use the pyserial library. First, you need to install the library if you haven't already. You can install it using pip:

sh
pip install pyserial

Here is a simple Python code to send "ON" to a serial port:

python
import serial

# Replace 'COM3' with the name of your serial port. 
# For example, on Windows it might be 'COM3', on Linux it might be '/dev/ttyUSB0'
serial_port = 'COM3'
baud_rate = 9600  # Set the baud rate to match your device

# Create a serial connection
ser = serial.Serial(serial_port, baud_rate)

# Ensure the connection is open
if ser.isOpen():
    print(f"Connected to {serial_port} at {baud_rate} baud.")
else:
    print(f"Failed to connect to {serial_port}")
    exit(1)

ser.write(b"ON")

# Optionally, you can close the connection
ser.close()

print("Message sent to the serial port.")

Key Points:

  1. Serial Port: Replace 'COM3' with your actual serial port name.
  2. Baud Rate: Ensure that the baud rate matches the settings of the device you are communicating with.
  3. Sending Dataser.write(b"ON") sends the string "ON" to the serial port. The b before the string indicates a bytes object, which is required by pyserial.

Usage:

  1. Ensure your device is connected and note the correct serial port.
  2. Update the serial_port variable with the correct port.
  3. Run the script. The message "ON" will be sent to the serial port
terminate the "ON" string with a carriage return and line feed

To send the string "ON" terminated with a carriage return (CR) and line feed (LF), you need to append \r\n to the string before sending it through the serial port. Here’s how you can modify the code:

python
import serial

# Replace 'COM3' with the name of your serial port. 
# For example, on Windows it might be 'COM3', on Linux it might be '/dev/ttyUSB0'
serial_port = 'COM3'
baud_rate = 9600  # Set the baud rate to match your device

# Create a serial connection
ser = serial.Serial(serial_port, baud_rate)

# Ensure the connection is open
if ser.isOpen():
    print(f"Connected to {serial_port} at {baud_rate} baud.")
else:
    print(f"Failed to connect to {serial_port}")
    exit(1)

# Send "ON" to the serial port with CR and LF
ser.write(b"ON\r\n")

# Optionally, you can close the connection
ser.close()

print("Message sent to the serial port.")

Key Points:

  1. Appending CR and LFb"ON\r\n" appends a carriage return (\r) and line feed (\n) to the string "ON".
  2. Bytes Object: The b before the string ensures that it is treated as a bytes object, which is required for pyserial.

Usage:

  1. Update the serial_port variable with your actual serial port.
  2. Run the script to send the string "ON" terminated with CR and LF to the serial port.

After providing these prompts and the results to the customer, he was able to modify the prompts to get exactly what he wanted.  It's great that AI can be used to easily extend the value of our products with little programming knowledge!