我可以在 PySerial 中使用 xmodem 协议吗? [英] Can I use the xmodem protocol with PySerial?

查看:49
本文介绍了我可以在 PySerial 中使用 xmodem 协议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 PySerial 与我的串行设备建立了工作连接,但我也想通过 xmodem 协议传输文件作为我程序的一部分.

I have a working connection with my serial device via PySerial, but I also want to transfer files via the xmodem protocol as part of my program.

哪种方式是最平台中立的方式来做到这一点?最坏的情况,我可以在 Python 中 close() 我的 serial.Serial 对象并使用 subprocess 调用 /usr/bin/sb,但这似乎不雅.

Which would be the most platform-neutral way to do this? Worst case, I could close() my serial.Serial object in Python and use subprocess to call upon /usr/bin/sb, but that seems inelegant.

我目前使用的是 Ubuntu 9.10 并且正在使用 USB-TTY 适配器.

I'm currently on Ubuntu 9.10 and am using a USB-TTY adapter.

有什么想法吗?

推荐答案

PyPi 上有 xmodem 模块.它在构造函数中需要两个函数来读取和写入数据,实现它们以与您打开的串行端口一起工作.以下是其用法的简单示例:

There is xmodem module on PyPi. It takes two functions in constructor for reading and writing data, implement them to work with your opened serial port. Below is simple sample of its usage:

import serial
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
from xmodem import XMODEM, CRC
from time import sleep

def readUntil(char = None):
    def serialPortReader():
        while True:
            tmp = port.read(1)
            if not tmp or (char and char == tmp):
                break
            yield tmp
    return ''.join(serialPortReader())

def getc(size, timeout=1):
    return port.read(size)

def putc(data, timeout=1):
    port.write(data)
    sleep(0.001) # give device time to send ACK


port = serial.Serial(port='COM5',parity=serial.PARITY_NONE,bytesize=serial.EIGHTBITS,stopbits=serial.STOPBITS_ONE,timeout=0,xonxoff=0,rtscts=0,dsrdtr=0,baudrate=115200)
port.write("command that loads data via xmodem\r\n")
sleep(0.02) # give device time to handle command
readUntil(CRC)
buffer = StringIO('data to send')
XMODEM(getc, putc).send(buffer, quiet = 1)
buffer.close()
readUntil()

这篇关于我可以在 PySerial 中使用 xmodem 协议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆