-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_serial.py
59 lines (45 loc) · 1.68 KB
/
ps_serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
import serial
import readline
def main(port):
# Configure the serial port
serial_port = serial.Serial()
serial_port.port = port
serial_port.baudrate = 9600
serial_port.parity = serial.PARITY_NONE
serial_port.bytesize = serial.EIGHTBITS
serial_port.stopbits = serial.STOPBITS_ONE
serial_port.xonxoff = True
serial_port.timeout = 0.1 # Set the timeout to 0.1 second
# Initialize an empty list to store the command history
history = []
try:
# Open the COM port
serial_port.open()
while True:
# Read the command from keyboard input with history support
tx_message = input("Enter a command (or 'quit' to exit): ")
if tx_message.lower() == 'quit':
break
# Add the command to the history
history.append(tx_message)
# Send the command to the power supply
serial_port.write(tx_message.encode() + b"\n")
# Get the response
rx_message = serial_port.readline().decode().strip()
print("Response:", rx_message)
except serial.SerialException as e:
print("Error:", e)
finally:
# Close the COM port
serial_port.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide the serial port as a command-line argument.")
print("Usage: python script_name.py <serial_port>")
sys.exit(1)
port = sys.argv[1]
# Enable command history and tab completion
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode vi')
main(port)