我需要调试两个嵌入式微 Controller 之间的串行协议(protocol)。
它们之间有一条与我的 FT2232h 迷你模块兼容的串行线。
情况与this question中描述的非常相似除了:
- 线路以 3.3V(而非 5V)驱动。
- 开发站正在运行 Linux(而非 Windows)。
- 我的时间要求较低(或多或少我只关心实际序列和非常大的延迟)。
- 波特率为“标准”115200。
我可以编写自己的监控程序,但我很惊讶我的搜索没有给出结果。
---delete---有人可以建议哪个(可能是免费的)项目可以帮助我吗?---
更新: 我没有找到合适的东西,所以我开始用 python 编写一个简单的监视器(下面的当前源代码)。这或多或少有效,但它似乎仍然缓冲字符,以便像这样的序列:
- rqst1 -->
- <-- ack1
- rqst2 -->
- <-- ack2
可能最终会写成:
我不知道缓冲是在 FT2232H、USB、Linux 驱动程序还是 Python 级别(可能在多个级别!)完成的。
有什么办法可以在下面的代码中强制“无缓冲”吗? (在我开始用纯 C 重写整个废话以提高速度之前)。
#!/usr/bin/python3
import serial, time, sys, threading
from colorama import Fore, Style, init as colorama_init
from binascii import hexlify
colorama_init()
# lock to serialize console output
lock = threading.Lock()
class Highlight:
def __init__(self, clazz, color):
self.color = color
self.clazz = clazz
def __enter__(self):
print(self.color, end="")
def __exit__(self, type, value, traceback):
if self.clazz == Fore:
print(Fore.RESET, end="")
else:
assert self.clazz == Style
print(Style.RESET_ALL, end="")
sys.stdout.flush()
if len(sys.argv) != 3 and len(sys.argv) != 4:
sys.stderr.write("Usage: %s <baud> <port1> [<port2>]\n" % (sys.argv[0]))
exit(1)
def open_serial(port, baud):
ser = serial.Serial()
ser.port = port
ser.baudrate = baud
ser.bytesize = serial.EIGHTBITS # number of bits per bytes
ser.parity = serial.PARITY_NONE # set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE # number of stop bits
# ser.timeout = None #block read
ser.timeout = 0 # non blocking read
ser.xonxoff = False # disable software flow control
ser.rtscts = False # disable hardware (RTS/CTS) flow control
ser.dsrdtr = False # disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 # timeout for write
try:
ser.open()
except Exception as e:
print("error open serial port: " + str(e))
exit()
return ser
def sprint(c, color):
if len(c) > 0:
print(color)
h = hexlify(c)
h += b' ' * (120 - len(h))
s = bytes(x if 31 < x < 128 else 46 for x in c)
with lock:
sys.stdout.buffer.write(h)
sys.stdout.buffer.write(s)
sys.stdout.flush()
s1 = open_serial(sys.argv[2], sys.argv[1])
s2 = open_serial(sys.argv[3], sys.argv[1])
running = True
try:
while running:
n = s1.in_waiting
if n > 0:
c = s1.read(n)
sprint(c, Fore.CYAN)
n = s2.in_waiting
if n > 0:
c = s2.read(n)
sprint(c, Fore.GREEN)
except Exception as e1:
print("error communicating...: " + str(e1))
s1.close()
s2.close()
except KeyboardInterrupt:
exit()
最佳答案
使用 strace,一个跟踪系统调用和信号的系统实用程序:
how should I use strace to snif the serial port?
或:
我发现后者更易于使用,因为您只需要发出如下命令:
jpnevulator --ascii --timing-print --tty /dev/ttyS0:SB115200d
调试您的端口。
如果您需要实现更复杂的调试工具,您可以开始在 github 上 fork 其源代码:
关于python - FT2232h/FT42232h 兼容全双工串行监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57163689/