From 18572ba5946d4499d9fc75c5159a8cf97b2390eb Mon Sep 17 00:00:00 2001 From: erki1993 Date: Wed, 11 Jan 2017 12:14:09 +0200 Subject: [PATCH 1/3] write_hex_file: new parameter byte_count, can choose hex data field length --- intelhex/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/intelhex/__init__.py b/intelhex/__init__.py index 301c568..f89cc49 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -544,7 +544,7 @@ def _get_eol_textfile(eolstyle, platform): raise ValueError("wrong eolstyle %s" % repr(eolstyle)) _get_eol_textfile = staticmethod(_get_eol_textfile) - def write_hex_file(self, f, write_start_addr=True, eolstyle='native'): + def write_hex_file(self, f, write_start_addr=True, eolstyle='native', byte_count=16): """Write data to file f in HEX format. @param f filename or file-like object for writing @@ -555,6 +555,7 @@ def write_hex_file(self, f, write_start_addr=True, eolstyle='native'): @param eolstyle can be used to force CRLF line-endings for output file on different platforms. Supported eol styles: 'native', 'CRLF'. + @param byte_count number of bytes in the data field """ fwrite = getattr(f, "write", None) if fwrite: @@ -656,7 +657,7 @@ def write_hex_file(self, f, write_start_addr=True, eolstyle='native'): # produce one record low_addr = cur_addr & 0x0FFFF # chain_len off by 1 - chain_len = min(15, 65535-low_addr, maxaddr-cur_addr) + chain_len = min(byte_count - 1, 65535-low_addr, maxaddr-cur_addr) # search continuous chain stop_addr = cur_addr + chain_len From 4ee8b4b4bc58b2fe4b2a01837add237e3f5748b1 Mon Sep 17 00:00:00 2001 From: erki1993 Date: Thu, 12 Jan 2017 12:18:35 +0200 Subject: [PATCH 2/3] add byte_count check --- intelhex/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/intelhex/__init__.py b/intelhex/__init__.py index f89cc49..fff8fb8 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -557,6 +557,8 @@ def write_hex_file(self, f, write_start_addr=True, eolstyle='native', byte_count Supported eol styles: 'native', 'CRLF'. @param byte_count number of bytes in the data field """ + if byte_count > 255 or byte_count < 1: + raise ValueError("wrong byte_count " + str(byte_count)) fwrite = getattr(f, "write", None) if fwrite: fobj = f From 736705eb4f993edd5807bb6c2630faf442baf330 Mon Sep 17 00:00:00 2001 From: erki1993 Date: Thu, 12 Jan 2017 12:19:30 +0200 Subject: [PATCH 3/3] add unittests bad_byte_count, byte_count_1, byte_count_13, byte_count_255 --- intelhex/test.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/intelhex/test.py b/intelhex/test.py index f63d1be..95cc30f 100755 --- a/intelhex/test.py +++ b/intelhex/test.py @@ -1659,6 +1659,77 @@ def test_sripts_hexdiff_version(self): def test_sripts_hexmerge_version(self): self.versionChecker('%s scripts/hexmerge.py --version') + +class TestWriteHexFileByteCount(unittest.TestCase): + + def setUp(self): + self.f = StringIO(hex8) + + def tearDown(self): + self.f.close() + del self.f + + def test_write_hex_file_bad_byte_count(self): + ih = intelhex.IntelHex(self.f) + sio = StringIO() + with self.assertRaises(ValueError): + ih.write_hex_file(sio, byte_count=0) + with self.assertRaises(ValueError): + ih.write_hex_file(sio, byte_count=-1) + with self.assertRaises(ValueError): + ih.write_hex_file(sio, byte_count=256) + + def test_write_hex_file_byte_count_1(self): + ih = intelhex.IntelHex(self.f) + sio = StringIO() + ih.write_hex_file(sio, byte_count=1) + s = sio.getvalue() + # control written hex first line to check that byte count is 1 + sio.seek(0) + self.assertEqual(sio.readline(), ':0100000002FD\n', + "Written hex is not in byte count 1") + sio.close() + + fin = StringIO(s) + ih2 = intelhex.IntelHex(fin) + + self.assertEqual(ih.tobinstr(), ih2.tobinstr(), + "Written hex file does not equal with original") + + def test_write_hex_file_byte_count_13(self): + ih = intelhex.IntelHex(self.f) + sio = StringIO() + ih.write_hex_file(sio, byte_count=13) + s = sio.getvalue() + # control written hex first line to check that byte count is 13 + sio.seek(0) + self.assertEqual(sio.readline(), ':0D0000000205A2E576246AF8E6057622786E\n', + "Written hex is not in byte count 1") + sio.close() + + fin = StringIO(s) + ih2 = intelhex.IntelHex(fin) + + self.assertEqual(ih.tobinstr(), ih2.tobinstr(), + "Written hex file does not equal with original") + + def test_write_hex_file_byte_count_255(self): + ih = intelhex.IntelHex(self.f) + sio = StringIO() + ih.write_hex_file(sio, byte_count=255) + s = sio.getvalue() + # control written hex first line to check that byte count is 255 + sio.seek(0) + self.assertEqual(sio.readline(), ':FF0000000205A2E576246AF8E60576227867300702786AE475F0011204AD0204552000EB7F2ED2008018EF540F2490D43440D4FF30040BEF24BFB41A0050032461FFE57760021577057AE57A7002057930070D7867E475F0011204ADEF02049B02057B7403D2078003E4C207F5768B678A688969E4F577F579F57AE57760077F2012003E80F57578FFC201C200C202C203C205C206C20812000CFF700D3007057F0012004FAF7AAE7922B4255FC2D5C20412000CFF24D0B40A00501A75F00A787730D50508B6FF0106C6A426F620D5047002D20380D924CFB41A00EF5004C2E5D20402024FD20180C6D20080C0D20280BCD2D580BAD20580B47F2012003E20020774010E\n', + "Written hex is not in byte count 1") + sio.close() + + fin = StringIO(s) + ih2 = intelhex.IntelHex(fin) + + self.assertEqual(ih.tobinstr(), ih2.tobinstr(), + "Written hex file does not equal with original") + ## # MAIN if __name__ == '__main__':