L
L
Leonid2018-11-09 14:30:38
Python
Leonid, 2018-11-09 14:30:38

Pyserial how to send command to switch?

How to send a command to the switch using the PySerial library?
executing code

import os
import sys
import time
import serial
import serial.tools.list_ports

print('Ищем порт...')
ports = serial.tools.list_ports.comports(include_links=False)
for port in ports :
    print('Нашли порт '+ port.device)

ser = serial.Serial(port.device)
if ser.isOpen():
    ser.close()

ser = serial.Serial(port.device, 9600, timeout=1)
ser.flushInput()
ser.flushOutput()
print('Цепляемся к порту ' + ser.name)

ser.write("sh sw \r\n".encode('utf-8')) #Asks the Lock-in for x-value

while True:
          response = ser.readline()
          print(response)

I get the answer:
5be5702463c90047652556.png
How to remove extra characters? And stop requests but don't close the port

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NaName, 2018-11-09
@leobatura

print(response.decode()) # maybe add .strip('\r\r\n')
break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question