Я пытаюсь подключиться к устройству с помощью последовательного порта. Я попробовал два способа - 1) писсериальный в Python и 2) замазкой. Мне удалось подключиться к устройству и использовать его, используя замазку. Однако в Python я могу подключиться только к устройству (открыть порт), но я не могу отправлять команды или читать порт. Мой сценарий Python (ниже) и настройки замазки. Но, возможно, я что -то упускаю. Как исправить свой скрипт?import serial
import time
def send_command_to_com_port(ser, command):
try:
# Send the command
command_encoded = command.encode('ascii')
ser.write(command_encoded)
print(command_encoded)
except serial.SerialException as e:
print(f"Error communicating with the COM port: {e}")
def read_from_com_port(ser):
try:
# Read response from the COM port
response = ser.read_until("\n") # Read until a newline character is encountered
if response:
print(f"Response received: {response.decode('ascii').strip()}")
else:
print("No response received from the COM port.")
except serial.SerialException as e:
print(f"Error communicating with the COM port: {e}")
# Specify the COM port and send the command
com_port = "COM4"
# Configure the serial connection
ser = serial.Serial(
port=com_port, # Replace with your COM port (e.g., 'COM3' on Windows or '/dev/ttyUSB0' on Linux)
baudrate=115200, # Baud rate
bytesize=serial.EIGHTBITS, # 8 data bits
parity=serial.PARITY_NONE, # No parity bit
stopbits=serial.STOPBITS_ONE, # One stop bit
timeout=1 # Timeout in seconds
)
# Ensure the COM port is open
print(f"Serial port opened: {ser.is_open}")
# Send commands to the COM port
send_command_to_com_port(ser, 'm0\n')
time.sleep(2)
read_from_com_port(ser)
# Close the serial port
ser.close()
print("Serial connection closed.")
< /code>
Вывод, который я получаю, всегда: < /p>
Serial port opened: True
b'm0\\n'
No response received from the COM port.
Serial connection closed.
Process finished with exit code 0
< /code>
Дополнительное примечание: все инструкции для устройства должны быть закодированы ASCII и должны заканчиваться новым символом '\ n'. < /p>
После отправки команды «M0». Я должен получить подтверждение в качестве строки. Но я ничего не получаю.
Подробнее здесь: https://stackoverflow.com/questions/794 ... g-pyserial
Не удалось подключиться к последовательному порту с помощью Pyserial ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Доступ к последовательному COM-порту Windows через контейнер докеров Linux
Anonymous » » в форуме Linux - 0 Ответы
- 18 Просмотры
-
Последнее сообщение Anonymous
-