pyserial输入缓冲区
我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我愚蠢的错误。在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.