Python RS232 COM4 API SCRIPT

Hi
I want to ask a question
The issue is

My rotation method is RS232 USB CABLE

Want to use SCRIPT in PYTHON
Go call rs232

Like

import serial
ser = serial.Serial (3) # open COM4 port
print ser.name # check which port was really used
ser.write (“XX”) # write a string

And save as .PY

When the execution reaches XX, an error will be displayed. What can I do?

Or use API to call PYTHON?