pyserial输入缓冲区

发布于 2024-10-21 00:06:05 字数 723 浏览 2 评论 0原文

我通过 USB 模拟串行线 (FT2232HL) 与我的硬件进行通信。我需要使用 pySerial 和 Python 成像库向其发送位图。这是代码:


#!/usr/bin/python2

from PIL import Image
import serial
import string

img = Image.open("db/bitmap.bmp")
img = img.convert("L")
img = img.tostring()

print "img length: " + `len(img)`

device = serial.Serial("/dev/ttyUSB1", 115200, timeout=30)
device.write(size)
device.write(img) #the bitmap has about 40kB
print "image written"

问题是它不起作用。我设置了一个环回,用cutecom捕获它,但似乎只传输了大约30kB(cutecom日志的大小)。我还尝试了另一个硬件(带 F5U103v 的贝尔金 USB 串行转换器),结果相同。我想它可能是一些 io 缓冲区,但我找不到任何有关它的信息。

编辑:第一个cutecom日志(包含从环回接收的数据)的大小是32725字节(正好发送42126字节)。当我运行脚本两次而不截断日志文件时,它的大小为 81838 字节。我还检查了 device.write() 调用的返回值,它恰好是 42126。

I communicate with my HW over USB emulated serial line (FT2232HL). I need to send a bitmap to it, using pySerial and Python Imaging Library. Here is the code:


#!/usr/bin/python2

from PIL import Image
import serial
import string

img = Image.open("db/bitmap.bmp")
img = img.convert("L")
img = img.tostring()

print "img length: " + `len(img)`

device = serial.Serial("/dev/ttyUSB1", 115200, timeout=30)
device.write(size)
device.write(img) #the bitmap has about 40kB
print "image written"

The problem is it doesn't work. I set up a loopback, capturing it with cutecom, but it seems that only about 30kB is transmitted (size of cutecom log). I also tried another hardware (Belkin usb-serial converter with F5U103v) with the same result. I suppose it can be some io buffer, but I couldn't find any info about it.

EDIT: the size of the first cutecom log (which contains data received from loopback) is 32725 bytes (sending exactly 42126 bytes). When I run the script twice without truncating the log file, it's size is 81838 bytes. I also checked return value of the device.write() call, it's exactly 42126.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-10-28 00:06:05

这是我愚蠢的错误。在cutecom中,我没有关闭日志文件,因此丢失的数据位于write()缓冲区中。由于另一个错误,设备无法工作。

It was my stupid fault. In cutecom I didn't close the log file, so the missing data were in the write() buffer. Device didn't work because of another bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文