Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crc error because calc_crc16 sometime return zero #1

Open
wants to merge 4 commits into
base: multi-protocol
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from modem import *
11 changes: 6 additions & 5 deletions modem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
__version__ = '0.2.4'

import gettext
from modem.protocol.xmodem import XMODEM
from modem.protocol.xmodem1k import XMODEM1K
from modem.protocol.xmodemcrc import XMODEMCRC
from modem.protocol.ymodem import YMODEM
from modem.protocol.zmodem import ZMODEM

from .protocol.xmodem import XMODEM
from .protocol.xmodem1k import XMODEM1K
from .protocol.xmodemcrc import XMODEMCRC
from .protocol.ymodem import YMODEM
from .protocol.zmodem import ZMODEM

gettext.install('modem')

Expand Down
4 changes: 2 additions & 2 deletions modem/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from modem.tools import crc16, crc32
from .tools import crc16, crc32


class Modem(object):
Expand All @@ -21,7 +21,7 @@ def calc_checksum(self, data, checksum=0):
'0x3c'

'''
return (sum(map(ord, data)) + checksum) % 256
return (sum(data) + checksum) % 256

def calc_crc16(self, data, crc=0):
'''
Expand Down
18 changes: 10 additions & 8 deletions modem/protocol/xmodem.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import time
from modem import error
from modem.base import Modem
from modem import const
from modem.tools import log

from .. import const
from .. import error
from ..base import Modem
from ..tools import log


class XMODEM(Modem):
Expand Down Expand Up @@ -216,9 +217,8 @@ def _send_stream(self, stream, crc_mode,
data = data.ljust(packet_size, b'\x00')

# Calculate CRC or checksum
crc = crc_mode and self.calc_crc16(data) or \
self.calc_checksum(data)

#calc_crc16 sometime return zero
crc = crc_mode and self.calc_crc16(data) #or self.calc_checksum(data)
# SENDS PACKET WITH CRC
if not self._send_packet(
sequence, data, packet_size, crc_mode,
Expand Down Expand Up @@ -276,14 +276,16 @@ def _send_packet(self, sequence, data, packet_size, crc_mode, crc,

if byte in [b'', const.NAK]:
error_count += 1
log.error(error_count)
if error_count >= retry:
# Excessive amounts of retransmissions requested
self.error(error.ABORT_ERROR_LIMIT)
log.error(error.ABORT_ERROR_LIMIT)
self.abort(timeout=timeout)
return False
continue

# Protocol error
log.error(byte)
log.error(error.ERROR_PROTOCOL)
error_count += 1
if error_count >= retry:
Expand Down
8 changes: 4 additions & 4 deletions modem/protocol/xmodem1k.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import time

from modem import error
from modem import const
from modem.tools import log
from modem.protocol.xmodem import XMODEM
from .xmodem import XMODEM
from .. import const
from .. import error
from ..tools import log


class XMODEM1K(XMODEM):
Expand Down
8 changes: 4 additions & 4 deletions modem/protocol/xmodemcrc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import time

from modem import error
from modem import const
from modem.tools import log
from modem.protocol.xmodem import XMODEM
from .xmodem import XMODEM
from .. import const
from .. import error
from ..tools import log


class XMODEMCRC(XMODEM):
Expand Down
48 changes: 25 additions & 23 deletions modem/protocol/ymodem.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import glob
import os
import time
from modem import const
from modem.tools import log
from modem.protocol.xmodem import XMODEM
from modem import error

from .xmodem import XMODEM
from .. import const
from .. import error
from ..tools import log


class YMODEM(XMODEM):
Expand All @@ -15,7 +16,7 @@ class YMODEM(XMODEM):

protocol = const.PROTOCOL_YMODEM

def send(self, pattern, retry=3, timeout=60):
def send(self, pattern, retry=3, timeout=10):
'''
Send one or more files via the YMODEM protocol.

Expand Down Expand Up @@ -94,26 +95,27 @@ def send(self, pattern, retry=3, timeout=60):

filedesc.close()
# WAIT A <CRC> BEFORE NEXT FILE
error_count = 0
if not self._wait_recv(error_count, timeout):
log.error(error.ABORT_INIT_NEXT)
# Already aborted
return False
if len(filenames) > 1:
error_count = 0
if not self._wait_recv(error_count, timeout):
log.error(error.ABORT_INIT_NEXT)
# Already aborted
return False

# End of batch transmission, send NULL file name
sequence = 0
error_count = 0
packet_size = 128
data = b'\x00' * packet_size
crc = self.calc_crc16(data) if crc_mode else self.calc_checksum(data)

# Emit packet
if not self._send_packet(
sequence, data, packet_size, crc_mode, crc,
error_count, retry, timeout):
log.error(error.ABORT_SEND_PACKET)
# Already aborted
return False
# sequence = 0
# error_count = 0
# packet_size = 128
# data = b'\x00' * packet_size
# crc = self.calc_crc16(data) if crc_mode else self.calc_checksum(data)
#
# # Emit packet
# if not self._send_packet(
# sequence, data, packet_size, crc_mode, crc,
# error_count, retry, timeout):
# log.error(error.ABORT_SEND_PACKET)
# # Already aborted
# return False

# All went fine
return True
Expand Down
7 changes: 4 additions & 3 deletions modem/protocol/zmodem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
import os
import time
from modem.base import Modem
from modem import const
from modem.tools import log

from .. import const
from ..base import Modem
from ..tools import log


class ZMODEM(Modem):
Expand Down
6 changes: 3 additions & 3 deletions modem/tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import struct
import logging
from zlib import crc32 as _crc32
from modem.const import CRC16_MAP
import struct
from collections.abc import Iterable
from zlib import crc32 as _crc32

from .const import CRC16_MAP

# Configure logging
logging.basicConfig(
Expand Down