我正在尝试自动化串行连接的登录。现在我正在使用PySerial进行婴儿步骤,解码的输出并不恒定。网络是这样的:
PC (Windows 10) -> Switch -> Router -> Switch -> Raspberry Pi 4 -> Router
我通过SSH连接到Raspberry Pi 4,然后从那里用USB到RS-232适配器连接到路由器(Lancom-1926 VAG-5G)。
串行连接的工作原理我已经尝试了PuTTY,屏幕和Minicom.波特率是模糊的,但可以从9600到115200之间.现在这是我的串行连接的测试代码:
#!/usr/bin/env python3
import serial
import time
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=57600, parity="N", stopbits=1, bytesize=8, timeout=3, rtscts=True)
ser.reset_input_buffer()
ser.reset_output_buffer()
print(ser.isOpen())
i = 0
while i<=15:
time.sleep(0.1)
ser.write(''.encode('utf-8') + b'\n')
i = i+1
print(ser.inWaiting())
input_data = ser.read(1024).decode('utf-8', errors='replace').strip()
print(input_data)
与PuTTY的正常连接如下所示:
b▒▒▒ĥ▒▒$▒▒d▒
Outband-115200 Bit/s OK
#
| LANCOM 1926VAG-5G
| Ver. 10.72.0092SU2 / 15.02.2023
| SN.
| Copyright (c) LANCOM Systems
Connection No.: 001 (Outband-115200 Bps)
Password:
Login Error
Goodbye
但是当我尝试代码时,它看起来像这样:
True
391
G�ɰhj��hL@Е��
Outband-57600 Bit/s OKr~Iy�b܁�nĒb���
� b�߿�<�lb����
�����p���������������p�
����
�CBRTb���Ą��$��d�G�ɰ)j��hL@Е��
Outband-57600 Bit/s OK~�y�b܁�nĒb���
� b�߿�<�$b���������p���������������p�
����
�CBRT���Ą��$��d�G�ɰ(j��hL@Е��
Outband-57600 Bit/s OKr~Yy�b܁�nĒb�
��
� b�O���lb�
�����
���p������������`��p�����
同样,但这次使用与PuTTY输出相同的波特率:
True
668
Outband-115200 Bit/s OKr~�y�b܁�nĒb��
�
� b�߿�<�lb怘���f?`?<<��`<�f~x��3���?�������������p��������������p������������������������������������������b���ĥ��$��d�
Outband-115200 Bit/s OKr~�y�b܁�nĒb��
�
� b�߿�<�lb怘���f?`?<<��`<�f~x��3���?�������������p��������������p������������������������������������������b���ĥ��$��d�
Outband-115200 Bit/s OKr~�y�b܁�nĒb��
�
� b�߿�<�lb怘���f?`?<<��`<�f~x��3���?�������������p��������������p������������������������������������������
为什么消息“Outband-57600 Bit/s OK”被解码,但之后什么都没有?(编辑:路由器不断与代码协商波特率)编辑:我注意到,当我尝试这个代码的思科交换机时,他仍然是启动我可以读到这:(9600波特率)
True
225
System Bootstrap, Version 15.0(1r)M12, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 2011 by cisco Systems, Inc.
Total memory size = 512 MB - On-board = 512 MB, DIMM0 = 0 MB
CISCO2901/K9 platform with 524288 Kbytes of main memory
Main memory is configured to 72/-1(On-board/DIMM0) bit mode with ECC enabled
Readonly ROMMON initialized
program load complete, entry point: 0x80803000, size: 0x1b340
program load complete, entry point: 0x80803000, size: 0x1b340
但是MOTD无法读取登录提示符,在他完成 Boot 后。我尝试了同样的事情,就像Cisco交换机一样,使用Lancom路由器:
True
269
bC���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
(我离开了一些行,因为它只是同样的胡言乱语。)从Lancom路由器的输出是ZLoader引导固件。通常它看起来像这样:
OK: Action Boot-System started
System is going down ...
@W@
Found primary and mirror bbt
#B#
Start firmware #2
ZLoader running.....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Outband-115200 Bit/s OK
1条答案
按热度按时间vh0rcniy1#
在我将LANtrace应用于带外连接后。我发现PySerial不传输8位,停止位1,因此协商总是失败,因为路由器总是只期望一个字节。在使用PuTTY跟踪连接后,我看到了两者之间的连接。在我更改代码以发送仅1个字母,延迟0.1秒后,协商成功。
这不是最好的答案,但至少现在有效。