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

Library and example2.py is not compatible with Python 3 #34

Open
jeffsf opened this issue Jan 13, 2019 · 5 comments
Open

Library and example2.py is not compatible with Python 3 #34

jeffsf opened this issue Jan 13, 2019 · 5 comments

Comments

@jeffsf
Copy link

jeffsf commented Jan 13, 2019

On a machine where Python 3 is now default and Python 2 is not installed (Debian and Ubuntu, for example), example2.py fails to run.

So far:

  • Python 2 print in example2.py, rather than print()
  • TypeError in "stuff"
Traceback (most recent call last):
  File "/home/jeff/devel/tsip/example2.py", line 58, in <module>
    gps_conn.write(tsip.Packet(0x35, 0b00110011, 0b00000011, 0b00000001, 0b00101001))
  File "/home/jeff/venv/tsip/lib/python3.5/site-packages/tsip/hlapi.py", line 178, in write
    super(GPS, self).write(frame(stuff(packet.pack())))
  File "/home/jeff/venv/tsip/lib/python3.5/site-packages/tsip/llapi.py", line 72, in stuff
    return packet.replace(CHR_DLE, CHR_DLE + CHR_DLE)
TypeError: a bytes-like object is required, not 'str'

apparently from config.py which declares those variables as strings.

Python 3.5.3

@jeffsf jeffsf changed the title example2.py is not compatible with Python 3 Library and example2.py is not compatible with Python 3 Jan 13, 2019
@mjuenema
Copy link
Owner

Oh yes, the never ending ASCII vs. bytes vs. unicode issue. Thanks for reporting this.

@GiriMahesh
Copy link

Hi @mjuenema,

Could you please let me know, whether this issue is fixed in latest version?

Thanks much.

@mjuenema
Copy link
Owner

Hi @GiriMahesh, I haven't done any work on python-TSIP for a very long time so there's no change. This is certainly not good but at the moment I simply don't have the time to work on this project.

@user-8472
Copy link

This is not a finished solution but for those just trying to get the library working under python3 these changes will get the code running.


diff --git a/tsip/config.py b/tsip/config.py
index b8dbd19..3d86507 100644
--- a/tsip/config.py
+++ b/tsip/config.py
@@ -14,6 +14,11 @@ ETX = 0x03
 CHR_DLE = chr(DLE)
 CHR_ETX = chr(ETX)

+# DLE and ETX as bytes
+bDLE = DLE.to_bytes(1, 'little')
+bETX = ETX.to_bytes(1, 'little')
+

 # Contants for setting bits
 #

diff --git a/tsip/llapi.py b/tsip/llapi.py
index f9bc7d8..3fb7be8 100644
--- a/tsip/llapi.py
+++ b/tsip/llapi.py
@@ -18,7 +18,8 @@ def is_framed(packet):

     """

-    return packet[0] == CHR_DLE and packet[-2] == CHR_DLE and packet[-1] == CHR_ETX
+    #print("is_framed packet[0]: {}".format(packet[0]))
+    return packet[0] == DLE and packet[-2] == DLE and packet[-1] == ETX


 def frame(data):
@@ -35,7 +36,7 @@ def frame(data):
     if is_framed(data):
         raise ValueError('data contains leading DLE and trailing DLE/ETX')
     else:
-        return CHR_DLE + data + CHR_DLE + CHR_ETX
+        return bDLE + data + bDLE + bETX


 def unframe(packet):
@@ -51,7 +52,8 @@ def unframe(packet):
     """

     if is_framed(packet):
-        return packet.lstrip(CHR_DLE).rstrip(CHR_ETX).rstrip(CHR_DLE)
+        #print("unframe lstrip: {}".format(packet.lstrip(bDLE)))
+        return packet.lstrip(bDLE).rstrip(bETX).rstrip(bDLE)
     else:
         raise ValueError('packet does not contain leading DLE and trailing DLE/ETX')

@@ -69,7 +71,7 @@ def stuff(packet):
     if is_framed(packet):
         raise ValueError('packet contains leading DLE and trailing DLE/ETX')
     else:
-        return packet.replace(CHR_DLE, CHR_DLE + CHR_DLE)
+        return packet.replace(bDLE, bDLE + bDLE)



@@ -87,7 +89,7 @@ def unstuff(packet):
     if is_framed(packet):
         raise ValueError('packet contains leading DLE and trailing DLE/ETX')
     else:
-        return packet.replace(CHR_DLE + CHR_DLE, CHR_DLE)
+        return packet.replace(bDLE + bDLE, bDLE)

 class gps(object):
@@ -100,21 +102,23 @@ class gps(object):

     def read(self):

-        packet = ''
+        packet = bytes()
         dle_count = 0

         last_b = None
         while True:
             b = self.conn.read(1)
+            #print("type b {}".format(type(b)))

             if len(b) == 0:
                 return None

-            packet += chr(ord(b))    # Python 3 work-around
+            packet += b

-            if b == CHR_DLE:
+            if b[0] == DLE:
                 dle_count += 1
-            elif b == CHR_ETX and last_b == CHR_DLE and (dle_count % 2) == 0:    # even, because leading DLE is counted!
+            elif b[0] == ETX and last_b[0] == DLE and (dle_count % 2) == 0:    # even, because leading DLE is counted!
+                #print("gps.read(): {}".format(packet))
                 return packet
             else:
                 pass

@mjuenema
Copy link
Owner

mjuenema commented Mar 2, 2021

@user-8472 Thanks for the code. As you can see I haven't worked on this project for years. I can even remember where I put my Trimble GPS receivers in the house.

Would you be able to submit a Pull Request for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants