Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help using Pyserial
#21
Well, here is another hickup. On the line that has the decode in it, I added decode(errors='replace') I read that this should replace the error with a question mark ?. However, it replaces the error with a ? inside a small box, a strange looking question mark. My plan was to replace the bad character, \xdf with a ? then use the find method to locate the ? and replace it using the replace method to a normal with a norrmal *. However, the find method does not find the ?in a box????

Tomorrow, I will try to merely find the " \xdf " and replace this small string with a *. However, as I recall the \ is an escape character so I guess I will have to look for something like \\xdf instead.

When I get this working, with your help of course, I will be a Python expert.

OH, from your previous Quote I think that this person said that a #: should proceed the command followed by a single #. If I read the quote correctly.
I have not encountered the degree symbol as yet but I am sure I will.

I emailed Meade about the bad * in the command responses. It will be interesting as to what they tell me, if anything.
Reply
#22
The replacement character is not a question mark, it is the REPLACEMENT CHARACTER (unicode U+FFFD). From the Python docs.

https://docs.python.org/3/howto/unicode.html

So they designed the serial protocol to make it easier to make the handset. Funny that they are using 223 instead of 248 which is actually the degree symbol in the extended ASCII character set.

I think the best way around the problem is to replace \xdf with * and then decode.
import serial
 
NAK = b"\x15"
EOL = "#".encode()
DEG = b'\xdf'
DEG_REPLACEMENT = "*".encode()
 
def sendCommand(ser, cmdStr):
    """Send command to telescope.  Prepend : and append #"""
    ser.write(f":{cmdStr}#".encode())
 
def readResponse(ser):
    """Read response.  Strip trailing #"""
    response = ser.read_until(EOL)
    if len(response) == 0:
        return None
    elif response[0] == NAK:
        return NAK
    if EOL in response:
        response =  response[:response.index(EOL)]
    response = response.replace(DEG, DEG_REPLACEMENT)
    return response.decode()
 
 
ser = serial.Serial("COM3", 9600, timeout=1.0)
sendCommand(ser, "GR")
print(readResponse(ser))
Or you code decode and the replace the REPLACEMENT CHARACTER with a degree symbol.
Reply
#23
Well, you saved me again. Looks like you code works perfect. Thanks for the code and the pointers to the documentation.

What is the best way to save this forum thread so I can get back to it in the future?

Do you use tkinter? Are you going to be my savior on my tkiinter project?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020