J'écris un programme qui nécessite l'utilisation de XMODEM pour transférer des données à partir d'un périphérique de détection. Je voudrais éviter d'avoir à écrire mon propre code XMODEM, donc je me demandais si quelqu'un savait s'il y avait un module XMODEM python disponible n'importe où?XMODEM pour python
Répondre
Je pense que vous êtes coincé avec rouler le vôtre.
Vous pourriez utiliser sz, qui implémente X/Y/ZMODEM. Vous pourriez appeler au binaire, ou porter le code nécessaire à Python.
Vous pouvez essayer d'utiliser SWIG pour créer des liaisons Python pour les bibliothèques C liées ci-dessus (ou toute autre bibliothèque C/C++ que vous trouverez en ligne). Cela vous permettra d'utiliser la même API C directement à partir de Python.
L'implémentation réelle sera bien sûr toujours en C/C++, puisque SWIG crée simplement des liaisons aux fonctions d'intérêt.
def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
if serial.read(1) != NAK:
t = t + 1
print anim[t%len(anim)],'\r',
if t == 60 : return False
else:
break
p = 1
s = file.read(128)
while s:
s = s + '\xFF'*(128 - len(s))
chk = 0
for c in s:
chk+=ord(c)
while 1:
serial.write(SOH)
serial.write(chr(p))
serial.write(chr(255 - p))
serial.write(s)
serial.write(chr(chk%256))
serial.flush()
answer = serial.read(1)
if answer == NAK: continue
if answer == ACK: break
return False
s = file.read(128)
p = (p + 1)%256
print '.',
serial.write(EOT)
return True
Il existe un module XMODEM sur PyPi. Il gère à la fois l'envoi et la réception de données avec XModem. Ci-dessous l'échantillon de son utilisation:
import serial
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
from xmodem import XMODEM, NAK
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 prepare new buffer and start sending it
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 initiates xmodem send from device\r\n")
sleep(0.02) # give device time to handle command and start sending response
readUntil(NAK)
buffer = StringIO()
XMODEM(getc, putc).recv(buffer, crc_mode = 0, quiet = 1)
contents = buffer.getvalue()
buffer.close()
readUntil()
Il y a un module python que vous pouvez utiliser ->https://pypi.python.org/pypi/xmodem
Vous pouvez voir le protocole de transfert dans http://pythonhosted.org//xmodem/xmodem.html
- 1. Protocole xmodem en Java
- 2. Problème de répétition du bloc avec l'implémentation Java XMODEM
- 3. Browscap Pour Python
- 4. Module Python pour VBox?
- 5. Capture générique pour python
- 6. Lecteur iCalendar pour Python?
- 7. Directory Walker pour Python
- 8. back-end pour python
- 9. Compile pyme pour Python 2.6
- 10. python Littéral invalide pour float
- 11. Mule vs ActiveMQ pour Python
- 12. Ressources pour passer à Python
- 13. python expression régulière pour retweets
- 14. Python "étendre" pour un dictionnaire
- 15. Equivalent pour LinkedHashMap en Python
- 16. Regex pour l'extraction en Python
- 17. Liaisons SVN Python pour Windows
- 18. Test pour ImportErrors en Python
- 19. Délai d'attente Ruby pour Python?
- 20. Interpréteur Javascript pour remplacer Python
- 21. Comment configurer Python pour .NET avec Python 2.6.1?
- 22. Python Pypi: quel est votre processus pour libérer des paquets pour différentes versions de Python? (Linux)
- 23. Comment installer simplejson Package pour Python
- 24. extensions de débogage rasade pour Python
- 25. Bibliothèques OCSP pour python/java/c?
- 26. Comment construire sqlite pour Python 2.4?
- 27. Python ou Ruby pour un développeur .NET?
- 28. Une base de données pour python 3?
- 29. mondiale pour l'application hotkey Python dans Gnome
- 30. L'équivalent os.execvp de Python pour PHP