【问题标题】:FT2232h/FT42232h compatible full-duplex serial monitorFT2232h/FT42232h 兼容全双工串行监视器
【发布时间】:2019-07-23 11:54:52
【问题描述】:

我需要调试两个嵌入式微控制器之间的串行协议。

在它们之间有一条与我的 FT2232h 迷你模块兼容的串行线。

情况与this question 中描述的非常相似,除了:

  • 线路驱动电压为 3.3V(不是 5V)。
  • 开发站正在运行 Linux(不是 Windows)。
  • 我的时序要求较低(或多或少我只关心实际序列和非常大的延迟)。
  • 波特率为“标准”115200。

可以编写自己的监控程序,但我很惊讶我的搜索没有给出结果。

---删除---有人可以建议哪个(可能是免费的)项目可以帮助我吗?---

更新: 我没有找到任何合适的东西,所以我开始用 python 编写一个简单的监视器(当前源代码如下)。这或多或少的作品,但它似乎仍然缓冲字符,以便像这样的序列:

  • rqst1 -->
  • rqst2 -->

可能最终写成:

我不知道缓冲是在 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()

【问题讨论】:

    标签: python linux serial-port monitor


    【解决方案1】:

    使用 strace,一个系统实用程序来跟踪系统调用和信号:

    how should I use strace to snif the serial port?

    或:

    jpnevulator

    我发现后者更易于使用,因为您只需要发出如下命令:

    jpnevulator --ascii --timing-print --tty /dev/ttyS0:SB115200d
    

    调试您的端口。

    如果您需要实现更复杂的调试工具,您可以开始在 github 上 fork 其源代码:

    snarlistic/jpnevulator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多