Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help using Pyserial
#5
Your program sends "hello" out the serial port. Why? I didn't see anything about having to send a program name to the telescope.

I thought readline() returned zero bytes when it times out. Looking at Pyserial documentation it says readline() is provided by io.IOBase.readline() and when I look there it says nothing about what readline does when it times out. BUT I did notice that read_until() behaves as you described. This would make it a good choice for reading the response.
import serial

NAK = b"\x15"
EOL = "#".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)]
    return response.decode()


ser = serial.Serial("COM3", 9600, timeout=1.0)
sendCommand(ser, "GR")
print(readResponse(ser))
NAK is not zero bytes. NAK is b'\15'.

Writing extra "#" is just going to cause confusion. Your telescope is going to say "I see #, where was the command?" and then it is probably going to return a NAK which you will read thinking it is the response to the command you are trying to send instead of a response to the leading "#".

As I said in my post, I do not have an ETX telescope. I guess to be clear I should say I don't have a Meade telescope and have no way to test the code I wrote. I would be surprised if it works.
Jeff_t likes this post
Reply


Messages In This Thread
Need help using Pyserial - by barryjo - Dec-29-2021, 12:31 AM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 02:06 AM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 03:05 AM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 03:43 AM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 03:49 AM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 03:54 AM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 04:03 AM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 04:23 AM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 04:57 PM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 05:03 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 05:08 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 05:12 PM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 05:24 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 05:29 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 08:22 PM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 09:02 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 09:37 PM
RE: Need help using Pyserial - by deanhystad - Dec-29-2021, 09:59 PM
RE: Need help using Pyserial - by barryjo - Dec-29-2021, 10:45 PM
RE: Need help using Pyserial - by Jeff_t - Dec-30-2021, 12:33 AM
RE: Need help using Pyserial - by barryjo - Dec-30-2021, 02:49 AM
RE: Need help using Pyserial - by deanhystad - Dec-30-2021, 02:59 AM
RE: Need help using Pyserial - by barryjo - Dec-30-2021, 03:00 PM

Forum Jump:

User Panel Messages

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