|
| 1 | +""" |
| 2 | +ASCOM interface for AutoSTAR_remote |
| 3 | +""" |
| 4 | + |
| 5 | +from PyQt5 import QtWidgets |
| 6 | +import win32com.client |
| 7 | + |
| 8 | + |
| 9 | +class ASCOM: |
| 10 | + |
| 11 | + def __init__(self, showDebugMessages=False): |
| 12 | + self.Telescope = None |
| 13 | + self.Name = "" |
| 14 | + # |
| 15 | + self.showDebugMessages = showDebugMessages |
| 16 | + |
| 17 | + def dbgMsg(self, msg): |
| 18 | + if self.showDebugMessages: |
| 19 | + print(f'DEBUG: {msg}') |
| 20 | + |
| 21 | + def open(self): |
| 22 | + self.close() |
| 23 | + try: |
| 24 | + Chooser = win32com.client.Dispatch("ASCOM.Utilities.Chooser") |
| 25 | + except win32com.client.pywintypes.com_error: |
| 26 | + QtWidgets.QMessageBox.critical(None, "Can not call ASCOM!", |
| 27 | + f"Is ASCOM installed?") |
| 28 | + else: |
| 29 | + Chooser.DeviceType = 'Telescope' |
| 30 | + self.Name = Chooser.Choose(None) |
| 31 | + if self.Name != "": |
| 32 | + self.Telescope = win32com.client.Dispatch(self.Name) |
| 33 | + self.Telescope.Connected = True |
| 34 | + if not self.Telescope.Connected: |
| 35 | + QtWidgets.QMessageBox.critical(None, "Can not connect to telescope!", |
| 36 | + f"Please check connection to\n{self.Name}.\nMaybe it is already in use.") |
| 37 | + self.Telescope = None |
| 38 | + |
| 39 | + def get_Parameter(self): |
| 40 | + # has no parameter |
| 41 | + return dict() |
| 42 | + |
| 43 | + def is_open(self): |
| 44 | + if self.Telescope is not None: |
| 45 | + if self.Telescope.Connected: |
| 46 | + return True |
| 47 | + return False |
| 48 | + |
| 49 | + def close(self): |
| 50 | + if self.is_open(): |
| 51 | + self.Telescope.Connected = False |
| 52 | + self.Telescope = None |
| 53 | + self.Name = "" |
| 54 | + |
| 55 | + def sendCommandBlind(self, cmd): |
| 56 | + if self.is_open(): |
| 57 | + self.dbgMsg(f'sendCommandBlind: {cmd}') |
| 58 | + try: |
| 59 | + ret = self.Telescope.CommandBlind(cmd, False) |
| 60 | + except win32com.client.pywintypes.com_error as e: |
| 61 | + print(f'ERROR in sendCommandBlind: {e}') |
| 62 | + return None |
| 63 | + else: |
| 64 | + return ret |
| 65 | + return None |
| 66 | + |
| 67 | + # The :ED# command sends the LCD contents, coded with the char table of the SED1233 LCD controller. |
| 68 | + # For any reason the COM interface or the win32com transforms this into unicode. Unfortunately the |
| 69 | + # special characters of the SED1233 controller get mapped to the wrong unicode. Here we fix this |
| 70 | + # with a translation table: |
| 71 | + CharacterTranslationTable = { |
| 72 | + 0x0d: ord('\n'), |
| 73 | + # 0x2020: ord(' '), |
| 74 | + 0xDF: ord('°'), |
| 75 | + 0x7E: 0x2192, # ord('>'), |
| 76 | + 0x7F: 0x2190, # ord('<'), |
| 77 | + 0x18: 0x2191, # ord('^'), |
| 78 | + 0x19: 0x2193, # ord('v'), |
| 79 | + # bar graph symbols |
| 80 | + 0x5F: 0x2582, |
| 81 | + 0x81: 0x2583, |
| 82 | + 0x201A: 0x2584, # raw: 0x82 |
| 83 | + 0x0192: 0x2585, # raw: 0x83 |
| 84 | + 0x201E: 0x2586, # raw: 0x84 |
| 85 | + 0x2026: 0x2587, # raw: 0x85 |
| 86 | + 0x2020: 0x2588, # raw: 0x86 |
| 87 | + } |
| 88 | + |
| 89 | + def get_LCD(self): |
| 90 | + if self.is_open(): |
| 91 | + try: |
| 92 | + Response = self.Telescope.CommandString("ED", False) |
| 93 | + except win32com.client.pywintypes.com_error as e: |
| 94 | + # Sometimes the handbox needs long time for calculations and does not |
| 95 | + # send the LCD contents before the ASCOM driver trows a timeout exception. |
| 96 | + # Here we catch these timeout exceptions. |
| 97 | + print(f'ERROR in get_LCD: {e}') |
| 98 | + self.dbgMsg(f'get_LCD response: ED --> {Response}') |
| 99 | + return Response[1:].translate(self.CharacterTranslationTable) |
| 100 | + return None |
0 commit comments